import { QueryFunction, QueryKey, useQuery } from '@tanstack/react-query'
import { AxiosError } from 'axios'
import { CustomUseQueryOptions } from '../../@core/types/api'

export const useCustomQuery = <TQueryFnData = any, TQueryKey extends QueryKey = QueryKey, TData = TQueryFnData>(
  queryKey: TQueryKey,
  queryFn: QueryFunction<TQueryFnData, TQueryKey>,
  options?: CustomUseQueryOptions<TQueryFnData, TQueryKey, TData, AxiosError<any, any>>
) => {
  const query = useQuery<TQueryFnData, AxiosError<any>, TData, TQueryKey>({
    queryKey,
    queryFn,
    refetchInterval: false,
    refetchOnMount: 'always',
    refetchOnReconnect: 'always',
    refetchOnWindowFocus: false,
    ...options
  })

  return query
}
