import React from "react";
import { acquireAccessToken } from './auth/authActions';
import { InteractionRequiredAuthError } from '@azure/msal-browser';
import { useTenantStore } from '../stores/tenantStore';
import { useSnackbar } from 'notistack';
import { Button } from '@material-ui/core';
import { useCallback } from 'react';
import { useHistory } from 'react-router-dom';
export const getMergedConfig = (accessToken, config) => {
    var _a;
    var _b;
    const headers = (_b = config === null || config === void 0 ? void 0 : config.headers) !== null && _b !== void 0 ? _b : {};
    const tenantId = (_a = useTenantStore.getState().currentTenant) === null || _a === void 0 ? void 0 : _a.tenantId;
    if (tenantId && !headers.currentTenantId) {
        headers.currentTenantId = tenantId;
    }
    if (accessToken)
        headers.Authorization || (headers.Authorization = `Bearer ${accessToken}`);
    config.headers = headers;
    return config;
};
export const handleResponseErrors = async (error) => {
    var _a;
    if (((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
        await acquireAccessToken();
    }
};
export const getAccessToken = async () => {
    return await acquireAccessToken();
};
/**
 * Checks to make sure the error isn't cancelled, if so just return w/o doing anything
 *Otherwise call a pre-method, create a snackbar, then call a post-method
 *preCallBack returns a bool, true = show snackbar
 */
export const useHandleAxiosSnackbar = () => {
    const { enqueueSnackbar, closeSnackbar } = useSnackbar();
    const history = useHistory();
    const dismissSnackbar = useCallback((key, callback) => {
        closeSnackbar(key);
        callback === null || callback === void 0 ? void 0 : callback();
    }, [closeSnackbar]);
    const handleErrorSnackbar = useCallback((error, customMessage, preCallBack, callback) => {
        var _a, _b;
        const fallbackErrorMessage = 'Error executing request';
        if (((_a = error === null || error === void 0 ? void 0 : error.toString()) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'cancel' ||
            (error === null || error === void 0 ? void 0 : error.code) === 'ERR_CANCELED')
            return;
        // Don't call the preCallback unless we actually failed.
        const showSnackBar = preCallBack === null || preCallBack === void 0 ? void 0 : preCallBack();
        if (preCallBack && !showSnackBar)
            return;
        let errorMessage = customMessage !== null && customMessage !== void 0 ? customMessage : (((_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.data) || (error === null || error === void 0 ? void 0 : error.message) || fallbackErrorMessage);
        if (typeof errorMessage !== 'string' ||
            !errorMessage ||
            errorMessage.length === 0) {
            errorMessage = fallbackErrorMessage;
        }
        enqueueSnackbar(errorMessage, {
            variant: 'error',
            anchorOrigin: {
                vertical: 'top',
                horizontal: 'center'
            },
            action: (key) => (React.createElement(Button, { color: "secondary", size: "small", onClick: () => {
                    dismissSnackbar(key);
                } }, "O"))
        });
        callback === null || callback === void 0 ? void 0 : callback();
    }, [dismissSnackbar, enqueueSnackbar]);
    const handleSuccessSnackbar = useCallback((message, preCallBack, callback, navigationPath, buttonText) => {
        const fallbackSuccessMessage = 'Executed request successfully';
        const showMessage = preCallBack === null || preCallBack === void 0 ? void 0 : preCallBack();
        if (preCallBack && !showMessage)
            return;
        let successMessage = message !== null && message !== void 0 ? message : fallbackSuccessMessage;
        if (typeof successMessage !== 'string' ||
            !successMessage ||
            successMessage.length === 0) {
            successMessage = fallbackSuccessMessage;
        }
        const options = {
            variant: 'default',
            anchorOrigin: {
                vertical: 'top',
                horizontal: 'center'
            },
            action: (key) => (React.createElement(React.Fragment, null, navigationPath && (React.createElement(Button, { color: "secondary", size: "small", onClick: () => {
                    history.push(navigationPath);
                    dismissSnackbar(key);
                } }, buttonText ? buttonText : 'Go to page')), React.createElement(Button, { color: "secondary", size: "small", onClick: () => {
                    dismissSnackbar(key);
                } }, "O")))
        };
        enqueueSnackbar(successMessage, options);
    }, [dismissSnackbar, enqueueSnackbar]);
    return { handleErrorSnackbar, handleSuccessSnackbar };
};
//# sourceMappingURL=apiHelpers.js.map