import { useMutation, useQuery } from '@tanstack/react-query';

import api from '../services/api';
import { ExportPersonResponse } from '../services/api/APIS/Reports/type';
import { downloadFileAsync } from '../services/nativeHelpers/donwloadFIleAsync';

export const useExportPersonAllGet = (token: string, disabled = false) => {
  return useQuery(['export-person-all', token], () => api.summary.exportPersonAllGet(token), {
    enabled: !!token && !disabled,
  });
};

export const useExportPersonGet = (token: string, closeModal: () => void) => {
  return useMutation(
    ['export-person', token],
    ({ exportId, format }: { exportId: string; format: string }) =>
      api.reports.exportPersonGet(token, exportId, format),
    {
      onSuccess: async (data?: ExportPersonResponse) => {
        closeModal();
        downloadFileAsync(
          data?.content || '',
          data?.fileName || '',
          data?.format.toLocaleLowerCase() || '',
        );
      },
    },
  );
};

export const useExportPersonViewGet = (token: string, templateId: string, disabled = false) => {
  return useQuery(
    ['export-person-view', token, templateId],
    () => api.summary.exportPersonViewGet(token, templateId),
    {
      enabled: !!token && !!templateId && !disabled,
    },
  );
};
