import { useEffect, useMemo } from 'react';
import useAxios from 'axios-hooks';
import Axios from 'axios';
import { handleResponseErrors, getMergedConfig, getAccessToken } from './apiHelpers';
import enableAxiosInterceptors from './axiosInterceptors';
import { useUser } from './auth/useUser';
enableAxiosInterceptors();
export const useAxiosMutate = (baseURL, url, config) => {
    const fullURL = { url: `${baseURL}/${url}` };
    const user = useUser();
    const headers = useMemo(() => {
        var _a, _b;
        const currentHeaders = (_a = config.headers) !== null && _a !== void 0 ? _a : {};
        currentHeaders.currentTenantId || (currentHeaders.currentTenantId = (_b = user === null || user === void 0 ? void 0 : user.currentTenantId) !== null && _b !== void 0 ? _b : '');
        return currentHeaders;
    }, [user, config]);
    config.headers = headers;
    const [{ data, loading, error }, execute] = useAxios(Object.assign(Object.assign({}, fullURL), config), { manual: true });
    useEffect(() => {
        handleResponseErrors(error);
    }, [error]);
    return [{ data, loading, error }, execute];
};
export const apiMutate = async (baseURL, url, config, data) => {
    const fullURL = { url: `${baseURL}/${url}` };
    const accessToken = await getAccessToken();
    const mergedConfig = getMergedConfig(accessToken, config);
    return Axios(Object.assign(Object.assign(Object.assign({}, fullURL), mergedConfig), data));
};
//# sourceMappingURL=useAxiosMutate.js.map