import { useCallback } from 'react';
import axios from 'axios';
import { jsonToQueryParams } from '@/utils';

const useCallApi = () => {
  const GetExportData = useCallback(async (url: string, param: any, fileName: string) => {
    try {
      url = `${url}?` + jsonToQueryParams(param);

      const currentDate = new Date().toISOString().split('T')[0];
      fileName = `${fileName}${currentDate}.xlsx`;
      const link = document.createElement('a');
      link.href = url;
      link.download = fileName;

      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(url);

      return true;
    } catch (error: any) {
      const { response } = error;
      return {
        status: false,
        message: response?.data?.error ?? response?.data?.message ?? 'Unknown error',
        data: null
      };
    }
  }, []);

  const GetData = useCallback(async (url: string, field: any) => {
    try {
      const response = await axios.get(url, { params: field });
      const result = response.data;

      if (result.status) {
        return { status: true, message: 'Success fetch data', data: result.data };
      }
    } catch (error: any) {
      const { response } = error;
      return { status: false, message: response.data.error ?? response.data.message, data: null };
    }
  }, []);

  const PostData = useCallback(async (url: string, field: any) => {
    try {
      const response = await axios.post(url, field);
      const result = response.data;

      if (result.status) {
        return { status: true, message: result.data };
      }
    } catch (error: any) {
      const { response } = error;
      return { status: false, message: response.data.error ?? response.data.message };
    }
  }, []);

  const PostDataFile = useCallback(async (url: string, field: any, config = {}) => {
    try {
      const response = await axios.post(url, field, config);
      const result = response.data;
  
      if (result.status) {
        return { status: true, message: result.data };
      }
      return { status: false, message: result.message };
    } catch (error: any) {
      const { response } = error;
      return {
        status: false,
        message: response?.data?.error ?? response?.data?.message ?? 'Something went wrong'
      };
    }
  }, []);

  const PutData = useCallback(async (url: string, field: any) => {
    try {
      const response = await axios.put(url, field);
      const result = response.data;

      if (result.status) {
        return { status: true, message: result.data };
      }
    } catch (error: any) {
      const { response } = error;
      return { status: false, message: response.data.error ?? response.data.message };
    }
  }, []);

  const DeleteData = useCallback(async (url: string, field: any) => {
    try {
      const response = await axios.delete(url, field);
      const result = response.data;

      if (result.status) {
        return { status: true, message: result.data };
      }
    } catch (error: any) {
      const { response } = error;
      return { status: false, message: response.data.error ?? response.data.message };
    }
  }, []);

  return { GetData, PostData, PutData, DeleteData, GetExportData, PostDataFile };
};

export { useCallApi };
