import axios from 'axios';
import { API_URL_PAYMENT } from '@/shared/api/api-url-payment';
import { resultify } from '@/shared/lib/resultify';
import { NiceAxiosError } from '@/shared/@types/error';
import { 
  PaymentInstallmentParams,
  PaymentInstallmentResponse
} from '../model/types';
import { 
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';
import { getHeaderUserAgent } from '@/shared/constants/url';

export const paymentInstallment = (params: PaymentInstallmentParams) => {
  let headerOptions = {
    menuId: 42,
    apiType: 'SELECT'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  return resultify(
    axios.post<PaymentInstallmentResponse>(API_URL_PAYMENT.paymentInstallment(), params, options),
  );
};

export const usePaymentInstallmentMutation = (options?: UseMutationOptions<PaymentInstallmentResponse, NiceAxiosError, PaymentInstallmentParams>) => {
  const mutation = useMutation<PaymentInstallmentResponse, NiceAxiosError, PaymentInstallmentParams>({
    ...options,
    mutationFn: (params: PaymentInstallmentParams) => paymentInstallment(params),
  });

  return {
    ...mutation,
  };
};
