import { getLoginRequest } from "../utilities/auth/signInAuthProvider.mjs";
import { useUser } from "../utilities/auth/useUser.mjs";
import { useIsUnityAuthenticated } from "../utilities/auth/useIsUnityAuthenticated.mjs";
import { useTheme } from "../utilities/theme/useTheme.mjs";
import { PageForbiddenRoute } from "./components/PageForbiddenRoute.mjs";
import { useMenuContext } from "../utilities/menus/MenuProvider.mjs";
import { useRegisterRoute } from "./useRegisterRoute.mjs";
import { LoadingIndicator } from "../UI/loading/LoadingIndicator/LoadingIndicator.mjs";
import { storeRedirectPath } from "../utilities/redirect/LocalRedirectUrlStorage.mjs";
import { PasswordResetRedirect } from "./components/PasswordResetRedirect.mjs";
import { useTenant } from "../utilities/tenant/useTenant.mjs";
import React, { useMemo } from "react";
import { InteractionStatus, InteractionType } from "@azure/msal-browser";
import { Route } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { MsalAuthenticationTemplate, useMsal } from "@azure/msal-react";
//#region src/routes/PrivateRoute.tsx
const PrivateRoute = ({ rolesMap: propRolesMap, menuActiveMap: propMenuActiveMap, ...routeProps }) => {
	const location = useRegisterRoute({ isPublic: false });
	const isAuthenticated = useIsUnityAuthenticated();
	const { inProgress } = useMsal();
	const user = useUser();
	const tenant = useTenant();
	const theme = useTheme();
	const { t } = useTranslation();
	const menuContext = useMenuContext?.();
	const rolesMap = menuContext?.rolesMap ?? propRolesMap;
	const menuActiveMap = menuContext?.menuActiveMap ?? propMenuActiveMap;
	const userRoles = useMemo(() => {
		return Array.isArray(user?.roles) ? user.roles.map((role) => role.roleId).filter(Boolean) : [];
	}, [user?.roles]);
	const userHasRoles = useMemo(() => {
		if (rolesMap?.size > 0) {
			if (!rolesMap.has(routeProps.location?.pathname)) return false;
			const routeRoles = rolesMap.get(routeProps.location?.pathname);
			if (!routeRoles?.length) return true;
			return routeRoles.some((element) => userRoles.includes(element));
		}
		return true;
	}, [
		userRoles,
		rolesMap,
		routeProps.location?.pathname
	]);
	const menuIsActive = useMemo(() => {
		if (menuActiveMap?.size > 0 && menuActiveMap.has(routeProps.location?.pathname)) return menuActiveMap.get(routeProps.location?.pathname);
	}, [menuActiveMap, routeProps.location?.pathname]);
	const activeTenant = tenant?.activeTenant ?? null;
	const isUserInitialized = Boolean(user?.userId);
	const isTenantInitialized = Boolean(activeTenant?.tenantId);
	const isThemeInitialized = Boolean(theme?.isInitialized);
	if (isAuthenticated && (!isUserInitialized || !isTenantInitialized) || isAuthenticated && !isThemeInitialized) return /* @__PURE__ */ React.createElement(LoadingIndicator, null);
	if (!isAuthenticated) {
		if (inProgress === InteractionStatus.Startup || inProgress === InteractionStatus.HandleRedirect) return /* @__PURE__ */ React.createElement(LoadingIndicator, null);
		if (inProgress !== InteractionStatus.None) return /* @__PURE__ */ React.createElement(LoadingIndicator, null);
		storeRedirectPath(location.pathname, location.search);
		let loginRequest = null;
		try {
			loginRequest = getLoginRequest();
		} catch {
			loginRequest = null;
		}
		if (!loginRequest) return /* @__PURE__ */ React.createElement(LoadingIndicator, null);
		return /* @__PURE__ */ React.createElement(MsalAuthenticationTemplate, {
			interactionType: InteractionType.Redirect,
			authenticationRequest: loginRequest,
			loadingComponent: () => /* @__PURE__ */ React.createElement(LoadingIndicator, null),
			errorComponent: (props) => {
				if (inProgress !== InteractionStatus.None) return /* @__PURE__ */ React.createElement(LoadingIndicator, null);
				if (!(Boolean(props?.error?.errorMessage) || /(^|&)(error|error_description)=/.test(window.location.hash?.replace(/^#/, "") ?? ""))) return /* @__PURE__ */ React.createElement(LoadingIndicator, null);
				return /* @__PURE__ */ React.createElement(PasswordResetRedirect, props);
			}
		}, /* @__PURE__ */ React.createElement(Route, routeProps));
	}
	if (rolesMap?.has(routeProps.location?.pathname) && !menuIsActive) return /* @__PURE__ */ React.createElement(Route, {
		path: "*",
		render: () => /* @__PURE__ */ React.createElement(PageForbiddenRoute, {
			message: t("Trying to access inactive menu. Please check your menu configurations"),
			showButton: false
		})
	});
	if (!user?.isUnitySystemAdmin && rolesMap?.size > 0 && !userHasRoles) return /* @__PURE__ */ React.createElement(Route, {
		path: "*",
		render: () => /* @__PURE__ */ React.createElement(PageForbiddenRoute, {
			message: t("You do not have the required roles to access this page. Please contact an administrator to get access"),
			showButton: false
		})
	});
	if (isUserInitialized) return /* @__PURE__ */ React.createElement(Route, routeProps);
	return /* @__PURE__ */ React.createElement(LoadingIndicator, null);
};
//#endregion
export { PrivateRoute };

//# sourceMappingURL=PrivateRoute.mjs.map