import { ConfigService } from "../configService/index.mjs";
import { TreeViewModeEnums } from "../enums/unitySystemEnums.mjs";
import { useUser } from "../utilities/auth/useUser.mjs";
import { useShellStore } from "../stores/shellStore.mjs";
import { transformMenuPageTree } from "../utilities/tree/TreeUtilities.mjs";
import { useAxiosGet } from "../utilities/useAxiosGet.mjs";
import { useSetLocationState } from "../hooks/useSetLocationState.mjs";
import { PageContainer } from "../page/PageContainer/PageContainer.mjs";
import { useBuildEnvironment } from "../shell/ui/useBuildEnvironment.mjs";
import { NodeLoader } from "../UI/feedback/NodeLoader.mjs";
import { UtilitySideBar } from "../UI/utilityDisplay/UtilitySideBar.mjs";
import { BaseTreeMenu } from "../treeMenu/BaseTreeMenu.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { makeStyles } from "@material-ui/core";
import { Prompt, useHistory, useLocation } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
//#region src/menuPage/MenuPage.jsx
const useStyles = makeStyles((theme) => ({
	root: {
		flex: 1,
		height: "100%",
		minHeight: 0,
		display: "grid",
		gridTemplateAreas: ["sidebar content"].map((line) => `"${line}"`).join(" "),
		gridTemplateColumns: "auto 1fr",
		gridTemplateRows: "auto 1fr"
	},
	sidebar: {
		gridArea: "sidebar",
		marginRight: theme.spacing(1),
		display: "flex",
		flexDirection: "column",
		minHeight: 0
	},
	content: {
		gridArea: "content",
		display: "flex",
		flexDirection: "column",
		position: "relative",
		zIndex: 1,
		height: `100%`,
		minHeight: 0,
		overflowY: "auto"
	},
	menuContent: {
		marginTop: theme.spacing(1),
		flex: 1,
		minHeight: 0,
		overflowX: "hidden",
		overflowY: "auto"
	},
	loader: {
		marginRight: theme.spacing(1),
		marginLeft: theme.spacing(1)
	},
	loadingContent: {
		flex: 1,
		display: "flex",
		flexDirection: "column",
		alignItems: "center",
		justifyContent: "center"
	}
}));
const EMPTY_HEADERS = {};
const findNodeById = (nodes, id) => {
	if (!nodes || !id) return null;
	for (const node of nodes) {
		if (String(node.id) === String(id)) return node;
		if (node.children) {
			const found = findNodeById(node.children, id);
			if (found) return found;
		}
	}
	return null;
};
const MenuPage = (props) => {
	const $ = c(94);
	const t0 = useBuildEnvironment() ? 25 : 0;
	let t1;
	if ($[0] !== props || $[1] !== t0) {
		t1 = {
			...props,
			appRibbonSpace: t0
		};
		$[0] = props;
		$[1] = t0;
		$[2] = t1;
	} else t1 = $[2];
	const classes = useStyles(t1);
	const history = useHistory();
	const location = useLocation();
	const user = useUser();
	const { t } = useTranslation();
	const setLocationState = useSetLocationState();
	const { innerMenuOverride, mainContent, menuTitle, allowMissingPageParam: t2, searchParamsTransformer } = props;
	const allowMissingPageParam = t2 === void 0 ? false : t2;
	const [sidebarExpanded, setSidebarExpanded] = useState(true);
	const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
	const { setSubHeaderActionHasChildren, setNavigationExpanded } = useShellStore();
	let t3;
	if ($[3] !== location.pathname) {
		t3 = location.pathname.replace("/menupage/", "");
		$[3] = location.pathname;
		$[4] = t3;
	} else t3 = $[4];
	const innerMenuId = t3;
	const [t4] = useAxiosGet(ConfigService.tenantV1ApiUrl, `menus/${innerMenuId}`, EMPTY_HEADERS, !innerMenuId || innerMenuOverride, false);
	const { data: fetchedInnerMenu, loading: menuLoading } = t4;
	const innerMenu = innerMenuOverride ?? fetchedInnerMenu;
	let t5;
	bb0: {
		if (!innerMenu) {
			let t6;
			if ($[5] === Symbol.for("react.memo_cache_sentinel")) {
				t6 = [];
				$[5] = t6;
			} else t6 = $[5];
			t5 = t6;
			break bb0;
		}
		let t6;
		if ($[6] !== innerMenu.menuItemInstanceStructureViews) {
			t6 = innerMenu.menuItemInstanceStructureViews?.reduce(_temp, []) ?? [];
			$[6] = innerMenu.menuItemInstanceStructureViews;
			$[7] = t6;
		} else t6 = $[7];
		t5 = t6;
	}
	const menuItemsList = t5;
	let t6;
	if ($[8] !== location.search) {
		t6 = new URLSearchParams(location.search).get("page");
		$[8] = location.search;
		$[9] = t6;
	} else t6 = $[9];
	const pageId = t6;
	let t7;
	bb1: {
		if (!menuItemsList.length) {
			t7 = null;
			break bb1;
		}
		let t8;
		if ($[10] !== menuItemsList || $[11] !== pageId) {
			t8 = findNodeById(menuItemsList, pageId);
			$[10] = menuItemsList;
			$[11] = pageId;
			$[12] = t8;
		} else t8 = $[12];
		const found = t8;
		if (found) {
			t7 = found;
			break bb1;
		}
		t7 = menuItemsList[0];
	}
	const selectedNode = t7;
	const [t8] = useAxiosGet(ConfigService.tenantV1ApiUrl, `page/${selectedNode?.properties?.page?.id}`, EMPTY_HEADERS, !selectedNode?.properties?.page?.id, false);
	const { data: pageData, loading: pageLoading } = t8;
	let t9;
	if ($[13] !== location || $[14] !== searchParamsTransformer) {
		t9 = (params, node, menuIndex) => {
			if (typeof searchParamsTransformer === "function") {
				const result = searchParamsTransformer({
					searchParams: params,
					node,
					menuIndex,
					location
				});
				if (result === null) return "";
				if (result instanceof URLSearchParams) return result.toString();
				if (typeof result === "string") return result;
				if (result !== void 0) return String(result);
			}
			return params.toString();
		};
		$[13] = location;
		$[14] = searchParamsTransformer;
		$[15] = t9;
	} else t9 = $[15];
	const buildSearchString = t9;
	let t10;
	if ($[16] !== allowMissingPageParam || $[17] !== buildSearchString || $[18] !== history || $[19] !== location.pathname || $[20] !== location.search || $[21] !== menuItemsList[0]) {
		t10 = (node_0, menuIndex_0) => {
			if (allowMissingPageParam) {
				const firstNode = menuItemsList?.[0];
				if (firstNode && String(node_0?.id) === String(firstNode.id)) {
					if (location.search) history.replace({
						pathname: location.pathname,
						search: ""
					});
					setHasUnsavedChanges(false);
					return;
				}
			}
			const searchParams = new URLSearchParams(location.search);
			searchParams.set("page", node_0.id);
			const searchString = buildSearchString(searchParams, node_0, menuIndex_0);
			history.push({
				pathname: location.pathname,
				search: searchString
			});
		};
		$[16] = allowMissingPageParam;
		$[17] = buildSearchString;
		$[18] = history;
		$[19] = location.pathname;
		$[20] = location.search;
		$[21] = menuItemsList[0];
		$[22] = t10;
	} else t10 = $[22];
	const handleSelectedNode = t10;
	let t11;
	if ($[23] === Symbol.for("react.memo_cache_sentinel")) {
		t11 = () => {
			setSidebarExpanded(_temp2);
		};
		$[23] = t11;
	} else t11 = $[23];
	const handleSidebarExpandClick = t11;
	const handleDropNode = _temp3;
	let t12;
	if ($[24] !== allowMissingPageParam || $[25] !== history || $[26] !== location.pathname || $[27] !== location.search || $[28] !== selectedNode) {
		t12 = () => {
			if (selectedNode && !allowMissingPageParam) {
				const currentUrlId = new URLSearchParams(location.search).get("page");
				if (String(selectedNode.id) !== String(currentUrlId)) {
					const newParams = new URLSearchParams(location.search);
					newParams.set("page", selectedNode.id);
					history.replace({
						pathname: location.pathname,
						search: newParams.toString()
					});
				}
			}
		};
		$[24] = allowMissingPageParam;
		$[25] = history;
		$[26] = location.pathname;
		$[27] = location.search;
		$[28] = selectedNode;
		$[29] = t12;
	} else t12 = $[29];
	const t13 = selectedNode?.id;
	let t14;
	if ($[30] !== allowMissingPageParam || $[31] !== history || $[32] !== location.search || $[33] !== t13) {
		t14 = [
			t13,
			location.search,
			history,
			allowMissingPageParam
		];
		$[30] = allowMissingPageParam;
		$[31] = history;
		$[32] = location.search;
		$[33] = t13;
		$[34] = t14;
	} else t14 = $[34];
	useEffect(t12, t14);
	let t15;
	if ($[35] !== selectedNode?.id || $[36] !== setNavigationExpanded || $[37] !== setSubHeaderActionHasChildren) {
		t15 = () => {
			if (selectedNode?.id) {
				setNavigationExpanded(false);
				setHasUnsavedChanges(false);
				setSubHeaderActionHasChildren(false);
			}
		};
		$[35] = selectedNode?.id;
		$[36] = setNavigationExpanded;
		$[37] = setSubHeaderActionHasChildren;
		$[38] = t15;
	} else t15 = $[38];
	const t16 = selectedNode?.id;
	let t17;
	if ($[39] !== setNavigationExpanded || $[40] !== setSubHeaderActionHasChildren || $[41] !== t16) {
		t17 = [
			t16,
			setNavigationExpanded,
			setSubHeaderActionHasChildren
		];
		$[39] = setNavigationExpanded;
		$[40] = setSubHeaderActionHasChildren;
		$[41] = t16;
		$[42] = t17;
	} else t17 = $[42];
	useEffect(t15, t17);
	let t18;
	if ($[43] !== innerMenu || $[44] !== location.state?.label || $[45] !== setLocationState || $[46] !== t) {
		t18 = () => {
			if (innerMenu && location.state?.label !== t(innerMenu?.name)) setLocationState({ label: t(innerMenu?.name) });
		};
		$[43] = innerMenu;
		$[44] = location.state?.label;
		$[45] = setLocationState;
		$[46] = t;
		$[47] = t18;
	} else t18 = $[47];
	const t19 = location.state?.label;
	let t20;
	if ($[48] !== innerMenu || $[49] !== setLocationState || $[50] !== t || $[51] !== t19) {
		t20 = [
			setLocationState,
			t,
			innerMenu,
			t19
		];
		$[48] = innerMenu;
		$[49] = setLocationState;
		$[50] = t;
		$[51] = t19;
		$[52] = t20;
	} else t20 = $[52];
	useEffect(t18, t20);
	let t21;
	if ($[53] !== hasUnsavedChanges) {
		t21 = /* @__PURE__ */ React.createElement(Prompt, {
			when: hasUnsavedChanges,
			message: "You have unsaved changes. Are you sure you want to leave this page?"
		});
		$[53] = hasUnsavedChanges;
		$[54] = t21;
	} else t21 = $[54];
	const t22 = innerMenu?.name || menuTitle;
	let t23;
	if ($[55] !== classes.nodeContainer || $[56] !== handleSelectedNode || $[57] !== menuItemsList || $[58] !== menuLoading || $[59] !== selectedNode?.id || $[60] !== selectedNode?.parentId || $[61] !== sidebarExpanded) {
		t23 = menuItemsList?.length > 0 && !menuLoading && menuItemsList.map((menuItems, index) => /* @__PURE__ */ React.createElement("div", {
			key: menuItems.id,
			className: classes.nodeContainer
		}, /* @__PURE__ */ React.createElement(BaseTreeMenu, {
			menuItems,
			onSelectNode: handleSelectedNode,
			onDropNode: handleDropNode,
			menuIndex: index,
			selectedNodeId: selectedNode?.id,
			selectedNodeParentId: selectedNode?.parentId,
			sidebarExpanded,
			treeViewMode: TreeViewModeEnums.Classic
		})));
		$[55] = classes.nodeContainer;
		$[56] = handleSelectedNode;
		$[57] = menuItemsList;
		$[58] = menuLoading;
		$[59] = selectedNode?.id;
		$[60] = selectedNode?.parentId;
		$[61] = sidebarExpanded;
		$[62] = t23;
	} else t23 = $[62];
	let t24;
	if ($[63] !== classes.loader || $[64] !== menuLoading) {
		t24 = menuLoading && /* @__PURE__ */ React.createElement("div", { className: classes.loader }, /* @__PURE__ */ React.createElement(NodeLoader, { loading: menuLoading }));
		$[63] = classes.loader;
		$[64] = menuLoading;
		$[65] = t24;
	} else t24 = $[65];
	let t25;
	if ($[66] !== classes.menuContent || $[67] !== innerMenuId || $[68] !== t23 || $[69] !== t24) {
		t25 = /* @__PURE__ */ React.createElement("div", {
			className: classes.menuContent,
			key: innerMenuId
		}, t23, t24);
		$[66] = classes.menuContent;
		$[67] = innerMenuId;
		$[68] = t23;
		$[69] = t24;
		$[70] = t25;
	} else t25 = $[70];
	let t26;
	if ($[71] !== t22 || $[72] !== t25) {
		t26 = /* @__PURE__ */ React.createElement(UtilitySideBar, {
			title: t22,
			isUsingChildren: true,
			resizable: true,
			removePadding: true,
			removeTopLayoutUnit: true,
			onUtilitySidebarToggleExpandClick: handleSidebarExpandClick,
			collapsedWidth: 64
		}, t25);
		$[71] = t22;
		$[72] = t25;
		$[73] = t26;
	} else t26 = $[73];
	let t27;
	if ($[74] !== classes.sidebar || $[75] !== t26) {
		t27 = /* @__PURE__ */ React.createElement("div", { className: classes.sidebar }, t26);
		$[74] = classes.sidebar;
		$[75] = t26;
		$[76] = t27;
	} else t27 = $[76];
	let t28;
	if ($[77] !== mainContent || $[78] !== menuItemsList || $[79] !== pageData || $[80] !== pageLoading || $[81] !== selectedNode || $[82] !== user) {
		t28 = mainContent ? typeof mainContent === "function" ? mainContent({
			selectedNode,
			menuItemsList
		}) : mainContent : selectedNode?.properties?.page?.id && /* @__PURE__ */ React.createElement(PageContainer, {
			user,
			key: selectedNode?.id,
			pageData,
			pageLoading,
			widgetParams: {
				pageId: pageData?.pageId,
				entityName: selectedNode?.properties?.page?.entity?.entityName,
				pbiReportCode: selectedNode?.properties?.page?.pbiReportCode,
				requiredFieldsMap: selectedNode?.properties?.page?.requiredFieldMapping,
				udpTagId: selectedNode?.properties?.page?.udpTagId,
				queryId: selectedNode?.properties?.page?.queryId,
				inquiryTreeId: selectedNode?.properties?.page?.inquiryTreeId,
				setHasUnsavedChanges
			},
			disableMargins: true,
			fromMenu: true
		});
		$[77] = mainContent;
		$[78] = menuItemsList;
		$[79] = pageData;
		$[80] = pageLoading;
		$[81] = selectedNode;
		$[82] = user;
		$[83] = t28;
	} else t28 = $[83];
	let t29;
	if ($[84] !== classes.content || $[85] !== t28) {
		t29 = /* @__PURE__ */ React.createElement("div", { className: classes.content }, t28);
		$[84] = classes.content;
		$[85] = t28;
		$[86] = t29;
	} else t29 = $[86];
	let t30;
	if ($[87] !== classes.root || $[88] !== t27 || $[89] !== t29) {
		t30 = /* @__PURE__ */ React.createElement("div", { className: classes.root }, t27, t29);
		$[87] = classes.root;
		$[88] = t27;
		$[89] = t29;
		$[90] = t30;
	} else t30 = $[90];
	let t31;
	if ($[91] !== t21 || $[92] !== t30) {
		t31 = /* @__PURE__ */ React.createElement(DndProvider, { backend: HTML5Backend }, t21, t30);
		$[91] = t21;
		$[92] = t30;
		$[93] = t31;
	} else t31 = $[93];
	return t31;
};
function _temp(acc, menuInstance) {
	const updatedMenu = transformMenuPageTree(menuInstance);
	if (updatedMenu) acc.push(updatedMenu);
	return acc;
}
function _temp2(expanded) {
	return !expanded;
}
function _temp3() {}
//#endregion
export { MenuPage as default };

//# sourceMappingURL=MenuPage.mjs.map