'use client';
import queryString from 'qs';
import { useEffect, useRef, useState } from 'react';
import { useLocale } from '../providers/Locale/index.js';
import { useTranslation } from '../providers/Translation/index.js';
import { requests } from '../utilities/api.js';
const usePayloadAPI = (url, options = {})=>{
    const { initialData, initialParams = {} } = options;
    const { i18n } = useTranslation();
    const [data, setData] = useState(initialData);
    const [params, setParams] = useState(initialParams);
    const [isLoading, setIsLoading] = useState(!initialData);
    const [isError, setIsError] = useState(false);
    const { code: locale } = useLocale();
    const hasInitialized = useRef(false);
    const search = queryString.stringify({
        locale,
        ...typeof params === 'object' ? params : {}
    }, {
        addQueryPrefix: true
    });
    useEffect(()=>{
        if (initialData && !hasInitialized.current) {
            hasInitialized.current = true;
            return;
        }
        const abortController = new AbortController();
        const fetchData = async ()=>{
            setIsError(false);
            setIsLoading(true);
            try {
                const response = await requests.get(`${url}${search}`, {
                    headers: {
                        'Accept-Language': i18n.language
                    },
                    signal: abortController.signal
                });
                if (response.status > 201) {
                    setIsError(true);
                }
                const json = await response.json();
                setData(json);
                setIsLoading(false);
            } catch (error) {
                if (!abortController.signal.aborted) {
                    setIsError(true);
                    setIsLoading(false);
                }
            }
        };
        if (url) {
            fetchData();
        } else {
            setIsError(false);
            setIsLoading(false);
        }
        return ()=>{
            abortController.abort();
        };
    }, [
        url,
        locale,
        search,
        i18n.language
    ]);
    return [
        {
            data,
            isError,
            isLoading
        },
        {
            setParams
        }
    ];
};
export default usePayloadAPI;

//# sourceMappingURL=usePayloadAPI.js.map