import { useQuery } from 'react-query';
import useAxios from './useAxios';
import useAuthState from '../../lib/state mangment/AuthState';
import { useNavigate } from 'react-router-dom';

function useGetQuery(KEY: string | string[], url: string , params:any={},options:any={}) {
  const axios = useAxios();
  const {logout} = useAuthState()
  const navigate = useNavigate()
  const language = localStorage.getItem("language") ?? "en"

  return useQuery(
    params ? [Array.isArray(KEY) ? KEY.join(',') : KEY, params,language] : [Array.isArray(KEY) ? KEY.join(',') : KEY,language],
     async () => {
    const response = await axios.get(url , {params});
    return response.data; 
  },


  {
    onError: (error:any) => {
      console.log('====================================');
      console.log(error.response.status);
      console.log('====================================');
      if(error.response.status == 401 || error.response.status == 403){
          logout()
          navigate("/auth")


      }
      
    },
    refetchOnWindowFocus: false,
    
    ...options

  }
  );
}

export default useGetQuery;
