/**
 * Generated by orval v8.8.0 🍺
 * Do not edit manually.
 * OpenAPI spec version: 0.0.0
 */
import { useMutation, useQuery } from "@tanstack/react-query";
import type {
  DataTag,
  DefinedInitialDataOptions,
  DefinedUseQueryResult,
  MutationFunction,
  QueryClient,
  QueryFunction,
  QueryKey,
  UndefinedInitialDataOptions,
  UseMutationOptions,
  UseMutationResult,
  UseQueryOptions,
  UseQueryResult,
} from "@tanstack/react-query";

import type {
  GopayCreatePayment201,
  GopayGetStatus200,
  GopayRefundPayment200,
  PaymentCreate,
  Refund,
} from "../models";

import { privateMutator } from "../../../privateClient";

// https://stackoverflow.com/questions/49579094/typescript-conditional-types-filter-out-readonly-properties-pick-only-requir/49579497#49579497
type IfEquals<X, Y, A = X, B = never> =
  (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? A : B;

type WritableKeys<T> = {
  [P in keyof T]-?: IfEquals<
    { [Q in P]: T[P] },
    { -readonly [Q in P]: T[P] },
    P
  >;
}[keyof T];

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
  k: infer I,
) => void
  ? I
  : never;
type DistributeReadOnlyOverUnions<T> = T extends any ? NonReadonly<T> : never;

type Writable<T> = Pick<T, WritableKeys<T>>;
type NonReadonly<T> = [T] extends [UnionToIntersection<T>]
  ? {
      [P in keyof Writable<T>]: T[P] extends object
        ? NonReadonly<NonNullable<T[P]>>
        : T[P];
    }
  : DistributeReadOnlyOverUnions<T>;

/**
 * Provede refundaci platby v GoPay a uloží záznam refundace.
 * @summary Refundovat platbu
 */
export const gopayRefundPayment = (
  paymentId: string,
  refund: NonReadonly<Refund>,
  signal?: AbortSignal,
) => {
  return privateMutator<GopayRefundPayment200>({
    url: `/api/payments/gopay/${paymentId}/refund/`,
    method: "POST",
    headers: { "Content-Type": "application/json" },
    data: refund,
    signal,
  });
};

export const getGopayRefundPaymentMutationOptions = <
  TError = void,
  TContext = unknown,
>(options?: {
  mutation?: UseMutationOptions<
    Awaited<ReturnType<typeof gopayRefundPayment>>,
    TError,
    { paymentId: string; data: NonReadonly<Refund> },
    TContext
  >;
}): UseMutationOptions<
  Awaited<ReturnType<typeof gopayRefundPayment>>,
  TError,
  { paymentId: string; data: NonReadonly<Refund> },
  TContext
> => {
  const mutationKey = ["gopayRefundPayment"];
  const { mutation: mutationOptions } = options
    ? options.mutation &&
      "mutationKey" in options.mutation &&
      options.mutation.mutationKey
      ? options
      : { ...options, mutation: { ...options.mutation, mutationKey } }
    : { mutation: { mutationKey } };

  const mutationFn: MutationFunction<
    Awaited<ReturnType<typeof gopayRefundPayment>>,
    { paymentId: string; data: NonReadonly<Refund> }
  > = (props) => {
    const { paymentId, data } = props ?? {};

    return gopayRefundPayment(paymentId, data);
  };

  return { mutationFn, ...mutationOptions };
};

export type GopayRefundPaymentMutationResult = NonNullable<
  Awaited<ReturnType<typeof gopayRefundPayment>>
>;
export type GopayRefundPaymentMutationBody = NonReadonly<Refund>;
export type GopayRefundPaymentMutationError = void;

/**
 * @summary Refundovat platbu
 */
export const useGopayRefundPayment = <TError = void, TContext = unknown>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof gopayRefundPayment>>,
      TError,
      { paymentId: string; data: NonReadonly<Refund> },
      TContext
    >;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof gopayRefundPayment>>,
  TError,
  { paymentId: string; data: NonReadonly<Refund> },
  TContext
> => {
  return useMutation(
    getGopayRefundPaymentMutationOptions(options),
    queryClient,
  );
};
/**
 * Načte aktuální stav platby GoPay a případně synchronizuje lokální záznam.
 * @summary Získat stav platby
 */
export const gopayGetStatus = (paymentId: string, signal?: AbortSignal) => {
  return privateMutator<GopayGetStatus200>({
    url: `/api/payments/gopay/${paymentId}/status/`,
    method: "GET",
    signal,
  });
};

export const getGopayGetStatusQueryKey = (paymentId: string) => {
  return [`/api/payments/gopay/${paymentId}/status/`] as const;
};

export const getGopayGetStatusQueryOptions = <
  TData = Awaited<ReturnType<typeof gopayGetStatus>>,
  TError = void,
>(
  paymentId: string,
  options?: {
    query?: Partial<
      UseQueryOptions<Awaited<ReturnType<typeof gopayGetStatus>>, TError, TData>
    >;
  },
) => {
  const { query: queryOptions } = options ?? {};

  const queryKey =
    queryOptions?.queryKey ?? getGopayGetStatusQueryKey(paymentId);

  const queryFn: QueryFunction<Awaited<ReturnType<typeof gopayGetStatus>>> = ({
    signal,
  }) => gopayGetStatus(paymentId, signal);

  return {
    queryKey,
    queryFn,
    enabled: !!paymentId,
    ...queryOptions,
  } as UseQueryOptions<
    Awaited<ReturnType<typeof gopayGetStatus>>,
    TError,
    TData
  > & { queryKey: DataTag<QueryKey, TData, TError> };
};

export type GopayGetStatusQueryResult = NonNullable<
  Awaited<ReturnType<typeof gopayGetStatus>>
>;
export type GopayGetStatusQueryError = void;

export function useGopayGetStatus<
  TData = Awaited<ReturnType<typeof gopayGetStatus>>,
  TError = void,
>(
  paymentId: string,
  options: {
    query: Partial<
      UseQueryOptions<Awaited<ReturnType<typeof gopayGetStatus>>, TError, TData>
    > &
      Pick<
        DefinedInitialDataOptions<
          Awaited<ReturnType<typeof gopayGetStatus>>,
          TError,
          Awaited<ReturnType<typeof gopayGetStatus>>
        >,
        "initialData"
      >;
  },
  queryClient?: QueryClient,
): DefinedUseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useGopayGetStatus<
  TData = Awaited<ReturnType<typeof gopayGetStatus>>,
  TError = void,
>(
  paymentId: string,
  options?: {
    query?: Partial<
      UseQueryOptions<Awaited<ReturnType<typeof gopayGetStatus>>, TError, TData>
    > &
      Pick<
        UndefinedInitialDataOptions<
          Awaited<ReturnType<typeof gopayGetStatus>>,
          TError,
          Awaited<ReturnType<typeof gopayGetStatus>>
        >,
        "initialData"
      >;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useGopayGetStatus<
  TData = Awaited<ReturnType<typeof gopayGetStatus>>,
  TError = void,
>(
  paymentId: string,
  options?: {
    query?: Partial<
      UseQueryOptions<Awaited<ReturnType<typeof gopayGetStatus>>, TError, TData>
    >;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
/**
 * @summary Získat stav platby
 */

export function useGopayGetStatus<
  TData = Awaited<ReturnType<typeof gopayGetStatus>>,
  TError = void,
>(
  paymentId: string,
  options?: {
    query?: Partial<
      UseQueryOptions<Awaited<ReturnType<typeof gopayGetStatus>>, TError, TData>
    >;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
} {
  const queryOptions = getGopayGetStatusQueryOptions(paymentId, options);

  const query = useQuery(queryOptions, queryClient) as UseQueryResult<
    TData,
    TError
  > & { queryKey: DataTag<QueryKey, TData, TError> };

  return { ...query, queryKey: queryOptions.queryKey };
}

/**
 * Vytvoří platbu v GoPay s minimálními povinnými poli. Citlivé údaje o kartě se neposílají, platbu obslouží GoPay stránka (gw_url). Pokud není zadán notification_url, použije se hodnota ze settings.GOPAY_NOTIFICATION_URL.
 * @summary Vytvořit platbu (minimální vstup)
 */
export const gopayCreatePayment = (
  paymentCreate: PaymentCreate,
  signal?: AbortSignal,
) => {
  return privateMutator<GopayCreatePayment201>({
    url: `/api/payments/gopay/create/`,
    method: "POST",
    headers: { "Content-Type": "application/json" },
    data: paymentCreate,
    signal,
  });
};

export const getGopayCreatePaymentMutationOptions = <
  TError = void,
  TContext = unknown,
>(options?: {
  mutation?: UseMutationOptions<
    Awaited<ReturnType<typeof gopayCreatePayment>>,
    TError,
    { data: PaymentCreate },
    TContext
  >;
}): UseMutationOptions<
  Awaited<ReturnType<typeof gopayCreatePayment>>,
  TError,
  { data: PaymentCreate },
  TContext
> => {
  const mutationKey = ["gopayCreatePayment"];
  const { mutation: mutationOptions } = options
    ? options.mutation &&
      "mutationKey" in options.mutation &&
      options.mutation.mutationKey
      ? options
      : { ...options, mutation: { ...options.mutation, mutationKey } }
    : { mutation: { mutationKey } };

  const mutationFn: MutationFunction<
    Awaited<ReturnType<typeof gopayCreatePayment>>,
    { data: PaymentCreate }
  > = (props) => {
    const { data } = props ?? {};

    return gopayCreatePayment(data);
  };

  return { mutationFn, ...mutationOptions };
};

export type GopayCreatePaymentMutationResult = NonNullable<
  Awaited<ReturnType<typeof gopayCreatePayment>>
>;
export type GopayCreatePaymentMutationBody = PaymentCreate;
export type GopayCreatePaymentMutationError = void;

/**
 * @summary Vytvořit platbu (minimální vstup)
 */
export const useGopayCreatePayment = <TError = void, TContext = unknown>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof gopayCreatePayment>>,
      TError,
      { data: PaymentCreate },
      TContext
    >;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof gopayCreatePayment>>,
  TError,
  { data: PaymentCreate },
  TContext
> => {
  return useMutation(
    getGopayCreatePaymentMutationOptions(options),
    queryClient,
  );
};
