/**
 * @packageDocumentation
 * @module api.functional.payments
 * @nestia Generated by Nestia - https://github.com/samchon/nestia
 */
//================================================================
import type { IConnection } from "@nestia/fetcher";
import { PlainFetcher } from "@nestia/fetcher/lib/PlainFetcher";

import type { IIamportPayment } from "../../structures/IIamportPayment";
import type { IIamportPaymentCancel } from "../../structures/IIamportPaymentCancel";
import type { IIamportResponse } from "../../structures/IIamportResponse";

/**
 * 결제 기록 열람하기.
 *
 * 아임포트를 통하여 발생한 결제 기록을 열람한다.
 *
 * @param imp_uid 대상 결제 기록의 {@link IIamportPayment.imp_uid }
 * @param query 결제 수단이 페이팔인 경우에 사용
 * @returns 결제 정보
 * @security bearer
 * @author Samchon
 *
 * @controller FakeIamportPaymentsController.at
 * @path GET /payments/:imp_uid
 * @nestia Generated by Nestia - https://github.com/samchon/nestia
 */
export async function at(
  connection: IConnection,
  imp_uid: string,
  query: IIamportPayment.IQuery,
): Promise<at.Output> {
  return PlainFetcher.fetch(connection, {
    ...at.METADATA,
    template: at.METADATA.path,
    path: at.path(imp_uid, query),
  });
}
export namespace at {
  export type Query = IIamportPayment.IQuery;
  export type Output = IIamportResponse<IIamportPayment>;

  export const METADATA = {
    method: "GET",
    path: "/payments/:imp_uid",
    request: null,
    response: {
      type: "application/json",
      encrypted: false,
    },
    status: 200,
  } as const;

  export const path = (imp_uid: string, query: at.Query) => {
    const variables: URLSearchParams = new URLSearchParams();
    for (const [key, value] of Object.entries(query as any))
      if (undefined === value) continue;
      else if (Array.isArray(value))
        value.forEach((elem: any) => variables.append(key, String(elem)));
      else variables.set(key, String(value));
    const location: string = `/payments/${encodeURIComponent(imp_uid?.toString() ?? "null")}`;
    return 0 === variables.size
      ? location
      : `${location}?${variables.toString()}`;
  };
}

/**
 * 결제 취소하기.
 *
 * 만약 가상 계좌를 통한 결제였다면, 반드시 환불 계좌 정보를 입력해줘야 한다.
 *
 * @param input 결제 취소 입력 정보
 * @returns 취소된 결제 정보
 * @security bearer
 * @author Samchon
 *
 * @controller FakeIamportPaymentsController.cancel
 * @path POST /payments/cancel
 * @nestia Generated by Nestia - https://github.com/samchon/nestia
 */
export async function cancel(
  connection: IConnection,
  input: IIamportPaymentCancel.ICreate,
): Promise<cancel.Output> {
  return PlainFetcher.fetch(
    {
      ...connection,
      headers: {
        ...connection.headers,
        "Content-Type": "application/json",
      },
    },
    {
      ...cancel.METADATA,
      template: cancel.METADATA.path,
      path: cancel.path(),
    },
    input,
  );
}
export namespace cancel {
  export type Input = IIamportPaymentCancel.ICreate;
  export type Output = IIamportResponse<IIamportPayment>;

  export const METADATA = {
    method: "POST",
    path: "/payments/cancel",
    request: {
      type: "application/json",
      encrypted: false,
    },
    response: {
      type: "application/json",
      encrypted: false,
    },
    status: 201,
  } as const;

  export const path = () => "/payments/cancel";
}
