import axios from 'axios';
import { API_URL_TRANSACTION } from '@/shared/api/api-url-transaction';
import { resultify } from '@/shared/lib/resultify';
import { NiceAxiosError } from '@/shared/@types/error';
import { 
  DownloadExcelParams,
  DownloadExcelResponse
} from '../model/types';
import { 
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';
import { getHeaderUserAgent } from '@/shared/constants/url';

export const downloadExcel = (params: DownloadExcelParams) => {
  let headerOptions = {
    menuId: 31,
    apiType: 'DOWNLOAD'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  return resultify(
    axios.post<DownloadExcelResponse>(API_URL_TRANSACTION.downloadExcel(), params, options),
  );
};

export const useDownloadExcelMutation = (options?: UseMutationOptions<DownloadExcelResponse, NiceAxiosError, DownloadExcelParams>) => {
  const mutation = useMutation<DownloadExcelResponse, NiceAxiosError, DownloadExcelParams>({
    ...options,
    mutationFn: (params: DownloadExcelParams) => downloadExcel(params),
  });

  return {
    ...mutation,
  };
};
