import { useQuery } from '@tanstack/react-query'
import { toast } from 'react-toastify'
import { useAxios } from './useAxios'

export const getCountry = () =>
  useAxios({
    url: 'country',
    method: 'GET',
  })

export type COUNTRY = {
  country_name: string
  country_code: string
  country_number_code: string
  country_language: string
  country_VAT: string
  country_flag_image: string
  currency_id?: string
  currency?: string
  country_currency?: string
  file?: string
  id?: string
}

export const useCountry = () => {
  const query = useQuery({
    queryKey: ['getCountry'],
    queryFn: getCountry,
    onError: (error: { message: string }) => {
      toast.error(error?.message || 'Failed to fetch countries', {
        closeButton: false,
        autoClose: 2000,
        hideProgressBar: true,
      })
    },
  })

  return {
    isCountryDataLoading: query.isLoading,
    countries: query.data?.data || [],
    refetchCountry: query.refetch,
  }
}
