import { useUser } from "../utilities/auth/useUser.mjs";
import { useInquiryStore } from "../stores/inquiryStore.mjs";
import { PageLoading } from "../UI/feedback/PageLoading.mjs";
import { useBuildEnvironment } from "./ui/useBuildEnvironment.mjs";
import { PageActionWrapper } from "../actions/PageActionWrapper.mjs";
import { useRouteRegistry } from "../routes/routeRegistryStore.mjs";
import { findHomeRouteSlug, getMenuItemsRecursively } from "../utilities/menus/menuUtilities.mjs";
import { useMenuContext } from "../utilities/menus/MenuProvider.mjs";
import { UdpRoutes } from "../routes/UdpRoutes.mjs";
import NavigationSidebar from "./ui/NavigationSidebar.mjs";
import SiteHeader from "./ui/SiteHeader.mjs";
import { UtilityBar } from "../UI/utilityDisplay/UtilityBar.mjs";
import { ActionProvider } from "../utilities/provider/ActionProvider.mjs";
import { useWidgetRegistry } from "./useWidgetRegistry.mjs";
import { ChatBotProvider } from "../utilities/chatBot/ChatBotProvider.mjs";
import UserProfileMenuWrapper from "./ui/profile/UserProfileMenuWrapper.mjs";
import { useShellVisibility } from "./useShellVisibility.mjs";
import ErrorBoundary from "../errorBoundary/ErrorBoundary.mjs";
import React, { Suspense, lazy, useCallback, useEffect, useMemo, useState } from "react";
import { Drawer, makeStyles, useMediaQuery, useTheme } from "@material-ui/core";
import { Switch as Switch$1 } from "react-router-dom";
import clsx from "clsx";
import PropTypes from "prop-types";
import { useTranslation } from "react-i18next";
//#region src/shell/Shell.jsx
const FUIAppSwitcher = lazy(() => import("./ui/appSwitcher/FUIAppSwitcher.mjs"));
const UtilitySideBar = lazy(() => import("../UI/utilityDisplay/UtilitySideBar.mjs").then((m) => ({ default: m.UtilitySideBar })));
const MenuTypeEnums = {
	Left_Menu: 1,
	Application_Menu: 2,
	Builders_Menu: 3,
	Favorites_Menu: 4
};
const useStyles = makeStyles((theme) => ({
	"@global": {
		html: { minHeight: "100%" },
		"html, body, #root": {
			display: "flex",
			flexDirection: "column"
		},
		"body, #root": { flex: 1 }
	},
	root: {
		flex: 1,
		minHeight: "100vh",
		display: "grid",
		gridTemplateAreas: ["sidebar header", "sidebar content"].map((line) => `"${line}"`).join(" "),
		gridTemplateColumns: "auto 1fr",
		gridTemplateRows: "auto 1fr"
	},
	header: { gridArea: "header" },
	sidebar: { gridArea: "sidebar" },
	content: {
		gridArea: "content",
		display: "flex",
		flexDirection: "column",
		position: "relative",
		zIndex: 1,
		height: ({ appRibbonSpace }) => `calc(100vh - ${56 + appRibbonSpace}px)`,
		overflowY: "auto"
	},
	paper: {
		width: "100%",
		[theme.breakpoints.up("sm")]: { width: (props) => props.drawerWidth || 520 }
	}
}), { name: "Layout" });
/**
* Core site layout common to all pages
*/
const Shell = (props) => {
	const buildEnv = useBuildEnvironment();
	const classes = useStyles({
		...props,
		ribbonSpace: buildEnv ? 25 : 0
	});
	const { children, userAvatar = /* @__PURE__ */ React.createElement(UserProfileMenuWrapper, null), appContent, leftMenu, appSelectorMenus, siteName, className, navRoot, hideToggle, useMockData, mockMenus, user: passedInUser, unityUrl, expander, sidebarTitle, navMenuHoverOn = false, secondExtended, toggleOne, toggleTwo, closePrimary, closeSecondary, panelExpanded, panelExpandedSecondary, setPanelExpanded = false, setPanelExpandedSecondary = false, breadCrumbAltLabelList = [], currentLocation, utilitySidebarResizable, utilitySidebarToggleOverride, onUtilitySidebarToggleExpandClick, hideLayout = false, getAccessToken, switcherData, selectedItem, switcherWidth, languages, projectSwitcherColor, widgets, getWidget: getWidgetProp, isValidWidget: isValidWidgetProp, appSwitcherHeader, hamburgerMenuAccessRole, hideLanguageSwitcher = false, subheader, hideHelpCenter = false, leftMenuLoading = false } = props;
	const contextUser = useUser();
	const user = passedInUser ?? contextUser;
	useWidgetRegistry({
		widgets,
		getWidget: getWidgetProp,
		isValidWidget: isValidWidgetProp
	});
	const isPublicRoute = useRouteRegistry((state) => state.routeMap[state.currentPath])?.isPublic !== false;
	const [appSwitcherMobile, setAppSwitcherMobile] = useState(false);
	const [appSwitcherDesktop, setAppSwitcherDesktop] = useState(false);
	const { i18n } = useTranslation();
	const inquiryTitle = useInquiryStore((state_0) => state_0.title);
	const menuContext = useMenuContext?.();
	const resolvedLeftMenu = useMemo(() => leftMenuLoading ? [] : leftMenu ?? menuContext?.leftMenu, [
		leftMenuLoading,
		leftMenu,
		menuContext
	]);
	const homeRoute = useMemo(() => leftMenu?.length ? findHomeRouteSlug(leftMenu) : menuContext?.homeRoute, [leftMenu, menuContext]);
	const resolvedAppSelectorMenus = appSelectorMenus ?? menuContext?.appSelectorMenus;
	const providedLanguages = languages ?? menuContext?.languages;
	const resolvedLanguages = Array.isArray(providedLanguages) ? providedLanguages : [];
	const effectiveShellHidden = useShellVisibility({
		hideLayout,
		isPublicRoute,
		hasUser: Boolean(user?.id)
	});
	const getMenuItems = useCallback((menuTypeId, menus) => {
		if (!menus && !useMockData) return [];
		const foundMenus = (useMockData ? mockMenus : menus).filter((menu) => menu.menuTypeId === menuTypeId && menu.active);
		if (foundMenus) return foundMenus.reduce((menuItemInstanceStructureViews, menu_0) => {
			return [...menuItemInstanceStructureViews, ...menu_0.menuItemInstanceStructureViews];
		}, []);
		else return [];
	}, [useMockData, mockMenus]);
	const menus_0 = useMemo(() => {
		return getMenuItemsRecursively(getMenuItems(1, resolvedLeftMenu), true, user);
	}, [
		getMenuItems,
		user,
		resolvedLeftMenu
	]);
	const mobile = useMediaQuery(useTheme().breakpoints.up("sm"));
	const handleAppSwitcher = (type, bool) => {
		if (type === "mobile") setAppSwitcherMobile(bool);
		else setAppSwitcherDesktop(bool);
	};
	const builderMenu = useMemo(() => {
		return getMenuItemsRecursively(getMenuItems(MenuTypeEnums.Builders_Menu, resolvedAppSelectorMenus), true, user, getAccessToken);
	}, [
		getMenuItems,
		user,
		resolvedAppSelectorMenus,
		getAccessToken
	]);
	const favoriteMenu = useMemo(() => {
		return getMenuItemsRecursively(getMenuItems(MenuTypeEnums.Favorites_Menu, resolvedAppSelectorMenus), true, user, getAccessToken);
	}, [
		getMenuItems,
		user,
		resolvedAppSelectorMenus,
		getAccessToken
	]);
	const appMenu = useMemo(() => {
		return getMenuItemsRecursively(getMenuItems(MenuTypeEnums.Application_Menu, resolvedAppSelectorMenus), true, user, getAccessToken);
	}, [
		getMenuItems,
		user,
		resolvedAppSelectorMenus,
		getAccessToken
	]);
	const hideAppMenu = useMemo(() => !resolvedAppSelectorMenus || hamburgerMenuAccessRole && !user?.roleNames.includes(hamburgerMenuAccessRole), [
		user,
		resolvedAppSelectorMenus,
		hamburgerMenuAccessRole
	]);
	useEffect(() => {
		i18n.changeLanguage("en-US");
	}, [i18n]);
	return /* @__PURE__ */ React.createElement(ChatBotProvider, null, /* @__PURE__ */ React.createElement(ActionProvider, null, /* @__PURE__ */ React.createElement("div", { className: classes.root }, !effectiveShellHidden && /* @__PURE__ */ React.createElement(SiteHeader, {
		className: classes.header,
		siteName,
		appContent,
		userAvatar,
		breadCrumbRoot: navRoot,
		setAppSwitcherOpen: (type_0, bool_0) => handleAppSwitcher(type_0, bool_0),
		breadCrumbAltLabelList,
		currentLocation,
		switcherData,
		selectedItem,
		switcherWidth,
		hideAppLaunch: hideAppMenu,
		languages: resolvedLanguages,
		hideLanguageSwitcher,
		backgroundColor: projectSwitcherColor,
		hideHelpCenter
	}), /* @__PURE__ */ React.createElement("div", {
		style: { display: "flex" },
		className: classes.sidebar
	}, !effectiveShellHidden && mobile && /* @__PURE__ */ React.createElement(NavigationSidebar, {
		className: classes.sidebar,
		data: menus_0,
		navMenuHoverOn,
		user,
		useGlobalState: true,
		leftMenuLoading: leftMenuLoading || menuContext?.leftMenuLoading
	}), expander && /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Suspense, { fallback: null }, /* @__PURE__ */ React.createElement(UtilitySideBar, {
		title: inquiryTitle || sidebarTitle,
		resizable: utilitySidebarResizable,
		utilitySidebarToggleOverride,
		onUtilitySidebarToggleExpandClick,
		sidebarHidden: effectiveShellHidden,
		...props
	})))), /* @__PURE__ */ React.createElement("div", { className: classes.content }, !effectiveShellHidden && subheader, /* @__PURE__ */ React.createElement(UtilityBar, {
		className: clsx(className),
		gutters: false,
		hideToggle,
		expander,
		secondExtended,
		toggleOne,
		toggleTwo,
		closePrimary,
		closeSecondary,
		expanded: panelExpanded,
		expandedSecondary: panelExpandedSecondary,
		setExpanded: setPanelExpanded,
		setExpandedSecondary: setPanelExpandedSecondary
	}), /* @__PURE__ */ React.createElement(ErrorBoundary, null, /* @__PURE__ */ React.createElement(PageActionWrapper, null, /* @__PURE__ */ React.createElement(Suspense, { fallback: /* @__PURE__ */ React.createElement(PageLoading, null) }, /* @__PURE__ */ React.createElement(Switch$1, null, children, /* @__PURE__ */ React.createElement(UdpRoutes, { homeRoute })))))), /* @__PURE__ */ React.createElement(Drawer, {
		variant: "temporary",
		anchor: "left",
		open: appSwitcherDesktop,
		classes: !mobile ? {} : !resolvedAppSelectorMenus ? {} : { paper: classes.paper },
		onClose: () => handleAppSwitcher("desktop", false)
	}, mobile && resolvedAppSelectorMenus && /* @__PURE__ */ React.createElement(Suspense, { fallback: null }, /* @__PURE__ */ React.createElement(FUIAppSwitcher, {
		onClose: () => handleAppSwitcher("desktop", false),
		favoriteMenu,
		builderMenu,
		appMenu,
		unityUrl,
		user,
		header: appSwitcherHeader && typeof appSwitcherHeader === "function" ? appSwitcherHeader(() => {
			handleAppSwitcher("desktop", false);
		}) : appSwitcherHeader
	})), (!mobile || mobile && !resolvedAppSelectorMenus) && /* @__PURE__ */ React.createElement(NavigationSidebar, {
		toggle: false,
		className: classes.sidebar,
		data: menus_0,
		navMenuHoverOn,
		user,
		useGlobalState: true,
		leftMenuLoading: leftMenuLoading || menuContext?.leftMenuLoading
	})), /* @__PURE__ */ React.createElement(Drawer, {
		variant: "temporary",
		anchor: "right",
		open: appSwitcherMobile,
		classes: { paper: classes.paper },
		onClose: () => handleAppSwitcher("mobile", false)
	}, /* @__PURE__ */ React.createElement(Suspense, { fallback: null }, /* @__PURE__ */ React.createElement(FUIAppSwitcher, {
		onClose: () => handleAppSwitcher("mobile", false),
		favoriteMenu,
		builderMenu,
		appMenu,
		unityUrl,
		user,
		header: appSwitcherHeader && typeof appSwitcherHeader === "function" ? appSwitcherHeader(() => {
			handleAppSwitcher("mobile", false);
		}) : appSwitcherHeader
	}))))));
};
Shell.propTypes = {
	switcherData: PropTypes.array,
	selectedItem: PropTypes.string,
	switcherWidth: PropTypes.number,
	children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]).isRequired,
	siteName: PropTypes.string,
	userAvatar: PropTypes.element,
	appContent: PropTypes.element,
	leftMenu: PropTypes.array,
	appSelectorMenus: PropTypes.array,
	className: PropTypes.object,
	navRoot: PropTypes.oneOfType([
		PropTypes.string,
		PropTypes.element,
		PropTypes.node
	]),
	useMockData: PropTypes.bool,
	mockMenus: PropTypes.array,
	unityUrl: PropTypes.string,
	expander: PropTypes.bool,
	sidebarTitle: PropTypes.string,
	navMenuHoverOn: PropTypes.bool,
	toggleOne: PropTypes.bool,
	toggleTwo: PropTypes.bool,
	closePrimary: PropTypes.bool,
	closeSecondary: PropTypes.bool,
	panelExpanded: PropTypes.bool,
	panelExpandedSecondary: PropTypes.bool,
	setPanelExpanded: PropTypes.func,
	setPanelExpandedSecondary: PropTypes.func,
	breadCrumbAltLabelList: PropTypes.array,
	currentLocation: PropTypes.object,
	utilitySidebarResizable: PropTypes.bool,
	utilitySidebarToggleOverride: PropTypes.bool,
	onUtilitySidebarToggleExpandClick: PropTypes.func,
	hideLayout: PropTypes.bool,
	languages: PropTypes.arrayOf(PropTypes.object),
	projectSwitcherColor: PropTypes.string,
	widgets: PropTypes.object,
	getWidget: PropTypes.func,
	isValidWidget: PropTypes.func,
	currentTenant: PropTypes.object,
	hamburgerMenuAccessRole: PropTypes.string
};
//#endregion
export { Shell };

//# sourceMappingURL=Shell.mjs.map