import { getPathObject, getPrimaryKeyValues } from "../utilities/maintenanceEngine/CrudBrowserUtilities.mjs";
import { ConfigService } from "../configService/index.mjs";
import { apiMutate } from "../utilities/useAxiosMutate.mjs";
import { toLowerFirstChar } from "../utilities/input/StringUtilities.mjs";
import { getSanitizedName } from "../utilities/entities/entityUtilities.mjs";
import { getSuccessAction } from "../UI/SuccessAction.mjs";
import { useActionStore } from "../stores/actionStore.mjs";
import { useShellStore } from "../stores/shellStore.mjs";
import { getSearchObject } from "../utilities/search/SearchUtilities.mjs";
import { ActionPanelPlacement } from "./ActionPanelPlacement.mjs";
import { LayoutUnit } from "../layout/LayoutUnit.mjs";
import { useAxiosGet } from "../utilities/useAxiosGet.mjs";
import { ActionSplitScreen } from "./ActionSplitScreen.mjs";
import { useCrudForeignKeyData } from "../hooks/crudForeignKeyData.mjs";
import { usePromotedMethodEntity } from "../hooks/usePromotedMethodEntity.mjs";
import { executeQueryAdHoc } from "../udp/utilities/methodUtils.mjs";
import { c } from "react-compiler-runtime";
import React, { Suspense, lazy, useEffect, useState } from "react";
import { makeStyles } from "@material-ui/core";
import { useSnackbar } from "notistack";
import PropTypes from "prop-types";
import { useTranslation } from "react-i18next";
import { Skeleton } from "@material-ui/lab";
//#region src/actions/ActionWrapper.jsx
const ActionModal = lazy(() => import("./ActionModal.mjs").then((m) => ({ default: m.ActionModal })));
const ActionSideSheet = lazy(() => import("./ActionSideSheet.mjs").then((m) => ({ default: m.ActionSideSheet })));
const useStyles = makeStyles((theme) => ({}));
const FIELD_NUMBER = 10;
const ActionWrapper = (props) => {
	const $ = c(108);
	const { actionState, actionPanel, dispatch, apiCatalogId, executeAction, splitScreenBody, entityAttributes, foreignKeyData, setForeignKeyData, crudTableEditionData, crudTableFormData, globalActions } = props;
	const classes = useStyles();
	const { getFKData } = useCrudForeignKeyData();
	const { enqueueSnackbar, closeSnackbar } = useSnackbar();
	let t0;
	if ($[0] !== closeSnackbar) {
		t0 = getSuccessAction(closeSnackbar);
		$[0] = closeSnackbar;
		$[1] = t0;
	} else t0 = $[1];
	const successAction = t0;
	const { t } = useTranslation();
	let t1;
	if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
		t1 = [];
		$[2] = t1;
	} else t1 = $[2];
	const [fkMethodEntities, setFKMethodEntities] = useState(t1);
	const [loadingContent, setLoadingContent] = useState(true);
	let t2;
	if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
		t2 = [];
		$[3] = t2;
	} else t2 = $[3];
	const [udpFieldInfos, setUdpFieldInfos] = useState(t2);
	const selectedNode = useActionStore(_temp);
	const currentContainerId = useShellStore(_temp2);
	const { data: udpOjectFieldInfo } = usePromotedMethodEntity("UdpObjectFieldInfo", true);
	const entityName = actionPanel ? actionPanel.entityName : actionState?.inquiryParams[currentContainerId || 0]?.selectedNode?.properties?.catalogReturnObject;
	let t3;
	if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
		t3 = {};
		$[4] = t3;
	} else t3 = $[4];
	const [t4] = useAxiosGet(ConfigService.integrationV1ApiUrl, `apicatalog/${apiCatalogId}/promotedMethodEntities/${entityName}/product/${ConfigService.config.UNITY_PRODUCT_ID}`, t3, !!!apiCatalogId || !!!entityName, false);
	const { data: selectedEntity } = t4;
	let t5;
	if ($[5] !== udpOjectFieldInfo) {
		t5 = async (entityName_0) => {
			let fieldInfos = [];
			if (udpOjectFieldInfo?.unityBaseSearchMethod?.apiMethodId) await executeQueryAdHoc({ data: getSearchObject([{
				searchField: "ObjectInfo.SanitizedName",
				searchOperator: "=",
				searchValue: getSanitizedName(entityName_0)
			}], 1, 50, true) }, udpOjectFieldInfo.unityBaseSearchMethod.apiMethodId).then((resp) => {
				fieldInfos = resp.data?.pageList;
			});
			return fieldInfos;
		};
		$[5] = udpOjectFieldInfo;
		$[6] = t5;
	} else t5 = $[6];
	const getMetaDataForUdpField = t5;
	let t6;
	if ($[7] !== executeAction || $[8] !== selectedEntity) {
		t6 = async (data) => {
			if (selectedEntity?.unityBaseGetAllMethod) return await executeAction(data, selectedEntity.unityBaseGetAllMethod.apiMethodId);
		};
		$[7] = executeAction;
		$[8] = selectedEntity;
		$[9] = t6;
	} else t6 = $[9];
	const fetchEntityData = t6;
	let t7;
	if ($[10] !== enqueueSnackbar || $[11] !== t) {
		t7 = () => {
			enqueueSnackbar(t("Invalid action value."), {
				variant: "error",
				anchorOrigin: {
					vertical: "top",
					horizontal: "center"
				}
			});
		};
		$[10] = enqueueSnackbar;
		$[11] = t;
		$[12] = t7;
	} else t7 = $[12];
	const showError = t7;
	let t8;
	if ($[13] !== apiCatalogId) {
		t8 = async (attributes) => {
			if (!attributes || !apiCatalogId) return [];
			const fkData = [];
			for (const attr of attributes) if (attr?.extendedProperties?.childEntity) {
				const ceFieldsArr = [];
				if (Object.keys(attr.extendedProperties.childEntityAssoc || {}).length && !attr.extendedProperties.childEntityAssoc.UdpType) attr.extendedProperties.childEntityAssoc.Keys.forEach((value, i) => {
					const name = value.split(",")[0];
					if (name) ceFieldsArr.push({
						idx: i,
						name: /^[A-Z]*$/.test(name) ? name.toLowerCase() : toLowerFirstChar(name)
					});
				});
				const ceAttr = attributes.find((p) => ceFieldsArr.find((cef) => cef.name === p.name && !p?.extendedProperties?.identifier));
				if (ceAttr) await apiMutate(ConfigService.integrationV1ApiUrl, `apicatalog/${apiCatalogId}/promotedMethodEntities/${attr.reference}/product/${ConfigService.config.UNITY_PRODUCT_ID}`, { method: "get" }, {}).then((resp_0) => {
					if (resp_0 && resp_0.data) fkData.push({
						fkName: ceAttr.name,
						entity: resp_0.data
					});
				});
			}
			setFKMethodEntities(fkData);
		};
		$[13] = apiCatalogId;
		$[14] = t8;
	} else t8 = $[14];
	const getFKMethodEntities = t8;
	let t9;
	if ($[15] !== dispatch) {
		t9 = (type, entity) => {
			dispatch({
				type,
				entity
			});
		};
		$[15] = dispatch;
		$[16] = t9;
	} else t9 = $[16];
	const handleOnClose = t9;
	let t10;
	if ($[17] !== actionState?.modal?.action?.value || $[18] !== executeAction || $[19] !== selectedEntity || $[20] !== showError) {
		t10 = async (data_0) => {
			if (selectedEntity?.unityBaseDeleteMethod) if (selectedEntity.unityBaseDeleteMethod.apiMethodId === actionState?.modal?.action?.value) return await executeAction(data_0, actionState?.modal?.action?.value);
			else showError();
		};
		$[17] = actionState?.modal?.action?.value;
		$[18] = executeAction;
		$[19] = selectedEntity;
		$[20] = showError;
		$[21] = t10;
	} else t10 = $[21];
	const onDelete = t10;
	let t11;
	if ($[22] !== actionState?.menuListState || $[23] !== enqueueSnackbar || $[24] !== entityAttributes || $[25] !== fetchEntityData || $[26] !== onDelete || $[27] !== selectedEntity || $[28] !== successAction || $[29] !== t) {
		t11 = async (value_0) => {
			if (selectedEntity) {
				const entityName_1 = selectedEntity?.entityName;
				const primaryKeyValues = getPrimaryKeyValues(entityAttributes, value_0);
				const pathObject = getPathObject(selectedEntity?.unityBaseDeleteMethod.pathParameters, primaryKeyValues);
				if (selectedEntity?.unityBaseDeleteMethod) {
					if ((await onDelete({ data: { ...pathObject } }).catch((res) => {
						enqueueSnackbar(res.message, {
							variant: "error",
							anchorOrigin: {
								vertical: "top",
								horizontal: "center"
							}
						});
					}))?.status === 200) {
						enqueueSnackbar(entityName_1 ? t(`${entityName_1} has been deleted`) : t("Entity has been deleted"), {
							anchorOrigin: {
								vertical: "top",
								horizontal: "center"
							},
							action: successAction
						});
						if (actionState?.menuListState && actionState?.menuListState?.menuList.length > 0) {
							const handleRemoveMenuItem = actionState?.menuListState?.handleRemoveMenuItem;
							if (handleRemoveMenuItem) handleRemoveMenuItem(actionState?.menuListState?.menuList);
						}
						fetchEntityData({ data: {} });
					}
				}
			}
		};
		$[22] = actionState?.menuListState;
		$[23] = enqueueSnackbar;
		$[24] = entityAttributes;
		$[25] = fetchEntityData;
		$[26] = onDelete;
		$[27] = selectedEntity;
		$[28] = successAction;
		$[29] = t;
		$[30] = t11;
	} else t11 = $[30];
	const handleOnDelete = t11;
	let skeletonArr;
	if ($[31] !== classes) {
		skeletonArr = [];
		for (let i_0 = 0; i_0 < FIELD_NUMBER; i_0++) skeletonArr.push(/* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Skeleton, {
			key: i_0,
			variant: "rect",
			className: classes.skeleton
		}), /* @__PURE__ */ React.createElement(LayoutUnit, { unit: "xxs" })));
		$[31] = classes;
		$[32] = skeletonArr;
	} else skeletonArr = $[32];
	const skeleton = skeletonArr;
	let t12;
	let t13;
	if ($[33] !== entityAttributes || $[34] !== getFKMethodEntities) {
		t12 = () => {
			if (entityAttributes) {
				getFKMethodEntities(entityAttributes);
				setLoadingContent(false);
			}
		};
		t13 = [entityAttributes, getFKMethodEntities];
		$[33] = entityAttributes;
		$[34] = getFKMethodEntities;
		$[35] = t12;
		$[36] = t13;
	} else {
		t12 = $[35];
		t13 = $[36];
	}
	useEffect(t12, t13);
	let t14;
	let t15;
	if ($[37] !== enqueueSnackbar || $[38] !== entityAttributes || $[39] !== fkMethodEntities || $[40] !== getFKData || $[41] !== setForeignKeyData) {
		t14 = () => {
			if (entityAttributes && fkMethodEntities && fkMethodEntities.length > 0) getFKData(fkMethodEntities, setForeignKeyData, enqueueSnackbar);
		};
		t15 = [
			enqueueSnackbar,
			entityAttributes,
			fkMethodEntities,
			getFKData,
			setForeignKeyData
		];
		$[37] = enqueueSnackbar;
		$[38] = entityAttributes;
		$[39] = fkMethodEntities;
		$[40] = getFKData;
		$[41] = setForeignKeyData;
		$[42] = t14;
		$[43] = t15;
	} else {
		t14 = $[42];
		t15 = $[43];
	}
	useEffect(t14, t15);
	let t16;
	if ($[44] !== entityName || $[45] !== getMetaDataForUdpField || $[46] !== udpOjectFieldInfo?.unityBaseSearchMethod?.apiMethodId) {
		t16 = () => {
			const setFieldInfos = async () => {
				if (!entityName) return;
				setUdpFieldInfos(await getMetaDataForUdpField(entityName));
			};
			if (udpOjectFieldInfo?.unityBaseSearchMethod?.apiMethodId) setFieldInfos();
		};
		$[44] = entityName;
		$[45] = getMetaDataForUdpField;
		$[46] = udpOjectFieldInfo?.unityBaseSearchMethod?.apiMethodId;
		$[47] = t16;
	} else t16 = $[47];
	let t17;
	if ($[48] !== entityName || $[49] !== getMetaDataForUdpField || $[50] !== udpOjectFieldInfo) {
		t17 = [
			getMetaDataForUdpField,
			udpOjectFieldInfo,
			entityName
		];
		$[48] = entityName;
		$[49] = getMetaDataForUdpField;
		$[50] = udpOjectFieldInfo;
		$[51] = t17;
	} else t17 = $[51];
	useEffect(t16, t17);
	const t18 = actionState?.actionPanels[currentContainerId || 0];
	const t19 = actionState?.showInquiryHeaderActions;
	const t20 = actionState?.inquiryParams[currentContainerId || 0]?.selectedNode;
	let t21;
	if ($[52] !== globalActions || $[53] !== t18 || $[54] !== t19 || $[55] !== t20) {
		t21 = /* @__PURE__ */ React.createElement(ActionPanelPlacement, {
			actionPanels: t18,
			showInquiryHeaderActions: t19,
			selectedNode: t20,
			globalActions
		});
		$[52] = globalActions;
		$[53] = t18;
		$[54] = t19;
		$[55] = t20;
		$[56] = t21;
	} else t21 = $[56];
	let t22;
	if ($[57] !== splitScreenBody || $[58] !== t21) {
		t22 = /* @__PURE__ */ React.createElement(React.Fragment, null, splitScreenBody, t21);
		$[57] = splitScreenBody;
		$[58] = t21;
		$[59] = t22;
	} else t22 = $[59];
	const t23 = actionState?.splitScreen?.open;
	const t24 = actionState?.splitScreen?.action;
	const t25 = actionState?.splitScreen?.type;
	const t26 = actionState?.splitScreen?.params;
	const t27 = selectedNode?.nodeObj;
	const t28 = actionState?.menuListState?.menuList;
	const t29 = actionState?.inquiryParams[currentContainerId || 0]?.selectedNode;
	const t30 = actionState?.inquiryParams[currentContainerId || 0]?.selectedNodeParent;
	const t31 = actionState?.inquiryParams[currentContainerId || 0]?.refreshTree;
	let t32;
	if ($[60] !== actionPanel || $[61] !== executeAction || $[62] !== selectedEntity || $[63] !== t22 || $[64] !== t23 || $[65] !== t24 || $[66] !== t25 || $[67] !== t26 || $[68] !== t27 || $[69] !== t28 || $[70] !== t29 || $[71] !== t30 || $[72] !== t31) {
		t32 = /* @__PURE__ */ React.createElement(ActionSplitScreen, {
			body: t22,
			open: t23,
			action: t24,
			type: t25,
			params: t26,
			executeAction,
			actionPanel,
			selectedNodeObj: t27,
			menuNodeList: t28,
			sidebarEntity: selectedEntity,
			selectedNode: t29,
			selectedNodeParent: t30,
			refreshTree: t31
		});
		$[60] = actionPanel;
		$[61] = executeAction;
		$[62] = selectedEntity;
		$[63] = t22;
		$[64] = t23;
		$[65] = t24;
		$[66] = t25;
		$[67] = t26;
		$[68] = t27;
		$[69] = t28;
		$[70] = t29;
		$[71] = t30;
		$[72] = t31;
		$[73] = t32;
	} else t32 = $[73];
	let t33;
	if ($[74] !== actionPanel || $[75] !== actionState?.inquiryParams || $[76] !== actionState?.sideSheet?.action || $[77] !== actionState?.sideSheet?.open || $[78] !== crudTableEditionData || $[79] !== crudTableFormData || $[80] !== currentContainerId || $[81] !== dispatch || $[82] !== entityAttributes || $[83] !== executeAction || $[84] !== fetchEntityData || $[85] !== foreignKeyData || $[86] !== handleOnClose || $[87] !== loadingContent || $[88] !== selectedEntity || $[89] !== selectedNode?.nodeObj || $[90] !== showError || $[91] !== skeleton || $[92] !== udpFieldInfos) {
		t33 = actionState?.sideSheet?.open && /* @__PURE__ */ React.createElement(Suspense, { fallback: null }, /* @__PURE__ */ React.createElement(ActionSideSheet, {
			key: actionState?.sideSheet?.action?.actionId,
			open: actionState?.sideSheet?.open,
			dispatch,
			action: actionState?.sideSheet?.action,
			actionPanel,
			selectedNodeObj: selectedNode?.nodeObj,
			executeAction,
			selectedEntity,
			handleOnClose: () => {
				handleOnClose("OPEN_SIDESHEET", {
					open: false,
					action: null
				});
			},
			entityAttributes,
			foreignKeyData,
			loadingContent,
			fetchEntityData,
			skeleton,
			showError,
			crudTableEditionData,
			crudTableFormData,
			selectedNode: actionState?.inquiryParams[currentContainerId || 0]?.selectedNode,
			selectedNodeParent: actionState?.inquiryParams[currentContainerId || 0]?.selectedNodeParent,
			refreshTree: actionState?.inquiryParams[currentContainerId || 0]?.refreshTree,
			udpFieldInfos
		}));
		$[74] = actionPanel;
		$[75] = actionState?.inquiryParams;
		$[76] = actionState?.sideSheet?.action;
		$[77] = actionState?.sideSheet?.open;
		$[78] = crudTableEditionData;
		$[79] = crudTableFormData;
		$[80] = currentContainerId;
		$[81] = dispatch;
		$[82] = entityAttributes;
		$[83] = executeAction;
		$[84] = fetchEntityData;
		$[85] = foreignKeyData;
		$[86] = handleOnClose;
		$[87] = loadingContent;
		$[88] = selectedEntity;
		$[89] = selectedNode?.nodeObj;
		$[90] = showError;
		$[91] = skeleton;
		$[92] = udpFieldInfos;
		$[93] = t33;
	} else t33 = $[93];
	let t34;
	if ($[94] !== actionState?.modal?.action || $[95] !== actionState?.modal?.open || $[96] !== actionState?.modal?.title || $[97] !== handleOnClose || $[98] !== handleOnDelete || $[99] !== selectedEntity?.entityName || $[100] !== selectedNode?.nodeObj || $[101] !== showError || $[102] !== t) {
		t34 = actionState?.modal?.open && /* @__PURE__ */ React.createElement(Suspense, { fallback: null }, /* @__PURE__ */ React.createElement(ActionModal, {
			open: actionState?.modal?.open,
			action: actionState?.modal?.action,
			title: actionState?.modal?.title + selectedEntity?.entityName,
			message: t(`Are you sure you want to delete this ${selectedEntity?.entityName}?`),
			confirmAction: () => handleOnDelete(selectedNode?.nodeObj),
			handleOnClose: () => {
				handleOnClose("OPEN_MODAL", {
					open: false,
					action: null
				});
			},
			showError
		}));
		$[94] = actionState?.modal?.action;
		$[95] = actionState?.modal?.open;
		$[96] = actionState?.modal?.title;
		$[97] = handleOnClose;
		$[98] = handleOnDelete;
		$[99] = selectedEntity?.entityName;
		$[100] = selectedNode?.nodeObj;
		$[101] = showError;
		$[102] = t;
		$[103] = t34;
	} else t34 = $[103];
	let t35;
	if ($[104] !== t32 || $[105] !== t33 || $[106] !== t34) {
		t35 = /* @__PURE__ */ React.createElement(React.Fragment, null, t32, t33, t34);
		$[104] = t32;
		$[105] = t33;
		$[106] = t34;
		$[107] = t35;
	} else t35 = $[107];
	return t35;
};
ActionWrapper.propTypes = {
	actionState: PropTypes.object,
	actionPanel: PropTypes.object,
	dispatch: PropTypes.func,
	catalog: PropTypes.object,
	executeAction: PropTypes.func,
	user: PropTypes.object,
	splitScreenBody: PropTypes.object
};
function _temp(state) {
	return state.selectedNode;
}
function _temp2(state_0) {
	return state_0.currentContainerId;
}
//#endregion
export { ActionWrapper };

//# sourceMappingURL=ActionWrapper.mjs.map