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

const useApiFetchLocations = () => {
    const { refetch, ...query } = useQuery({
        queryKey: ['locations'],
        queryFn: async () => {
            try {
                const response = await axios.get(`/api/location/all-establishments`);
                console.log("fetched locations");
                return response.data;
            } catch (error) {
                throw new Error(`Erreur lors de la requête API : ${error?.message}`);
            }
        }
    });

    const manualRefetch = async () => {
        await refetch();
    };

    return { manualRefetch, ...query };
};

export default useApiFetchLocations;
