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

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