import { UdpPageEnums } from "../../enums/pageEnums.mjs";
import { DeviceTypeEnums, MenuItemEnums } from "../../enums/unitySystemEnums.mjs";
import { getAccessToken } from "../apiHelpers.mjs";
import * as Icons from "@fluentui/react-icons";
import { isDesktop, isMobileOnly, isTablet } from "react-device-detect";
//#region src/utilities/menus/menuUtilities.js
const TENANT_AWARE_MENU_TYPES = [
	MenuItemEnums.Page,
	MenuItemEnums.Route,
	MenuItemEnums.Inquiry,
	MenuItemEnums.Report
];
const getTenantAwareHref = (link, user, menuItemId) => {
	if (!link || !user?.currentTenantId) return void 0;
	if (user.currentTenantId === user.defaultTenantId) return void 0;
	if (!TENANT_AWARE_MENU_TYPES.includes(menuItemId)) return void 0;
	return `${link}${link.includes("?") ? "&" : "?"}tenantId=${user.currentTenantId}`;
};
const getMenuItemsRecursively = (menuItems, activeOnly, user, checkDeviceSize = true) => {
	return menuItems.reduce((formattedItems, item) => {
		try {
			if (activeOnly && !item.menuItemInstance.isActive) return formattedItems;
			const { FluentIcon, WorkFlowIcon, AppIcon, Label, Slug, RedirectURL, AppendTokenToURL, DevicesHiddenOn, appendCurrentTenant, ...otherParams } = JSON.parse(item.menuItemInstance.parameters);
			let redirectInfo = "";
			if (RedirectURL) {
				redirectInfo = RedirectURL;
				if (appendCurrentTenant) redirectInfo = `${RedirectURL}?tenantId=${user?.currentTenantId || ""}`;
			}
			const link = getFormattedSlug(Slug, { ...otherParams }) ?? "";
			const formattedParameters = {
				id: item.menuItemInstance.menuItemInstanceId,
				label: Label ?? "",
				link,
				tenantHref: getTenantAwareHref(link, user, item.menuItemInstance.menuItemId),
				redirectInfo,
				getAccessToken: getAccessToken && AppendTokenToURL ? getAccessToken : null,
				menuItemId: item.menuItemInstance.menuItemId,
				menuStructureId: item.menuStructureId,
				parentId: item.parentId,
				productId: item.menuItemInstance.productId,
				isActive: item.menuItemInstance.isActive,
				roleIds: item.menuItemInstance.roleIds,
				tenantSsoId: item?.menuItemInstance?.tenantSsoId,
				devicesHiddenOn: DevicesHiddenOn || [],
				params: { ...otherParams }
			};
			let iconName = "";
			if (FluentIcon) iconName = FluentIcon;
			else if (WorkFlowIcon) iconName = WorkFlowIcon;
			formattedParameters.icon = iconName ? Icons[iconName] : null;
			formattedParameters.iconName = iconName ? iconName : null;
			formattedParameters["items"] = item.menuItemInstanceStructures.length ? getMenuItemsRecursively(item.menuItemInstanceStructures, activeOnly, user, getAccessToken, checkDeviceSize) : [];
			if (checkDeviceSize) {
				if (!(isDesktop && formattedParameters?.devicesHiddenOn?.includes(DeviceTypeEnums.Desktop)) && !(isTablet && formattedParameters?.devicesHiddenOn?.includes(DeviceTypeEnums.Tablet)) && !(isMobileOnly && formattedParameters?.devicesHiddenOn?.includes(DeviceTypeEnums.Mobile))) formattedItems.push(formattedParameters);
			} else formattedItems.push(formattedParameters);
		} catch {}
		return formattedItems;
	}, []);
};
const getFormattedSlug = (slug, params) => {
	let formattedSlug = slug;
	if (params?.PageId === UdpPageEnums.Udp_Maintenance_Engine_Page) formattedSlug = `${slug}${params?.entityName ? `?entityName=${params?.entityName}` : ""}`;
	else if (params?.PageId === UdpPageEnums.Udp_Data_Browser_Page) formattedSlug = `${slug}?queryId=${params?.queryId}`;
	return formattedSlug;
};
const findHomeRouteInStructures = (structures) => {
	for (const item of structures) {
		try {
			const params = JSON.parse(item.menuItemInstance.parameters);
			if (params?.isHomeRoute) return params.Slug ?? null;
		} catch {}
		if (item.menuItemInstanceStructures?.length) {
			const found = findHomeRouteInStructures(item.menuItemInstanceStructures);
			if (found) return found;
		}
	}
	return null;
};
const findHomeRouteSlug = (menu) => {
	for (const m of menu) {
		if (!m.active) continue;
		const found = findHomeRouteInStructures(m.menuItemInstanceStructureViews ?? []);
		if (found) return found;
	}
	return null;
};
const getRouteRolesMapAndMenuActiveMap = (menu) => {
	const updatedRolesMap = /* @__PURE__ */ new Map();
	const updatedisMenuActiveMap = /* @__PURE__ */ new Map();
	menu.filter((m) => m.isActive).forEach((m) => {
		m.menuItemInstanceStructureViews.map((menuItemInstanceStructureView) => {
			let menuItemInstance = menuItemInstanceStructureView.menuItemInstance;
			if (menuItemInstance.menuItemId == MenuItemEnums.Route || menuItemInstance.menuItemId == MenuItemEnums.Page || menuItemInstance.menuItemId == MenuItemEnums.Report || menuItemInstance.menuItemId == MenuItemEnums.Inquiry || menuItemInstance.menuItemId == MenuItemEnums.Inner_Menu) try {
				const slug = JSON.parse(menuItemInstance.parameters)?.Slug;
				updatedRolesMap.set(slug, menuItemInstance.roleIds);
				updatedisMenuActiveMap.set(slug, menuItemInstance.isActive);
			} catch (e) {
				console.error("Failed to parse menu item parameters", e);
			}
		});
	});
	return {
		rolesMap: updatedRolesMap,
		menuActiveMap: updatedisMenuActiveMap
	};
};
//#endregion
export { findHomeRouteSlug, getMenuItemsRecursively, getRouteRolesMapAndMenuActiveMap };

//# sourceMappingURL=menuUtilities.mjs.map