import Axios from 'axios';
import { getAccessToken, getMergedConfig } from './apiHelpers';
import { whitelistedPaths } from './auth/whitelistedPaths.internal';
const axiosInterceptors = () => {
    Axios.interceptors.request.use(async (config) => {
        var _a;
        // don't want to change the access token if it's already there, this rule is already applied in getMergedConfig
        // we don't want to call getAccessToken until it is actually need it because it might log the user out
        if (!whitelistedPaths.includes(window.location.pathname)) {
            let accessToken = '';
            if (!((_a = config === null || config === void 0 ? void 0 : config.headers) === null || _a === void 0 ? void 0 : _a.Authorization)) {
                accessToken = await getAccessToken();
            }
            return getMergedConfig(accessToken, config);
        }
        return config;
    }, (error) => Promise.reject(error));
    Axios.interceptors.response.use((response) => response, async (error) => {
        var _a;
        const config = error === null || error === void 0 ? void 0 : error.config;
        if (((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 &&
            !(config === null || config === void 0 ? void 0 : config._retry) &&
            !whitelistedPaths.includes(window.location.pathname)) {
            config.headers.Authorization = `Bearer ${await getAccessToken()}`;
            config._retry = true;
            return Axios(config);
        }
        return Promise.reject(error);
    });
};
export default axiosInterceptors;
//# sourceMappingURL=axiosInterceptors.js.map