import { getPathObject } from "../utilities/maintenanceEngine/CrudBrowserUtilities.mjs";
import { ConfigService } from "../configService/index.mjs";
import { NodeActionEnums } from "../enums/unitySystemEnums.mjs";
import { useUser } from "../utilities/auth/useUser.mjs";
import { ActionForm } from "./ActionForm.mjs";
import { getSuccessAction } from "../UI/SuccessAction.mjs";
import { useActionStore } from "../stores/actionStore.mjs";
import { LayoutUnit } from "../layout/LayoutUnit.mjs";
import { SideSheet } from "../UI/surfaces/SideSheet.mjs";
import { useAxiosGet } from "../utilities/useAxiosGet.mjs";
import { PageContainer } from "../page/PageContainer/PageContainer.mjs";
import { c } from "react-compiler-runtime";
import React, { 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";
import { DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
//#region src/actions/ActionSideSheet.jsx
const useStyles = makeStyles((theme) => ({ root: {} }));
const ActionSideSheet = (props) => {
	const $ = c(86);
	const { open, action, actionPanel, selectedNodeObj, executeAction, selectedEntity, handleOnClose, entityAttributes, foreignKeyData, loadingContent, fetchEntityData, skeleton, showError, crudTableEditionData, crudTableFormData, selectedNode, selectedNodeParent, refreshTree, udpFieldInfos } = props;
	const classes = useStyles();
	const [width, setWidth] = useState(600);
	const [buttonLabel, setButtonLabel] = useState("");
	const [onSubmit, setOnSubmit] = useState(_temp2);
	const [additionalOnClose, setAdditionalOnClose] = useState();
	let t0;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t0 = {};
		$[0] = t0;
	} else t0 = $[0];
	const [otherSideSheetProps, setOtherSideSheetProps] = useState(t0);
	const { enqueueSnackbar, closeSnackbar } = useSnackbar();
	let t1;
	if ($[1] !== closeSnackbar) {
		t1 = getSuccessAction(closeSnackbar);
		$[1] = closeSnackbar;
		$[2] = t1;
	} else t1 = $[2];
	const successAction = t1;
	const { t } = useTranslation();
	const user = useUser();
	const setSelectedNodeForStore = useActionStore(_temp3);
	const selectedNodeFromStore = useActionStore(_temp4);
	let t2;
	bb0: {
		if (action?.value) {
			if (selectedEntity?.unityBaseUpdateMethod?.apiMethodId === action.value) {
				t2 = selectedEntity.unityBaseUpdateMethod.pageId;
				break bb0;
			} else if (selectedEntity?.unityBaseCreateMethod?.apiMethodId === action.value) {
				t2 = selectedEntity.unityBaseCreateMethod.pageId;
				break bb0;
			}
		}
		t2 = "";
	}
	const promotedMethodPageId = t2;
	let t3;
	if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
		t3 = {};
		$[3] = t3;
	} else t3 = $[3];
	const [t4] = useAxiosGet(ConfigService.tenantV1ApiUrl, `page/${action?.parameters?.pageId || promotedMethodPageId}`, t3, !!!action?.parameters?.pageId && !!!promotedMethodPageId, false);
	const { data: page, loading: loadingPage } = t4;
	let t5;
	if ($[4] !== action || $[5] !== executeAction || $[6] !== selectedEntity || $[7] !== showError) {
		t5 = async (data) => {
			if (selectedEntity?.unityBaseCreateMethod && action?.value) if (selectedEntity.unityBaseCreateMethod.apiMethodId === action.value) return await executeAction(data, action.value);
			else showError();
		};
		$[4] = action;
		$[5] = executeAction;
		$[6] = selectedEntity;
		$[7] = showError;
		$[8] = t5;
	} else t5 = $[8];
	const onCreate = t5;
	let t6;
	if ($[9] !== action || $[10] !== executeAction || $[11] !== selectedEntity || $[12] !== showError) {
		t6 = async (data_0) => {
			if (selectedEntity?.unityBaseUpdateMethod && action?.value) if (selectedEntity.unityBaseUpdateMethod.apiMethodId === action.value) return await executeAction(data_0, action.value);
			else showError();
		};
		$[9] = action;
		$[10] = executeAction;
		$[11] = selectedEntity;
		$[12] = showError;
		$[13] = t6;
	} else t6 = $[13];
	const onUpdate = t6;
	let t7;
	if ($[14] !== enqueueSnackbar || $[15] !== fetchEntityData || $[16] !== selectedEntity?.unityBaseGetAllMethod || $[17] !== selectedNodeFromStore || $[18] !== selectedNodeObj || $[19] !== setSelectedNodeForStore) {
		t7 = async () => {
			if (selectedEntity?.unityBaseGetAllMethod) {
				const response = await fetchEntityData({ data: {} }).catch((res) => {
					enqueueSnackbar(res.message, {
						variant: "error",
						anchorOrigin: {
							vertical: "top",
							horizontal: "center"
						}
					});
				});
				if (response?.status === 200 && selectedNodeObj) {
					const updatedNode = response.data.find((d) => d.id === selectedNodeObj.id);
					const updatedSelectedNode = { ...selectedNodeFromStore };
					updatedSelectedNode.nodeObj = updatedNode;
					setSelectedNodeForStore(updatedSelectedNode);
				}
			}
		};
		$[14] = enqueueSnackbar;
		$[15] = fetchEntityData;
		$[16] = selectedEntity?.unityBaseGetAllMethod;
		$[17] = selectedNodeFromStore;
		$[18] = selectedNodeObj;
		$[19] = setSelectedNodeForStore;
		$[20] = t7;
	} else t7 = $[20];
	const handleFetchEntityData = t7;
	let t8;
	if ($[21] !== enqueueSnackbar || $[22] !== onCreate || $[23] !== selectedEntity || $[24] !== successAction || $[25] !== t) {
		t8 = async (values) => {
			if (selectedEntity && selectedEntity?.unityBaseCreateMethod) {
				const entityName = selectedEntity?.entityName;
				if (selectedEntity.unityBaseCreateMethod) {
					if ((await onCreate({ data: { requestBody: values } }).catch((res_0) => {
						enqueueSnackbar(res_0.message, {
							variant: "error",
							anchorOrigin: {
								vertical: "top",
								horizontal: "center"
							}
						});
					}))?.status === 200) enqueueSnackbar(entityName ? t(`${entityName} has been created`) : t("Entity has been created"), {
						anchorOrigin: {
							vertical: "top",
							horizontal: "center"
						},
						action: successAction
					});
				}
			} else enqueueSnackbar(t("Unable to retrieve entity methods"), {
				variant: "error",
				anchorOrigin: {
					vertical: "top",
					horizontal: "center"
				}
			});
		};
		$[21] = enqueueSnackbar;
		$[22] = onCreate;
		$[23] = selectedEntity;
		$[24] = successAction;
		$[25] = t;
		$[26] = t8;
	} else t8 = $[26];
	const handleOnCreate = t8;
	let t9;
	if ($[27] !== enqueueSnackbar || $[28] !== handleFetchEntityData || $[29] !== onUpdate || $[30] !== selectedEntity || $[31] !== successAction || $[32] !== t) {
		t9 = async (values_0, primaryKeyValues) => {
			if (selectedEntity && selectedEntity?.unityBaseUpdateMethod) {
				const entityName_0 = selectedEntity?.entityName;
				if (selectedEntity.unityBaseUpdateMethod) {
					if ((await onUpdate({ data: {
						...getPathObject(selectedEntity.unityBaseUpdateMethod.pathParameters, primaryKeyValues),
						requestBody: values_0
					} }).catch((res_1) => {
						enqueueSnackbar(res_1.message, {
							variant: "error",
							anchorOrigin: {
								vertical: "top",
								horizontal: "center"
							}
						});
					}))?.status === 200) {
						enqueueSnackbar(entityName_0 ? t(`${entityName_0} has been updated`) : t("Entity has been updated"), {
							anchorOrigin: {
								vertical: "top",
								horizontal: "center"
							},
							action: successAction
						});
						handleFetchEntityData();
					}
				}
			} else enqueueSnackbar(t("Unable to retrieve entity methods"), {
				variant: "error",
				anchorOrigin: {
					vertical: "top",
					horizontal: "center"
				}
			});
		};
		$[27] = enqueueSnackbar;
		$[28] = handleFetchEntityData;
		$[29] = onUpdate;
		$[30] = selectedEntity;
		$[31] = successAction;
		$[32] = t;
		$[33] = t9;
	} else t9 = $[33];
	const handleOnUpdate = t9;
	let t10;
	if ($[34] !== additionalOnClose || $[35] !== handleOnClose) {
		t10 = () => {
			if (additionalOnClose) additionalOnClose();
			handleOnClose();
		};
		$[34] = additionalOnClose;
		$[35] = handleOnClose;
		$[36] = t10;
	} else t10 = $[36];
	const handleOnCloseSideSheet = t10;
	let t11;
	if (page) {
		const t12 = action?.actionId;
		const t13 = page?.pageId;
		const t14 = selectedNode?.properties?.searchValue;
		const t15 = selectedNode?.nodeObj;
		const t16 = selectedNodeParent?.nodeObj;
		const t17 = selectedNode?.inquiryParams;
		let t18;
		if ($[37] !== action?.parameters?.requiredFieldMapping) {
			t18 = action?.parameters?.requiredFieldMapping || {};
			$[37] = action?.parameters?.requiredFieldMapping;
			$[38] = t18;
		} else t18 = $[38];
		let t19;
		if ($[39] !== handleOnClose || $[40] !== onCreate || $[41] !== onUpdate || $[42] !== refreshTree || $[43] !== t13 || $[44] !== t14 || $[45] !== t15 || $[46] !== t16 || $[47] !== t17 || $[48] !== t18) {
			t19 = {
				pageId: t13,
				contentId: t14,
				nodeObj: t15,
				parentNodeObj: t16,
				inquiryParams: t17,
				refreshTree,
				requiredFieldsMap: t18,
				setSideSheetWidth: setWidth,
				setSideSheetButtonLabel: setButtonLabel,
				setSideSheetOnSubmit: setOnSubmit,
				setSideSheetAdditionalOnClose: setAdditionalOnClose,
				setSideSheetProps: setOtherSideSheetProps,
				closeSideSheet: handleOnClose,
				onCreate,
				onUpdate
			};
			$[39] = handleOnClose;
			$[40] = onCreate;
			$[41] = onUpdate;
			$[42] = refreshTree;
			$[43] = t13;
			$[44] = t14;
			$[45] = t15;
			$[46] = t16;
			$[47] = t17;
			$[48] = t18;
			$[49] = t19;
		} else t19 = $[49];
		let t20;
		if ($[50] !== page || $[51] !== t12 || $[52] !== t19 || $[53] !== user) {
			t20 = /* @__PURE__ */ React.createElement(DndProvider, { backend: HTML5Backend }, /* @__PURE__ */ React.createElement(PageContainer, {
				user,
				key: t12,
				pageData: page,
				widgetParams: t19,
				disableMargins: true
			}));
			$[50] = page;
			$[51] = t12;
			$[52] = t19;
			$[53] = user;
			$[54] = t20;
		} else t20 = $[54];
		t11 = t20;
	} else {
		const t12 = action?.actionId;
		let t13;
		if ($[55] !== crudTableEditionData || $[56] !== crudTableFormData || $[57] !== entityAttributes || $[58] !== foreignKeyData || $[59] !== handleOnClose || $[60] !== handleOnCreate || $[61] !== handleOnUpdate || $[62] !== t12 || $[63] !== udpFieldInfos) {
			t13 = /* @__PURE__ */ React.createElement(ActionForm, {
				key: t12,
				entityAttributes,
				foreignKeyData,
				onCreate: handleOnCreate,
				closeSideSheet: handleOnClose,
				onUpdate: handleOnUpdate,
				crudTableEditionData,
				crudTableFormData,
				udpFieldInfos
			});
			$[55] = crudTableEditionData;
			$[56] = crudTableFormData;
			$[57] = entityAttributes;
			$[58] = foreignKeyData;
			$[59] = handleOnClose;
			$[60] = handleOnCreate;
			$[61] = handleOnUpdate;
			$[62] = t12;
			$[63] = udpFieldInfos;
			$[64] = t13;
		} else t13 = $[64];
		t11 = t13;
	}
	const content = t11;
	let t12;
	if (action?.parameters?.pageDisplayName) t12 = action.parameters.pageDisplayName;
	else if (page) t12 = page?.name;
	else t12 = (action?.actionTypeId === NodeActionEnums.Create ? "Add new " : "Edit ") + (action?.parameters?.entityName ?? actionPanel?.entityName);
	const sideSheetTitle = t12;
	let t13;
	if ($[65] !== sideSheetTitle || $[66] !== t) {
		t13 = t(sideSheetTitle);
		$[65] = sideSheetTitle;
		$[66] = t;
		$[67] = t13;
	} else t13 = $[67];
	let t14;
	if ($[68] !== buttonLabel || $[69] !== t) {
		t14 = t(buttonLabel);
		$[68] = buttonLabel;
		$[69] = t;
		$[70] = t14;
	} else t14 = $[70];
	let t15;
	if ($[71] !== classes || $[72] !== content || $[73] !== loadingContent || $[74] !== loadingPage || $[75] !== skeleton) {
		t15 = loadingContent || loadingPage ? /* @__PURE__ */ React.createElement(React.Fragment, null, skeleton.map(_temp5), /* @__PURE__ */ React.createElement(LayoutUnit, { unit: "md" }), /* @__PURE__ */ React.createElement(Skeleton, {
			variant: "rect",
			className: classes.skeletonButton
		})) : content;
		$[71] = classes;
		$[72] = content;
		$[73] = loadingContent;
		$[74] = loadingPage;
		$[75] = skeleton;
		$[76] = t15;
	} else t15 = $[76];
	let t16;
	if ($[77] !== handleOnCloseSideSheet || $[78] !== onSubmit || $[79] !== open || $[80] !== otherSideSheetProps || $[81] !== t13 || $[82] !== t14 || $[83] !== t15 || $[84] !== width) {
		t16 = /* @__PURE__ */ React.createElement(SideSheet, {
			open,
			title: t13,
			buttonLabel: t14,
			onSubmit,
			onClose: handleOnCloseSideSheet,
			width,
			...otherSideSheetProps
		}, t15);
		$[77] = handleOnCloseSideSheet;
		$[78] = onSubmit;
		$[79] = open;
		$[80] = otherSideSheetProps;
		$[81] = t13;
		$[82] = t14;
		$[83] = t15;
		$[84] = width;
		$[85] = t16;
	} else t16 = $[85];
	return t16;
};
ActionSideSheet.propTypes = {
	user: PropTypes.object,
	open: PropTypes.bool,
	dispatch: PropTypes.func,
	action: PropTypes.object,
	actionPanels: PropTypes.array,
	catalog: PropTypes.object,
	selectedNodeObj: PropTypes.object
};
function _temp() {
	return console.warn("onSubmit not set");
}
function _temp2() {
	return _temp;
}
function _temp3(state) {
	return state.setSelectedNode;
}
function _temp4(state_0) {
	return state_0.selectedNode;
}
function _temp5(sk, index) {
	return /* @__PURE__ */ React.createElement(React.Fragment, { key: index }, sk);
}
//#endregion
export { ActionSideSheet };

//# sourceMappingURL=ActionSideSheet.mjs.map