import { acquireAccessToken } from "./auth/authActions.mjs";
import { useTenantStore } from "../stores/tenantStore.mjs";
import React, { useCallback } from "react";
import { Button } from "@material-ui/core";
import "@azure/msal-browser";
import { useSnackbar } from "notistack";
import { useHistory } from "react-router-dom";
//#region src/utilities/apiHelpers.jsx
const getMergedConfig = (accessToken, config) => {
	const headers = config?.headers ?? {};
	const tenantId = useTenantStore.getState().currentTenant?.tenantId;
	if (tenantId && !headers.currentTenantId) headers.currentTenantId = tenantId;
	if (accessToken) headers.Authorization ||= `Bearer ${accessToken}`;
	config.headers = headers;
	return config;
};
const handleResponseErrors = async (error) => {
	if (error?.response?.status === 401) await acquireAccessToken();
};
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
*/
const useHandleAxiosSnackbar = () => {
	const { enqueueSnackbar, closeSnackbar } = useSnackbar();
	const history = useHistory();
	const dismissSnackbar = useCallback((key, callback) => {
		closeSnackbar(key);
		callback?.();
	}, [closeSnackbar]);
	return {
		handleErrorSnackbar: useCallback((error, customMessage, preCallBack, callback_0) => {
			const fallbackErrorMessage = "Error executing request";
			if (error?.toString()?.toLowerCase() === "cancel" || error?.code === "ERR_CANCELED") return;
			const showSnackBar = preCallBack?.();
			if (preCallBack && !showSnackBar) return;
			let errorMessage = customMessage ?? (error?.response?.data || error?.message || fallbackErrorMessage);
			if (typeof errorMessage !== "string" || !errorMessage || errorMessage.length === 0) errorMessage = fallbackErrorMessage;
			enqueueSnackbar(errorMessage, {
				variant: "error",
				anchorOrigin: {
					vertical: "top",
					horizontal: "center"
				},
				action: (key_0) => /* @__PURE__ */ React.createElement(Button, {
					color: "secondary",
					size: "small",
					onClick: () => {
						dismissSnackbar(key_0);
					}
				}, "Ok")
			});
			callback_0?.();
		}, [dismissSnackbar, enqueueSnackbar]),
		handleSuccessSnackbar: useCallback((message, preCallBack_0, callback_1, navigationPath, buttonText) => {
			const fallbackSuccessMessage = "Executed request successfully";
			const showMessage = preCallBack_0?.();
			if (preCallBack_0 && !showMessage) return;
			let successMessage = message ?? fallbackSuccessMessage;
			if (typeof successMessage !== "string" || !successMessage || successMessage.length === 0) successMessage = fallbackSuccessMessage;
			enqueueSnackbar(successMessage, {
				variant: "default",
				anchorOrigin: {
					vertical: "top",
					horizontal: "center"
				},
				action: (key_1) => /* @__PURE__ */ React.createElement(React.Fragment, null, navigationPath && /* @__PURE__ */ React.createElement(Button, {
					color: "secondary",
					size: "small",
					onClick: () => {
						history.push(navigationPath);
						dismissSnackbar(key_1);
					}
				}, buttonText ? buttonText : "Go to page"), /* @__PURE__ */ React.createElement(Button, {
					color: "secondary",
					size: "small",
					onClick: () => {
						dismissSnackbar(key_1);
					}
				}, "Ok"))
			});
		}, [dismissSnackbar, enqueueSnackbar])
	};
};
//#endregion
export { getAccessToken, getMergedConfig, handleResponseErrors, useHandleAxiosSnackbar };

//# sourceMappingURL=apiHelpers.mjs.map