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 { useAxiosGet } from "../utilities/useAxiosGet.mjs";
import { useSetLocationState } from "../hooks/useSetLocationState.mjs";
import { useBuildEnvironment } from "../shell/ui/useBuildEnvironment.mjs";
import { NodeLoader } from "../UI/feedback/NodeLoader.mjs";
import { UtilitySideBar } from "../UI/utilityDisplay/UtilitySideBar.mjs";
import { WorkflowTreeMenu } from "./menus/WorkflowTreeMenu.mjs";
import { transformWorkflowPlayTree } from "../utilities/workflow/workflowUtilities.mjs";
import { WorkflowContent } from "./WorkflowContent.mjs";
import { useUpdateSearchParams } from "../hooks/useUpdateSearchParams.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { makeStyles } from "@material-ui/core";
import { useLocation } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
//#region src/workflow/WorkflowContainer.jsx
const useStyles = makeStyles((theme) => ({
	root: {
		flex: 1,
		minHeight: ({ appRibbonSpace }) => `calc(100vh - ${appRibbonSpace + 56}px)`,
		display: "grid",
		gridTemplateAreas: ["sidebar content"].map((line) => `"${line}"`).join(" "),
		gridTemplateColumns: "auto 1fr",
		gridTemplateRows: "auto 1fr"
	},
	sidebar: {
		gridArea: "sidebar",
		marginRight: theme.spacing(1)
	},
	content: {
		gridArea: "content",
		display: "flex",
		flexDirection: "column",
		position: "relative",
		zIndex: 1,
		height: ({ appRibbonSpace }) => `calc(100vh - ${appRibbonSpace + 56}px)`,
		overflowY: "auto"
	},
	menuContent: {
		marginTop: theme.spacing(1),
		height: ({ appRibbonSpace }) => `calc(100vh - ${appRibbonSpace + 116}px)`,
		overflowX: "hidden"
	},
	loader: {
		marginRight: theme.spacing(1),
		marginLeft: theme.spacing(1)
	},
	loadingContent: {
		flex: 1,
		display: "flex",
		flexDirection: "column",
		alignItems: "center",
		justifyContent: "center"
	}
}));
const WorkflowContainer = (props) => {
	const $ = c(79);
	const { playId, taskId, autoCollapseNav: t0 } = props;
	const autoCollapseNav = t0 === void 0 ? true : t0;
	const t1 = useBuildEnvironment() ? 25 : 0;
	let t2;
	if ($[0] !== props || $[1] !== t1) {
		t2 = {
			...props,
			appRibbonSpace: t1
		};
		$[0] = props;
		$[1] = t1;
		$[2] = t2;
	} else t2 = $[2];
	const classes = useStyles(t2);
	const location = useLocation();
	const updateSearchParams = useUpdateSearchParams();
	const user = useUser();
	const setLocationState = useSetLocationState();
	const { t } = useTranslation();
	let t3;
	if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
		t3 = [];
		$[3] = t3;
	} else t3 = $[3];
	const [menuItemsList, setMenuItemsList] = useState(t3);
	const [selectedNode, setSelectedNode] = useState(null);
	const [sidebarExpanded, setSidebarExpanded] = useState(true);
	const [reloadFromPage, setReloadFromPage] = useState(false);
	const [workflow, setWorkflow] = useState(false);
	const state = location.state;
	const searchParamStr = location.search;
	let t4;
	let udpPlayId;
	if ($[4] !== location.pathname || $[5] !== playId || $[6] !== searchParamStr || $[7] !== state || $[8] !== taskId) {
		const searchParams = new URLSearchParams(searchParamStr);
		let t5;
		if ($[11] !== location.pathname) {
			t5 = location.pathname.replace("/workflowtask/", "");
			$[11] = location.pathname;
			$[12] = t5;
		} else t5 = $[12];
		udpPlayId = playId || t5 || (state ? state.playId : searchParams.has("playId") === true ? searchParams.get("playId") : "");
		t4 = taskId || (state ? state.taskId : searchParams.has("taskId") === true ? searchParams.get("taskId") : "");
		$[4] = location.pathname;
		$[5] = playId;
		$[6] = searchParamStr;
		$[7] = state;
		$[8] = taskId;
		$[9] = t4;
		$[10] = udpPlayId;
	} else {
		t4 = $[9];
		udpPlayId = $[10];
	}
	const udpTaskId = t4;
	let t5;
	if ($[13] === Symbol.for("react.memo_cache_sentinel")) {
		t5 = {};
		$[13] = t5;
	} else t5 = $[13];
	const [t6, reloadWorkflow] = useAxiosGet(ConfigService.builderV1ApiUrl, `workflow/play/${udpPlayId}`, t5, !!!udpPlayId, false);
	const { data: workflowPlay, loading: workflowPlayLoading } = t6;
	let t7;
	if ($[14] === Symbol.for("react.memo_cache_sentinel")) {
		t7 = {};
		$[14] = t7;
	} else t7 = $[14];
	const [t8] = useAxiosGet(ConfigService.securityV1ApiUrl, `users?tenantId=${user?.currentTenantId || ""}`, t7, !!!user?.currentTenantId);
	const { data: users } = t8;
	let t9;
	if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
		t9 = {};
		$[15] = t9;
	} else t9 = $[15];
	const [t10] = useAxiosGet(ConfigService.tenantV1ApiUrl, `groups?tenantId=${user?.currentTenantId || ""}&productId=${ConfigService.config.UNITY_PRODUCT_ID}`, t9, !!!user?.currentTenantId);
	const { data: groups } = t10;
	const { setSubHeaderActionHasChildren, setNavigationExpanded } = useShellStore();
	let t11;
	if ($[16] !== reloadWorkflow) {
		t11 = async () => {
			setReloadFromPage(true);
			await reloadWorkflow();
			setReloadFromPage(false);
		};
		$[16] = reloadWorkflow;
		$[17] = t11;
	} else t11 = $[17];
	const handleReloadWorkflow = t11;
	let t12;
	if ($[18] !== handleReloadWorkflow || $[19] !== updateSearchParams) {
		t12 = async (node, menuIndex) => {
			if (node.isSelectable) {
				setSelectedNode(node);
				await handleReloadWorkflow();
				if (node?.properties?.playSceneId) updateSearchParams({ taskId: node.properties.playSceneId });
			}
		};
		$[18] = handleReloadWorkflow;
		$[19] = updateSearchParams;
		$[20] = t12;
	} else t12 = $[20];
	const handleSelectedNode = t12;
	const handleDropNode = _temp;
	let t13;
	if ($[21] === Symbol.for("react.memo_cache_sentinel")) {
		t13 = () => {
			setSidebarExpanded(_temp2);
		};
		$[21] = t13;
	} else t13 = $[21];
	const handleSidebarExpandClick = t13;
	let t14;
	let t15;
	if ($[22] !== workflowPlay) {
		t14 = () => {
			if (workflowPlay) setWorkflow(workflowPlay);
		};
		t15 = [workflowPlay];
		$[22] = workflowPlay;
		$[23] = t14;
		$[24] = t15;
	} else {
		t14 = $[23];
		t15 = $[24];
	}
	useEffect(t14, t15);
	let t16;
	let t17;
	if ($[25] !== setLocationState || $[26] !== t) {
		t16 = () => {
			setLocationState({ label: t("Workflow") });
		};
		t17 = [setLocationState, t];
		$[25] = setLocationState;
		$[26] = t;
		$[27] = t16;
		$[28] = t17;
	} else {
		t16 = $[27];
		t17 = $[28];
	}
	useEffect(t16, t17);
	let t18;
	let t19;
	if ($[29] !== workflow) {
		t18 = () => {
			if (workflow) setSelectedNode((prevNode) => {
				if (!!prevNode) {
					const foundNode = workflow.workflowPlayNode?.children?.find((child) => child.id === prevNode.id);
					if (!!foundNode) {
						const updatedNode = { ...prevNode };
						updatedNode.properties.state = foundNode.state;
						return updatedNode;
					} else return prevNode;
				}
			});
		};
		t19 = [workflow];
		$[29] = workflow;
		$[30] = t18;
		$[31] = t19;
	} else {
		t18 = $[30];
		t19 = $[31];
	}
	useEffect(t18, t19);
	let t20;
	let t21;
	if ($[32] !== workflow) {
		t20 = () => {
			if (workflow) {
				const list = [];
				const updatedMenu = transformWorkflowPlayTree(workflow.workflowPlayNode);
				if (updatedMenu) list.push(updatedMenu);
				setMenuItemsList(list);
			}
		};
		t21 = [workflow];
		$[32] = workflow;
		$[33] = t20;
		$[34] = t21;
	} else {
		t20 = $[33];
		t21 = $[34];
	}
	useEffect(t20, t21);
	let t22;
	let t23;
	if ($[35] !== autoCollapseNav || $[36] !== menuItemsList || $[37] !== selectedNode || $[38] !== setNavigationExpanded || $[39] !== udpTaskId) {
		t22 = () => {
			if (!selectedNode) {
				const menu = menuItemsList[0];
				if (menu) {
					if (!!udpTaskId) setSelectedNode(menu.children.find((child_0) => child_0?.properties?.playSceneId === udpTaskId) || menu.children[0] || null);
					else {
						const firstSelectableMenuItem = menu?.children?.find(_temp3);
						setSelectedNode(firstSelectableMenuItem || null);
					}
					if (autoCollapseNav) setNavigationExpanded(false);
				}
			}
		};
		t23 = [
			udpTaskId,
			menuItemsList,
			selectedNode,
			setNavigationExpanded,
			autoCollapseNav
		];
		$[35] = autoCollapseNav;
		$[36] = menuItemsList;
		$[37] = selectedNode;
		$[38] = setNavigationExpanded;
		$[39] = udpTaskId;
		$[40] = t22;
		$[41] = t23;
	} else {
		t22 = $[40];
		t23 = $[41];
	}
	useEffect(t22, t23);
	let t24;
	let t25;
	if ($[42] !== setSubHeaderActionHasChildren) {
		t24 = () => {
			setSubHeaderActionHasChildren(false);
		};
		t25 = [setSubHeaderActionHasChildren];
		$[42] = setSubHeaderActionHasChildren;
		$[43] = t24;
		$[44] = t25;
	} else {
		t24 = $[43];
		t25 = $[44];
	}
	useEffect(t24, t25);
	let t26;
	if ($[45] !== classes.nodeContainer || $[46] !== handleSelectedNode || $[47] !== menuItemsList || $[48] !== reloadFromPage || $[49] !== selectedNode?.id || $[50] !== selectedNode?.parentId || $[51] !== sidebarExpanded || $[52] !== workflowPlayLoading) {
		t26 = menuItemsList?.length > 0 && (!workflowPlayLoading || reloadFromPage) && menuItemsList.map((menuItems, index) => /* @__PURE__ */ React.createElement("div", {
			key: menuItems.id,
			className: classes.nodeContainer
		}, /* @__PURE__ */ React.createElement(WorkflowTreeMenu, {
			menuItems,
			onSelectNode: handleSelectedNode,
			onDropNode: handleDropNode,
			menuIndex: index,
			selectedNodeId: selectedNode?.id,
			selectedNodeParentId: selectedNode?.parentId,
			sidebarExpanded,
			treeViewMode: TreeViewModeEnums.Classic
		})));
		$[45] = classes.nodeContainer;
		$[46] = handleSelectedNode;
		$[47] = menuItemsList;
		$[48] = reloadFromPage;
		$[49] = selectedNode?.id;
		$[50] = selectedNode?.parentId;
		$[51] = sidebarExpanded;
		$[52] = workflowPlayLoading;
		$[53] = t26;
	} else t26 = $[53];
	let t27;
	if ($[54] !== classes.loader || $[55] !== reloadFromPage || $[56] !== workflowPlayLoading) {
		t27 = workflowPlayLoading && !reloadFromPage && /* @__PURE__ */ React.createElement("div", { className: classes.loader }, /* @__PURE__ */ React.createElement(NodeLoader, { loading: workflowPlayLoading }));
		$[54] = classes.loader;
		$[55] = reloadFromPage;
		$[56] = workflowPlayLoading;
		$[57] = t27;
	} else t27 = $[57];
	let t28;
	if ($[58] !== classes.menuContent || $[59] !== playId || $[60] !== t26 || $[61] !== t27) {
		t28 = /* @__PURE__ */ React.createElement(UtilitySideBar, {
			title: "Workflow",
			isUsingChildren: true,
			resizable: true,
			removePadding: true,
			removeTopLayoutUnit: true,
			onUtilitySidebarToggleExpandClick: handleSidebarExpandClick,
			collapsedWidth: 64
		}, /* @__PURE__ */ React.createElement("div", {
			className: classes.menuContent,
			key: playId
		}, t26, t27));
		$[58] = classes.menuContent;
		$[59] = playId;
		$[60] = t26;
		$[61] = t27;
		$[62] = t28;
	} else t28 = $[62];
	let t29;
	if ($[63] !== classes.sidebar || $[64] !== t28) {
		t29 = /* @__PURE__ */ React.createElement("div", { className: classes.sidebar }, t28);
		$[63] = classes.sidebar;
		$[64] = t28;
		$[65] = t29;
	} else t29 = $[65];
	let t30;
	if ($[66] !== groups || $[67] !== handleReloadWorkflow || $[68] !== selectedNode || $[69] !== users || $[70] !== workflow) {
		t30 = /* @__PURE__ */ React.createElement(WorkflowContent, {
			selectedNode,
			workflow,
			reloadWorkflow: handleReloadWorkflow,
			users,
			groups
		});
		$[66] = groups;
		$[67] = handleReloadWorkflow;
		$[68] = selectedNode;
		$[69] = users;
		$[70] = workflow;
		$[71] = t30;
	} else t30 = $[71];
	let t31;
	if ($[72] !== classes.content || $[73] !== t30) {
		t31 = /* @__PURE__ */ React.createElement("div", { className: classes.content }, t30);
		$[72] = classes.content;
		$[73] = t30;
		$[74] = t31;
	} else t31 = $[74];
	let t32;
	if ($[75] !== classes.root || $[76] !== t29 || $[77] !== t31) {
		t32 = /* @__PURE__ */ React.createElement(DndProvider, { backend: HTML5Backend }, /* @__PURE__ */ React.createElement("div", { className: classes.root }, t29, t31));
		$[75] = classes.root;
		$[76] = t29;
		$[77] = t31;
		$[78] = t32;
	} else t32 = $[78];
	return t32;
};
function _temp(sourceNode, targetNode, sourceNodeMenuIndex, targetNodeMenuIndex) {}
function _temp2(expanded) {
	return !expanded;
}
function _temp3(item) {
	return item.isSelectable;
}
//#endregion
export { WorkflowContainer, WorkflowContainer as default };

//# sourceMappingURL=WorkflowContainer.mjs.map