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

const useApiFetchLocation = (locationId: any) => {
    const { refetch, ...query } = useQuery({
        queryKey: ['location', locationId],
        queryFn: async () => {
            try {
                console.log("fetching location: ", locationId);
                const response = await axios.get(`/admin/location/${locationId}`);
                console.log("fetched location: ", response);
                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 useApiFetchLocation;
