import { ConfigService } from "../../configService/index.mjs";
import { UserContext, UserDispatchContext } from "./internal.mjs";
import { getStoredTenant, storeTenant } from "../storage/UnitySessionStorage.mjs";
import { useApiQuery } from "../useApiQuery.mjs";
import { LoadingIndicator } from "../../UI/loading/LoadingIndicator/LoadingIndicator.mjs";
import { TenantProvider } from "../tenant/TenantProvider.mjs";
import { User } from "./user.mjs";
import { c } from "react-compiler-runtime";
import React, { Suspense, lazy, useEffect, useState } from "react";
import { keepPreviousData } from "@tanstack/react-query";
import { useAccount } from "@azure/msal-react";
//#region src/utilities/auth/UserProvider.jsx
const UserErrorModal = lazy(() => import("./UserErrorModal.mjs"));
const UserProvider = (props) => {
	const $ = c(22);
	const { children, isWhiteListed, doNotWaitForTenant: t0, unityTenantId } = props;
	const doNotWaitForTenant = t0 === void 0 ? false : t0;
	const activeAccount = useAccount();
	let t1;
	if ($[0] !== unityTenantId) {
		t1 = () => {
			const urlTenant = new URLSearchParams(window.location.search).get("tenantId");
			if (urlTenant) return urlTenant;
			const storedTenant = getStoredTenant();
			if (storedTenant) return storedTenant;
			return unityTenantId || null;
		};
		$[0] = unityTenantId;
		$[1] = t1;
	} else t1 = $[1];
	const [activeTenantId, setActiveTenantId] = useState(t1);
	const unityUserUrl = activeTenantId ? `user/unityuser?tenantId=${encodeURIComponent(activeTenantId)}` : "user/unityuser";
	let t2;
	if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
		t2 = {};
		$[2] = t2;
	} else t2 = $[2];
	const t3 = !!activeAccount;
	let t4;
	if ($[3] !== t3) {
		t4 = {
			enabled: t3,
			staleTime: 0,
			placeholderData: keepPreviousData
		};
		$[3] = t3;
		$[4] = t4;
	} else t4 = $[4];
	const { data: userInfo, error: userError, isLoading: userInfoLoading } = useApiQuery(ConfigService.securityV2ApiUrl, unityUserUrl, t2, t4);
	let t5;
	if ($[5] !== userInfo) {
		t5 = userInfo ? new User(userInfo) : null;
		$[5] = userInfo;
		$[6] = t5;
	} else t5 = $[6];
	const user = t5;
	const isErrorModalOpen = Boolean(userError);
	const errorMessage = userError ? "Unable to get user details. Please try again." : "";
	let t6;
	let t7;
	if ($[7] !== activeTenantId || $[8] !== user) {
		t6 = () => {
			if (user?.userId) sessionStorage.setItem("user-id", user.userId);
			if (user?.currentTenantId && !activeTenantId) {
				storeTenant(user.currentTenantId);
				setActiveTenantId(user.currentTenantId);
			}
		};
		t7 = [user, activeTenantId];
		$[7] = activeTenantId;
		$[8] = user;
		$[9] = t6;
		$[10] = t7;
	} else {
		t6 = $[9];
		t7 = $[10];
	}
	useEffect(t6, t7);
	let t8;
	if ($[11] === Symbol.for("react.memo_cache_sentinel")) {
		t8 = (newTenantId) => {
			if (!newTenantId) return;
			storeTenant(newTenantId);
			setActiveTenantId(newTenantId);
		};
		$[11] = t8;
	} else t8 = $[11];
	const changeTenant = t8;
	const shouldBlockForUser = Boolean(activeAccount) && !user && !userError && userInfoLoading && !isWhiteListed;
	if (isErrorModalOpen) {
		let t9;
		if ($[12] === Symbol.for("react.memo_cache_sentinel")) {
			t9 = /* @__PURE__ */ React.createElement(LoadingIndicator, null);
			$[12] = t9;
		} else t9 = $[12];
		let t10;
		if ($[13] !== errorMessage) {
			t10 = /* @__PURE__ */ React.createElement(Suspense, { fallback: t9 }, /* @__PURE__ */ React.createElement(UserErrorModal, { errorMessage }));
			$[13] = errorMessage;
			$[14] = t10;
		} else t10 = $[14];
		return t10;
	}
	if (!doNotWaitForTenant && shouldBlockForUser) {
		let t9;
		if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
			t9 = /* @__PURE__ */ React.createElement(LoadingIndicator, null);
			$[15] = t9;
		} else t9 = $[15];
		return t9;
	}
	let t9;
	if ($[16] === Symbol.for("react.memo_cache_sentinel")) {
		t9 = { changeTenant };
		$[16] = t9;
	} else t9 = $[16];
	let t10;
	if ($[17] !== children) {
		t10 = /* @__PURE__ */ React.createElement(UserDispatchContext.Provider, { value: t9 }, /* @__PURE__ */ React.createElement(TenantProvider, null, children));
		$[17] = children;
		$[18] = t10;
	} else t10 = $[18];
	let t11;
	if ($[19] !== t10 || $[20] !== user) {
		t11 = /* @__PURE__ */ React.createElement(UserContext.Provider, { value: user }, t10);
		$[19] = t10;
		$[20] = user;
		$[21] = t11;
	} else t11 = $[21];
	return t11;
};
//#endregion
export { UserProvider as default };

//# sourceMappingURL=UserProvider.mjs.map