import { ConfigService } from "../../configService/index.mjs";
import { useUser } from "../../utilities/auth/useUser.mjs";
import { useAxiosGet } from "../../utilities/useAxiosGet.mjs";
import Page from "../Page.mjs";
import { PrimaryActionHeader } from "../../UI/utilityDisplay/utilityHeader/PrimaryActionHeader.mjs";
import { PageContextProvider } from "../../contexts/PageContext.mjs";
import { useSetLocationState } from "../../hooks/useSetLocationState.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { Card, CircularProgress, makeStyles } from "@material-ui/core";
import { Redirect } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { v4 } from "uuid";
import { Alert } from "@material-ui/lab";
import { DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
//#region src/page/PageContainer/PageContainer.jsx
const useStyles = makeStyles((theme) => ({
	root: {
		marginTop: (props) => props.disableMargins ? 0 : theme.spacing(3),
		marginRight: (props) => props.disableMargins ? 0 : theme.spacing(3),
		marginLeft: (props) => props.disableMargins ? 0 : theme.spacing(3),
		height: (props) => props.height ?? "100%",
		overflow: (props) => props.height && "auto"
	},
	staticRoot: {
		marginTop: 0,
		marginRight: 0,
		marginLeft: 0,
		height: (props) => props.height ?? "100%",
		overflow: (props) => props.height && "auto"
	},
	title: {
		flex: 1,
		marginBottom: theme.spacing(1)
	},
	icon: { marginTop: theme.spacing(-2.5) },
	loadingContent: {
		flex: 1,
		display: "flex",
		flexDirection: "column",
		alignItems: "center",
		justifyContent: "center",
		height: (props) => props.loadingHeight ? props.loadingHeight : "calc(100vh - 160px)"
	}
}));
const PageContainer = (props) => {
	const $ = c(72);
	const { widgetParams, redirectSlug, pageData, pageLoading: t0, disableMargin: t1, location, fromMenu: t2, loadingHeight, isHomePage: t3, pageId: id, updateLocationState: t4, autoHeight } = props;
	const pageLoading = t0 === void 0 ? false : t0;
	const disableMargin = t1 === void 0 ? true : t1;
	const fromMenu = t2 === void 0 ? false : t2;
	const isHomePage = t3 === void 0 ? false : t3;
	const updateLocationState = t4 === void 0 ? true : t4;
	const user = useUser();
	const classes = useStyles(props);
	const [page, setPage] = useState(null);
	let t5;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t5 = [];
		$[0] = t5;
	} else t5 = $[0];
	const [alertWarnings, setAlertWarnings] = useState(t5);
	const params = location?.state?.params;
	let t6;
	if ($[1] !== location?.pathname) {
		t6 = location?.pathname?.replace("/page/", "");
		$[1] = location?.pathname;
		$[2] = t6;
	} else t6 = $[2];
	const pathPageId = t6;
	const pageId = (pathPageId === "/" ? null : pathPageId) || params?.PageId || id;
	const { t } = useTranslation();
	const setLocationState = useSetLocationState();
	let t7;
	if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
		t7 = {};
		$[3] = t7;
	} else t7 = $[3];
	const [t8] = useAxiosGet(ConfigService.tenantV1ApiUrl, `page/${pageId}`, t7, !!!pageId, false);
	const { data: pageInfo, loading: pageInfoLoading } = t8;
	let t9;
	if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
		t9 = {};
		$[4] = t9;
	} else t9 = $[4];
	const [t10] = useAxiosGet(ConfigService.tenantV1ApiUrl, `page/homePage?tenantId=${user?.currentTenantId}&productId=${ConfigService.config.UNITY_PRODUCT_ID}`, t9, !isHomePage, false);
	const { data: homePage } = t10;
	let t11;
	if ($[5] === Symbol.for("react.memo_cache_sentinel")) {
		t11 = (id_0) => {
			setAlertWarnings((prevWarnings) => prevWarnings.filter((warning) => warning.id !== id_0));
		};
		$[5] = t11;
	} else t11 = $[5];
	const removeWarning = t11;
	let t12;
	if ($[6] !== t) {
		t12 = (t13, widgetParams_0) => {
			const fieldsStr = (t13 === void 0 ? "" : t13) || "";
			if (!fieldsStr.trim()) return;
			const warnings = [];
			const requiredFieldsMap = widgetParams_0?.requiredFieldsMap || {};
			const nodeObj = widgetParams_0?.nodeObj || {};
			const fields = fieldsStr.split(",").map(_temp);
			const missingRequiredFieldNames = [];
			fields.forEach((fieldName) => {
				const requiredField = requiredFieldsMap[fieldName];
				if (!requiredField?.value) missingRequiredFieldNames.push(fieldName);
				else if (requiredField.type === "entity" && (nodeObj[requiredField.value] === void 0 || nodeObj[requiredField.value] === null)) missingRequiredFieldNames.push(fieldName);
			});
			missingRequiredFieldNames.forEach((name) => {
				warnings.push({
					id: v4(),
					message: `${name} ${t("field is missing a value")}`
				});
			});
			return warnings;
		};
		$[6] = t;
		$[7] = t12;
	} else t12 = $[7];
	const getRequiredFieldWarnings = t12;
	let t13;
	if ($[8] !== autoHeight || $[9] !== disableMargin || $[10] !== page || $[11] !== pageId || $[12] !== params?.InquiryTreeId || $[13] !== params?.entityName || $[14] !== params?.pbiReportCode || $[15] !== params?.queryId || $[16] !== params?.requiredFieldMapping || $[17] !== params?.udpTagId || $[18] !== user || $[19] !== widgetParams) {
		t13 = page?.widgets && Object.keys(JSON.parse(page.widgets)).map((key) => {
			const widgetStr = JSON.parse(page.widgets)[key];
			const widget = widgetStr ? JSON.parse(widgetStr) : null;
			if (widget?.length) widget.forEach((w) => {
				if (pageId) w.params = {
					entityName: params?.entityName,
					pbiReportCode: params?.pbiReportCode,
					queryId: params?.queryId,
					inquiryTreeId: params?.InquiryTreeId,
					udpTagId: params?.udpTagId,
					requiredFieldsMap: params?.requiredFieldMapping,
					...w.params
				};
				if (widgetParams) w.params = {
					...w.params,
					...widgetParams
				};
			});
			return /* @__PURE__ */ React.createElement(DndProvider, {
				backend: HTML5Backend,
				key: pageId ? `${key}-${params?.entityName}` : key
			}, /* @__PURE__ */ React.createElement(Page, {
				user,
				gridConfig: JSON.parse(page.gridProperties)[key],
				widgetConfig: widget,
				disableMargin,
				autoHeight,
				pageTypeId: page.pageTypeId
			}));
		});
		$[8] = autoHeight;
		$[9] = disableMargin;
		$[10] = page;
		$[11] = pageId;
		$[12] = params?.InquiryTreeId;
		$[13] = params?.entityName;
		$[14] = params?.pbiReportCode;
		$[15] = params?.queryId;
		$[16] = params?.requiredFieldMapping;
		$[17] = params?.udpTagId;
		$[18] = user;
		$[19] = widgetParams;
		$[20] = t13;
	} else t13 = $[20];
	const pages = t13;
	let t14;
	let t15;
	if ($[21] !== homePage || $[22] !== pageData || $[23] !== pageId || $[24] !== pageInfo) {
		t14 = () => {
			if (pageId) {
				if (pageInfo) setPage(pageInfo);
			} else if (homePage) setPage(homePage);
			else setPage(pageData);
			setAlertWarnings([]);
		};
		t15 = [
			pageId,
			pageInfo,
			pageData,
			homePage
		];
		$[21] = homePage;
		$[22] = pageData;
		$[23] = pageId;
		$[24] = pageInfo;
		$[25] = t14;
		$[26] = t15;
	} else {
		t14 = $[25];
		t15 = $[26];
	}
	useEffect(t14, t15);
	let t16;
	if ($[27] !== fromMenu || $[28] !== getRequiredFieldWarnings || $[29] !== page || $[30] !== pageId || $[31] !== params?.InquiryTreeId || $[32] !== params?.entityName || $[33] !== params?.pbiReportCode || $[34] !== params?.queryId || $[35] !== params?.requiredFieldMapping || $[36] !== params?.udpTagId || $[37] !== widgetParams) {
		t16 = () => {
			if (page?.widgets) {
				let warnings_0 = [];
				Object.keys(JSON.parse(page.widgets)).forEach((key_0) => {
					const widgetStr_0 = JSON.parse(page.widgets)[key_0];
					const widget_0 = widgetStr_0 ? JSON.parse(widgetStr_0) : null;
					if (widget_0?.length) widget_0.forEach((w_0) => {
						if (pageId) w_0.params = {
							entityName: params?.entityName,
							pbiReportCode: params?.pbiReportCode,
							queryId: params?.queryId,
							inquiryTreeId: params?.InquiryTreeId,
							udpTagId: params?.udpTagId,
							requiredFieldsMap: params?.requiredFieldMapping,
							...w_0.params
						};
						if (widgetParams) w_0.params = {
							...w_0.params,
							...widgetParams
						};
						if (fromMenu || page?.pageId === widgetParams?.pageId) warnings_0 = getRequiredFieldWarnings(w_0.requiredParameters, w_0.params);
					});
				});
				setAlertWarnings(warnings_0);
			}
		};
		$[27] = fromMenu;
		$[28] = getRequiredFieldWarnings;
		$[29] = page;
		$[30] = pageId;
		$[31] = params?.InquiryTreeId;
		$[32] = params?.entityName;
		$[33] = params?.pbiReportCode;
		$[34] = params?.queryId;
		$[35] = params?.requiredFieldMapping;
		$[36] = params?.udpTagId;
		$[37] = widgetParams;
		$[38] = t16;
	} else t16 = $[38];
	let t17;
	if ($[39] !== fromMenu || $[40] !== getRequiredFieldWarnings || $[41] !== page || $[42] !== pageId || $[43] !== params || $[44] !== widgetParams) {
		t17 = [
			page,
			widgetParams,
			pageId,
			params,
			fromMenu,
			getRequiredFieldWarnings
		];
		$[39] = fromMenu;
		$[40] = getRequiredFieldWarnings;
		$[41] = page;
		$[42] = pageId;
		$[43] = params;
		$[44] = widgetParams;
		$[45] = t17;
	} else t17 = $[45];
	useEffect(t16, t17);
	let t18;
	let t19;
	if ($[46] !== page || $[47] !== setLocationState || $[48] !== t || $[49] !== updateLocationState) {
		t18 = () => {
			if (page && updateLocationState) setLocationState({ label: t(page?.name) });
		};
		t19 = [
			setLocationState,
			t,
			page,
			updateLocationState
		];
		$[46] = page;
		$[47] = setLocationState;
		$[48] = t;
		$[49] = updateLocationState;
		$[50] = t18;
		$[51] = t19;
	} else {
		t18 = $[50];
		t19 = $[51];
	}
	useEffect(t18, t19);
	if (redirectSlug && !pageData) {
		let t20;
		if ($[52] !== redirectSlug) {
			t20 = /* @__PURE__ */ React.createElement(Redirect, { to: redirectSlug });
			$[52] = redirectSlug;
			$[53] = t20;
		} else t20 = $[53];
		return t20;
	}
	if (pageInfoLoading || pageLoading) {
		const t20 = loadingHeight || 64;
		let t21;
		if ($[54] !== t20) {
			t21 = /* @__PURE__ */ React.createElement(CircularProgress, { size: t20 });
			$[54] = t20;
			$[55] = t21;
		} else t21 = $[55];
		let t22;
		if ($[56] !== classes.loadingContent || $[57] !== t21) {
			t22 = /* @__PURE__ */ React.createElement("div", { className: classes.loadingContent }, t21);
			$[56] = classes.loadingContent;
			$[57] = t21;
			$[58] = t22;
		} else t22 = $[58];
		return t22;
	}
	let t20;
	if ($[59] !== page) {
		t20 = page?.showPageName && /* @__PURE__ */ React.createElement(Card, { variant: "outlined" }, /* @__PURE__ */ React.createElement(PrimaryActionHeader, {
			title: page.name,
			hidePAB: true,
			single: true
		}));
		$[59] = page;
		$[60] = t20;
	} else t20 = $[60];
	const t21 = Number(page?.pageTypeId) === 1 ? classes.staticRoot : classes.root;
	let t22;
	if ($[61] !== alertWarnings) {
		t22 = alertWarnings?.length > 0 && alertWarnings.map((warning_0) => /* @__PURE__ */ React.createElement(Alert, {
			key: warning_0.id,
			onClose: () => removeWarning(warning_0.id),
			severity: "warning"
		}, warning_0.message || ""));
		$[61] = alertWarnings;
		$[62] = t22;
	} else t22 = $[62];
	let t23;
	if ($[63] !== pages) {
		t23 = /* @__PURE__ */ React.createElement(PageContextProvider, null, pages);
		$[63] = pages;
		$[64] = t23;
	} else t23 = $[64];
	let t24;
	if ($[65] !== t21 || $[66] !== t22 || $[67] !== t23) {
		t24 = /* @__PURE__ */ React.createElement("div", { className: t21 }, t22, t23);
		$[65] = t21;
		$[66] = t22;
		$[67] = t23;
		$[68] = t24;
	} else t24 = $[68];
	let t25;
	if ($[69] !== t20 || $[70] !== t24) {
		t25 = /* @__PURE__ */ React.createElement(React.Fragment, null, t20, t24);
		$[69] = t20;
		$[70] = t24;
		$[71] = t25;
	} else t25 = $[71];
	return t25;
};
function _temp(str) {
	return str.trim();
}
//#endregion
export { PageContainer };

//# sourceMappingURL=PageContainer.mjs.map