import { ConfigService } from "../../configService/index.mjs";
import { useUser } from "../auth/useUser.mjs";
import { useAxiosGet } from "../useAxiosGet.mjs";
import { findHomeRouteSlug, getRouteRolesMapAndMenuActiveMap } from "./menuUtilities.mjs";
import { c } from "react-compiler-runtime";
import React, { createContext, useContext } from "react";
//#region src/utilities/menus/MenuProvider.tsx
const MenuContext = createContext(null);
const buildRouteAndActiveMaps = (menu) => {
	const rawResult = getRouteRolesMapAndMenuActiveMap(menu);
	const rolesMap = /* @__PURE__ */ new Map();
	if (rawResult?.rolesMap instanceof Map) rawResult.rolesMap.forEach((value, key) => {
		if (typeof key !== "string") return;
		const normalizedKey = key;
		const normalizedValue = Array.isArray(value) ? value.map((roleId) => String(roleId)) : [];
		rolesMap.set(normalizedKey, normalizedValue);
	});
	const menuActiveMap = /* @__PURE__ */ new Map();
	if (rawResult?.menuActiveMap instanceof Map) rawResult.menuActiveMap.forEach((value, key) => {
		if (typeof key !== "string") return;
		menuActiveMap.set(key, Boolean(value));
	});
	return {
		rolesMap,
		menuActiveMap
	};
};
const MenuProvider = (t0) => {
	const $ = c(34);
	const { children } = t0;
	const user = useUser();
	const currentTenantId = user?.currentTenantId;
	const userId = user?.id;
	let t1;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t1 = {};
		$[0] = t1;
	} else t1 = $[0];
	const [t2, refetchLeftMenu] = useAxiosGet(ConfigService.tenantV1ApiUrl, `menus?menuTypeId=1&productId=${ConfigService.config.UNITY_PRODUCT_ID}&tenantId=${currentTenantId}`, t1, !currentTenantId);
	const { data: leftMenu, loading: leftMenuLoading, error: leftMenuError } = t2;
	let t3;
	if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
		t3 = {};
		$[1] = t3;
	} else t3 = $[1];
	const [t4, refetchAppSelectorMenus] = useAxiosGet(ConfigService.tenantV1ApiUrl, `menus?tenantId=${currentTenantId}&verticalId=${ConfigService.config.UNITY_VERTICAL_ID}&userId=${userId ?? ""}`, t3, !userId);
	const { data: appSelectorMenus, loading: appSelectorMenusLoading, error: appSelectorMenusError } = t4;
	let t5;
	if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
		t5 = {};
		$[2] = t5;
	} else t5 = $[2];
	const [t6, refetchLanguages] = useAxiosGet(ConfigService.tenantV1ApiUrl, "language", t5, !userId);
	const { data: languages, loading: languagesLoading, error: languagesError } = t6;
	let t7;
	bb0: {
		if (Array.isArray(leftMenu) && leftMenu.length > 0) {
			let t8;
			if ($[3] !== leftMenu) {
				t8 = buildRouteAndActiveMaps(leftMenu);
				$[3] = leftMenu;
				$[4] = t8;
			} else t8 = $[4];
			let t9;
			if ($[5] !== leftMenu) {
				t9 = findHomeRouteSlug(leftMenu);
				$[5] = leftMenu;
				$[6] = t9;
			} else t9 = $[6];
			let t10;
			if ($[7] !== t8 || $[8] !== t9) {
				t10 = {
					...t8,
					homeRoute: t9
				};
				$[7] = t8;
				$[8] = t9;
				$[9] = t10;
			} else t10 = $[9];
			t7 = t10;
			break bb0;
		}
		let t8;
		if ($[10] === Symbol.for("react.memo_cache_sentinel")) {
			t8 = {
				rolesMap: /* @__PURE__ */ new Map(),
				menuActiveMap: /* @__PURE__ */ new Map(),
				homeRoute: null
			};
			$[10] = t8;
		} else t8 = $[10];
		t7 = t8;
	}
	const { rolesMap, menuActiveMap, homeRoute } = t7;
	let t8;
	if ($[11] !== currentTenantId || $[12] !== refetchAppSelectorMenus || $[13] !== refetchLanguages || $[14] !== refetchLeftMenu || $[15] !== userId) {
		t8 = () => {
			const refetchPromises = [];
			if (currentTenantId) refetchPromises.push(refetchLeftMenu());
			if (userId) refetchPromises.push(refetchAppSelectorMenus());
			refetchPromises.push(refetchLanguages());
			return Promise.all(refetchPromises);
		};
		$[11] = currentTenantId;
		$[12] = refetchAppSelectorMenus;
		$[13] = refetchLanguages;
		$[14] = refetchLeftMenu;
		$[15] = userId;
		$[16] = t8;
	} else t8 = $[16];
	const refetchAll = t8;
	let t9;
	if ($[17] !== appSelectorMenus || $[18] !== appSelectorMenusError || $[19] !== appSelectorMenusLoading || $[20] !== homeRoute || $[21] !== languages || $[22] !== languagesError || $[23] !== languagesLoading || $[24] !== leftMenu || $[25] !== leftMenuError || $[26] !== leftMenuLoading || $[27] !== menuActiveMap || $[28] !== refetchAll || $[29] !== rolesMap) {
		t9 = {
			leftMenu,
			leftMenuLoading,
			leftMenuError,
			appSelectorMenus,
			appSelectorMenusLoading,
			appSelectorMenusError,
			languages,
			languagesLoading,
			languagesError,
			rolesMap,
			menuActiveMap,
			homeRoute,
			refetchAll
		};
		$[17] = appSelectorMenus;
		$[18] = appSelectorMenusError;
		$[19] = appSelectorMenusLoading;
		$[20] = homeRoute;
		$[21] = languages;
		$[22] = languagesError;
		$[23] = languagesLoading;
		$[24] = leftMenu;
		$[25] = leftMenuError;
		$[26] = leftMenuLoading;
		$[27] = menuActiveMap;
		$[28] = refetchAll;
		$[29] = rolesMap;
		$[30] = t9;
	} else t9 = $[30];
	const value = t9;
	let t10;
	if ($[31] !== children || $[32] !== value) {
		t10 = /* @__PURE__ */ React.createElement(MenuContext.Provider, { value }, children);
		$[31] = children;
		$[32] = value;
		$[33] = t10;
	} else t10 = $[33];
	return t10;
};
const useMenuContext = () => {
	const $ = c(2);
	const context = useContext(MenuContext);
	let t0;
	if ($[0] !== context) {
		t0 = context ?? {
			leftMenuLoading: false,
			appSelectorMenusLoading: false,
			languagesLoading: false,
			rolesMap: /* @__PURE__ */ new Map(),
			menuActiveMap: /* @__PURE__ */ new Map(),
			homeRoute: null,
			refetchAll: _temp
		};
		$[0] = context;
		$[1] = t0;
	} else t0 = $[1];
	return t0;
};
function _temp() {
	return Promise.resolve([]);
}
//#endregion
export { MenuProvider, useMenuContext };

//# sourceMappingURL=MenuProvider.mjs.map