import { handleResponseErrors } from "./apiHelpers.mjs";
import axiosInterceptors from "./axiosInterceptors.mjs";
import { useUser } from "./auth/useUser.mjs";
import { useEffect, useMemo } from "react";
import useAxios from "axios-hooks";
//#region src/utilities/useAxiosGet.js
axiosInterceptors();
/**
* 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>]}
*/
const useAxiosGetQuery = (url, params, config = {}, isManual) => {
	const user = useUser();
	const { skipTenantHeader, headers: configHeaders, ...restConfig } = config ?? {};
	if (!user?.accessToken) isManual = true;
	/** @type {[AxiosState<T>, import('axios-hooks').Refetch<T>]} */
	const [{ data, loading, error }, refetch] = useAxios({
		url,
		params,
		headers: useMemo(() => {
			const currentHeaders = { ...configHeaders ?? {} };
			if (!skipTenantHeader) currentHeaders.currentTenantId ||= user?.currentTenantId ?? "";
			return currentHeaders;
		}, [
			configHeaders,
			skipTenantHeader,
			user?.currentTenantId
		]),
		...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>]}
*/
const useAxiosGet = (baseURL, url, config = {}, isManual, useCache = true) => {
	const user = useUser();
	const { skipTenantHeader, headers: configHeaders, ...restConfig } = config ?? {};
	const headers = useMemo(() => {
		const currentHeaders = { ...configHeaders ?? {} };
		if (!skipTenantHeader) currentHeaders.currentTenantId ||= user?.currentTenantId ?? "";
		return currentHeaders;
	}, [
		configHeaders,
		skipTenantHeader,
		user?.currentTenantId
	]);
	/** @type {[AxiosState<T>, import('axios-hooks').Refetch<T>]} */
	const [{ data, loading, error }, refetch] = useAxios({
		url: `${baseURL}/${url}`,
		headers,
		...restConfig
	}, {
		manual: isManual,
		useCache
	});
	useEffect(() => {
		handleResponseErrors(error);
	}, [error]);
	return [{
		data,
		loading,
		error
	}, refetch];
};
//#endregion
export { useAxiosGet, useAxiosGetQuery };

//# sourceMappingURL=useAxiosGet.mjs.map