import { Form } from "../utilities/form/Form.mjs";
import { SubmitButton } from "../utilities/form/SubmitButton.mjs";
import { FormButtons } from "../utilities/form/FormButtons.mjs";
import { getPrimaryKeyValues } from "../utilities/maintenanceEngine/CrudBrowserUtilities.mjs";
import { CardinalityTypeEnums } from "../enums/unitySystemEnums.mjs";
import { useCrudActions } from "../hooks/crudActions.mjs";
import { c } from "react-compiler-runtime";
import React from "react";
import { Typography, makeStyles } from "@material-ui/core";
import PropTypes from "prop-types";
//#region src/actions/ActionForm.jsx
const useStyles = makeStyles((theme) => ({
	root: { marginTop: theme.spacing(-2) },
	body: { marginTop: theme.spacing(2) },
	actions: {
		marginTop: theme.spacing(2),
		marginBottom: theme.spacing(2),
		display: "flex",
		flexDirection: "row"
	},
	title: { flexGrow: 1 },
	divider: { marginTop: theme.spacing(2) },
	subject: {
		marginLeft: theme.spacing(1),
		display: "flex",
		flexDirection: "row",
		flexGrow: 1
	},
	switchRoot: { marginLeft: theme.spacing(1) },
	input: {
		height: 34,
		padding: 0
	},
	formTextInput: { fontSize: 13 },
	labelRoot: { fontSize: 13 }
}));
const ActionForm = (props) => {
	const $ = c(30);
	const { entityAttributes, foreignKeyData, onCreate, closeSideSheet, onUpdate, crudTableEditionData, crudTableFormData, udpFieldInfos } = props;
	const classes = useStyles();
	const { renderInputByFieldType } = useCrudActions();
	let t0;
	if ($[0] !== crudTableEditionData || $[1] !== entityAttributes) {
		t0 = getPrimaryKeyValues(entityAttributes, crudTableEditionData);
		$[0] = crudTableEditionData;
		$[1] = entityAttributes;
		$[2] = t0;
	} else t0 = $[2];
	const primaryKeyValues = t0;
	let t1;
	bb0: {
		if (!entityAttributes?.length) {
			let t2;
			if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
				t2 = [];
				$[3] = t2;
			} else t2 = $[3];
			t1 = t2;
			break bb0;
		}
		let mtmPropertyList;
		if ($[4] !== entityAttributes) {
			mtmPropertyList = /* @__PURE__ */ new Set();
			entityAttributes.forEach((attr) => {
				if (attr?.extendedProperties?.childEntity) Object.keys(attr.extendedProperties.childEntityAssoc || {}).length && attr.extendedProperties.childEntityAssoc.Cardinality === CardinalityTypeEnums.Many_To_Many && attr.extendedProperties.childEntityAssoc.Keys.forEach((value, i) => {
					const name = value.split(",")[0];
					if (name) mtmPropertyList.add(name.toLowerCase());
				});
			});
			$[4] = entityAttributes;
			$[5] = mtmPropertyList;
		} else mtmPropertyList = $[5];
		t1 = mtmPropertyList;
	}
	const manyToManyPropertyList = t1;
	let t2;
	if ($[6] !== closeSideSheet || $[7] !== crudTableEditionData || $[8] !== crudTableFormData || $[9] !== entityAttributes || $[10] !== foreignKeyData || $[11] !== onCreate || $[12] !== onUpdate || $[13] !== primaryKeyValues) {
		t2 = async (values) => {
			const newTableData = {
				...crudTableFormData,
				...values
			};
			entityAttributes.filter(_temp).forEach((ea_0) => {
				delete newTableData[ea_0.name];
			});
			foreignKeyData.forEach((fkd) => {
				Object.keys(newTableData).forEach((key) => {
					if (key === fkd.fieldName) newTableData[key] = fkd.data.find((d) => d.id === newTableData[key].id)?.id;
				});
			});
			if (!crudTableEditionData) onCreate && await onCreate(newTableData);
			else onUpdate && await onUpdate(newTableData, primaryKeyValues);
			closeSideSheet && closeSideSheet();
		};
		$[6] = closeSideSheet;
		$[7] = crudTableEditionData;
		$[8] = crudTableFormData;
		$[9] = entityAttributes;
		$[10] = foreignKeyData;
		$[11] = onCreate;
		$[12] = onUpdate;
		$[13] = primaryKeyValues;
		$[14] = t2;
	} else t2 = $[14];
	const handleSubmit = t2;
	const isNewEntry = !crudTableEditionData;
	let t3;
	if ($[15] !== classes || $[16] !== crudTableFormData || $[17] !== entityAttributes || $[18] !== foreignKeyData || $[19] !== isNewEntry || $[20] !== manyToManyPropertyList || $[21] !== renderInputByFieldType || $[22] !== udpFieldInfos) {
		t3 = entityAttributes?.map((schemaAttribute, index) => {
			const fieldInfo = udpFieldInfos?.find((fi) => fi?.name.toLowerCase() === schemaAttribute?.name?.toLowerCase());
			if (fieldInfo?.isPrimaryKey && !isNewEntry) schemaAttribute.disabled = true;
			if (fieldInfo?.isPrimaryKey && isNewEntry && !fieldInfo?.isIdentity) schemaAttribute.disabled = false;
			return !schemaAttribute?.extendedProperties?.identifier && /* @__PURE__ */ React.createElement(React.Fragment, { key: `${schemaAttribute.name}-${index}` }, /* @__PURE__ */ React.createElement(Typography, {
				variant: "subtitle2",
				style: { marginTop: 8 }
			}, schemaAttribute.friendlyName), renderInputByFieldType(schemaAttribute, index, entityAttributes, fieldInfo, foreignKeyData, null, crudTableFormData, classes, false, null, manyToManyPropertyList));
		});
		$[15] = classes;
		$[16] = crudTableFormData;
		$[17] = entityAttributes;
		$[18] = foreignKeyData;
		$[19] = isNewEntry;
		$[20] = manyToManyPropertyList;
		$[21] = renderInputByFieldType;
		$[22] = udpFieldInfos;
		$[23] = t3;
	} else t3 = $[23];
	let t4;
	if ($[24] !== t3) {
		t4 = /* @__PURE__ */ React.createElement("div", null, t3);
		$[24] = t3;
		$[25] = t4;
	} else t4 = $[25];
	let t5;
	if ($[26] === Symbol.for("react.memo_cache_sentinel")) {
		t5 = /* @__PURE__ */ React.createElement(FormButtons, null, /* @__PURE__ */ React.createElement(SubmitButton, {
			variant: "contained",
			color: "secondary",
			disablePristine: false,
			disabled: false,
			style: { marginTop: 15 }
		}, "Save"));
		$[26] = t5;
	} else t5 = $[26];
	let t6;
	if ($[27] !== handleSubmit || $[28] !== t4) {
		t6 = /* @__PURE__ */ React.createElement(Form, { onSubmit: handleSubmit }, t4, t5);
		$[27] = handleSubmit;
		$[28] = t4;
		$[29] = t6;
	} else t6 = $[29];
	return t6;
};
ActionForm.propTypes = {
	entityAttributes: PropTypes.array,
	foreignKeyData: PropTypes.array,
	crudTableEditionData: PropTypes.array,
	closeSideSheet: PropTypes.func,
	onCreate: PropTypes.func,
	onUpdate: PropTypes.func
};
function _temp(ea) {
	return ea?.extendedProperties?.identifier;
}
//#endregion
export { ActionForm };

//# sourceMappingURL=ActionForm.mjs.map