var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { useEffect, useMemo } from 'react';
import useAxios from 'axios-hooks';
import { handleResponseErrors, getMergedConfig } from './apiHelpers';
import enableAxiosInterceptors from './axiosInterceptors';
import { useUser } from './auth/useUser';
import { getStoredTenant } from './storage/UnitySessionStorage';
enableAxiosInterceptors();
/**
 * A custom hook for making GET requests with query parameters.
 * @template T - The expected type of the response data.
 * @param {string} url - The URL for the request.
 * @param {object} [params] - Optional object for query parameters.
 * @param {import('axios').AxiosRequestConfig} [config] - Optional Axios request config.
 * @param {boolean} [isManual] - If true, the request is not made automatically.
 * @returns {[AxiosState<T>, import('axios-hooks').Refetch<T>]}
 */
export const useAxiosGetQuery = (url, params, config = {}, isManual) => {
    const user = useUser();
    const _a = config !== null && config !== void 0 ? config : {}, { skipTenantHeader, headers: configHeaders } = _a, restConfig = __rest(_a, ["skipTenantHeader", "headers"]);
    if (!(user === null || user === void 0 ? void 0 : user.accessToken)) {
        isManual = true;
    }
    const headers = useMemo(() => {
        var _a;
        const currentHeaders = Object.assign({}, (configHeaders !== null && configHeaders !== void 0 ? configHeaders : {}));
        if (!skipTenantHeader) {
            currentHeaders.currentTenantId || (currentHeaders.currentTenantId = (_a = user === null || user === void 0 ? void 0 : user.currentTenantId) !== null && _a !== void 0 ? _a : '');
        }
        return currentHeaders;
    }, [configHeaders, skipTenantHeader, user === null || user === void 0 ? void 0 : user.currentTenantId]);
    /** @type {[AxiosState<T>, import('axios-hooks').Refetch<T>]} */
    const [{ data, loading, error }, refetch] = useAxios(Object.assign({ url, params, headers }, restConfig), { manual: isManual });
    useEffect(() => {
        handleResponseErrors(error);
    }, [error]);
    return [{ data, loading, error }, refetch];
};
/**
 * A custom hook for making GET requests with Axios.
 * @template T - The expected type of the response data.
 * @param {string} baseURL - The base URL for the request.
 * @param {string} url - The URL path for the request.
 * @param {import('axios').AxiosRequestConfig} [config={}] - Optional Axios request config.
 * @param {boolean} [isManual] - If true, the request is not made automatically.
 * @param {boolean} [useCache=true] - If true, caching is enabled.
 * @returns {[AxiosState<T>, import('axios-hooks').Refetch<T>]}
 */
export const useAxiosGet = (baseURL, url, config = {}, isManual, useCache = true) => {
    const user = useUser();
    const _a = config !== null && config !== void 0 ? config : {}, { skipTenantHeader, headers: configHeaders } = _a, restConfig = __rest(_a, ["skipTenantHeader", "headers"]);
    const headers = useMemo(() => {
        var _a;
        const currentHeaders = Object.assign({}, (configHeaders !== null && configHeaders !== void 0 ? configHeaders : {}));
        if (!skipTenantHeader) {
            currentHeaders.currentTenantId || (currentHeaders.currentTenantId = (_a = user === null || user === void 0 ? void 0 : user.currentTenantId) !== null && _a !== void 0 ? _a : '');
        }
        return currentHeaders;
    }, [configHeaders, skipTenantHeader, user === null || user === void 0 ? void 0 : user.currentTenantId]);
    const fullURL = { url: `${baseURL}/${url}` };
    /** @type {[AxiosState<T>, import('axios-hooks').Refetch<T>]} */
    const [{ data, loading, error }, refetch] = useAxios(Object.assign(Object.assign(Object.assign({}, fullURL), { headers }), restConfig), { manual: isManual, useCache: useCache });
    useEffect(() => {
        handleResponseErrors(error);
    }, [error]);
    return [{ data, loading, error }, refetch];
};
//# sourceMappingURL=useAxiosGet.js.map