import { Form } from "../../../../utilities/form/Form.mjs";
import { Field } from "../../../../utilities/form/Field.mjs";
import { FluentButton } from "../../../../UI/inputs/buttons/FluentButton.mjs";
import { arrayMutators } from "../../../../utilities/form/arrayMutators.mjs";
import { EMPTY_GUID } from "../../../../enums/unitySystemEnums.mjs";
import { LayoutUnit } from "../../../../layout/LayoutUnit.mjs";
import { FluentTextField } from "../../../../UI/inputs/textField/FluentTextField.mjs";
import { AmbientAutoComplete } from "../../../../UI/inputs/textField/AmbientAutoComplete.mjs";
import { DOMAIN_VALUE_OPERATORS } from "../../../utilities/transactionConstants.mjs";
import { c } from "react-compiler-runtime";
import React, { forwardRef, useImperativeHandle, useRef } from "react";
import { FormControlLabel, Switch, makeStyles } from "@material-ui/core";
import { StatusCircleErrorXIcon } from "@fluentui/react-icons";
import { useTranslation } from "react-i18next";
import { FieldArray } from "react-final-form-arrays";
//#region src/udp/pages/UdpTransactionTypesPage/forms/UdpTransactionTypeConfigurationForm.jsx
const useStyles = makeStyles((theme) => ({
	domainFields: {
		display: "flex",
		flexDirection: "row",
		alignItems: "flex-end",
		justifyContent: "flex-start",
		"& > *": { marginRight: theme.spacing(2) }
	},
	domainValueField: { minWidth: "200px" },
	addRemoveButtons: {
		display: "flex",
		flexDirection: "row",
		alignItems: "center",
		justifyContent: "flex-start",
		margin: `${theme.spacing(2)}px  0  0 ${-theme.spacing(2)}px`,
		"& > *": { margin: `0 ${theme.spacing(2)}px` }
	},
	removeIcon: {
		fontSize: theme.spacing(3.5),
		marginTop: theme.spacing(1)
	},
	noRemoveIcon: { marginLeft: theme.spacing(3.5) }
}));
const noStatus = {
	name: "No Status",
	transactionTypeStatusId: EMPTY_GUID
};
const UdpTransactionTypeConfigurationForm = (props, ref) => {
	const $ = c(40);
	const { onSubmit, initialData, transactionRuleTypes, catalogObjectProperties, transactionTypeStatusOptions, roles } = props;
	const handleSubmitRef = useRef();
	const { t } = useTranslation();
	const classes = useStyles();
	let t0;
	bb0: {
		let ttstatuses;
		if (!transactionTypeStatusOptions) {
			t0 = void 0;
			break bb0;
		}
		let t1;
		if ($[0] !== transactionTypeStatusOptions) {
			t1 = [...transactionTypeStatusOptions, noStatus];
			$[0] = transactionTypeStatusOptions;
			$[1] = t1;
		} else t1 = $[1];
		ttstatuses = t1;
		t0 = ttstatuses;
	}
	const ttstatusesWithNostatusOption = t0;
	let t1;
	bb1: {
		let formattedData;
		if (!initialData) {
			t1 = void 0;
			break bb1;
		}
		let t2;
		if ($[2] !== initialData?.transactionRuleTypeId || $[3] !== transactionRuleTypes) {
			t2 = transactionRuleTypes?.find((trt) => trt?.transactionRuleTypeId === initialData?.transactionRuleTypeId);
			$[2] = initialData?.transactionRuleTypeId;
			$[3] = transactionRuleTypes;
			$[4] = t2;
		} else t2 = $[4];
		let t3;
		if ($[5] !== initialData?.configuration?.currentStatusId || $[6] !== ttstatusesWithNostatusOption) {
			t3 = ttstatusesWithNostatusOption?.find((tts) => tts?.transactionTypeStatusId === initialData?.configuration?.currentStatusId);
			$[5] = initialData?.configuration?.currentStatusId;
			$[6] = ttstatusesWithNostatusOption;
			$[7] = t3;
		} else t3 = $[7];
		let t4;
		if ($[8] !== initialData?.configuration?.tenantUserRoleIds || $[9] !== roles) {
			t4 = roles?.filter((role) => initialData?.configuration?.tenantUserRoleIds?.includes(role?.roleId));
			$[8] = initialData?.configuration?.tenantUserRoleIds;
			$[9] = roles;
			$[10] = t4;
		} else t4 = $[10];
		let t5;
		if ($[11] !== initialData?.configuration?.currentDomainValue) {
			t5 = initialData?.configuration?.currentDomainValue ? Object.entries(initialData?.configuration?.currentDomainValue).map(_temp) : [[]];
			$[11] = initialData?.configuration?.currentDomainValue;
			$[12] = t5;
		} else t5 = $[12];
		let t6;
		if ($[13] !== initialData || $[14] !== t2 || $[15] !== t3 || $[16] !== t4 || $[17] !== t5) {
			t6 = {
				...initialData,
				transactionRuleType: t2,
				currentStatus: t3,
				tenantUserRoles: t4,
				currentDomainValue: t5
			};
			$[13] = initialData;
			$[14] = t2;
			$[15] = t3;
			$[16] = t4;
			$[17] = t5;
			$[18] = t6;
		} else t6 = $[18];
		formattedData = t6;
		t1 = formattedData;
	}
	const formInitialValues = t1;
	let t2;
	if ($[19] === Symbol.for("react.memo_cache_sentinel")) {
		t2 = () => ({ submit: () => {
			handleSubmitRef?.current();
		} });
		$[19] = t2;
	} else t2 = $[19];
	useImperativeHandle(ref, t2);
	let t3;
	if ($[20] !== initialData || $[21] !== onSubmit) {
		t3 = (formData) => {
			onSubmit(formData, initialData);
		};
		$[20] = initialData;
		$[21] = onSubmit;
		$[22] = t3;
	} else t3 = $[22];
	const handleSubmit = t3;
	let t4;
	if ($[23] === Symbol.for("react.memo_cache_sentinel")) {
		t4 = { ...arrayMutators };
		$[23] = t4;
	} else t4 = $[23];
	let t5;
	if ($[24] !== formInitialValues) {
		t5 = { ...formInitialValues };
		$[24] = formInitialValues;
		$[25] = t5;
	} else t5 = $[25];
	let t6;
	if ($[26] !== catalogObjectProperties || $[27] !== classes.addRemoveButtons || $[28] !== classes.domainFields || $[29] !== classes.domainValueField || $[30] !== classes.removeIcon || $[31] !== roles || $[32] !== t || $[33] !== transactionRuleTypes || $[34] !== ttstatusesWithNostatusOption) {
		t6 = (t7) => {
			const { handleSubmit: handleSubmit_0, form } = t7;
			handleSubmitRef.current = handleSubmit_0;
			const push = form?.mutators?.push ?? _temp2;
			return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Field, {
				component: FluentTextField,
				label: t("Name"),
				id: "name",
				name: "name",
				fullWidth: true,
				variant: "outlined",
				margin: "normal",
				size: "small"
			}), /* @__PURE__ */ React.createElement(Field, {
				component: AmbientAutoComplete,
				label: t("Transaction Rule Type"),
				id: "transactionRuleType",
				name: "transactionRuleType",
				variant: "outlined",
				isMultiple: false,
				disableCloseOnSelect: false,
				options: transactionRuleTypes || [],
				getOptionLabelFunction: _temp3,
				valueForChange: _temp4,
				required: true
			}), /* @__PURE__ */ React.createElement(Field, {
				component: FluentTextField,
				label: t("Order"),
				id: "order",
				name: "order",
				fullWidth: true,
				variant: "outlined",
				margin: "normal",
				size: "small",
				type: "number",
				required: true
			}), /* @__PURE__ */ React.createElement(LayoutUnit, { unit: "nudge" }), /* @__PURE__ */ React.createElement(FormControlLabel, {
				control: /* @__PURE__ */ React.createElement(Field, {
					component: Switch,
					id: "enabled",
					name: "enabled",
					type: "checkbox",
					defaultValue: true
				}),
				label: t("Enabled")
			}), /* @__PURE__ */ React.createElement(Field, {
				component: AmbientAutoComplete,
				label: t("Current Status"),
				id: "currentStatus",
				name: "currentStatus",
				variant: "outlined",
				isMultiple: false,
				disableCloseOnSelect: false,
				options: ttstatusesWithNostatusOption || [],
				getOptionLabelFunction: _temp5
			}), /* @__PURE__ */ React.createElement(Field, {
				component: AmbientAutoComplete,
				label: t("Allowed Roles"),
				id: "tenantUserRoles",
				name: "tenantUserRoles",
				variant: "outlined",
				isMultiple: true,
				disableCloseOnSelect: true,
				groupBy: _temp6,
				options: roles || [],
				getOptionLabelFunction: _temp7
			}), /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: classes.addRemoveButtons }, /* @__PURE__ */ React.createElement(FluentButton, {
				variant: "contained",
				color: "primary",
				onClick: () => push("currentDomainValue", void 0)
			}, t("Add a Domain Field"))), /* @__PURE__ */ React.createElement(FieldArray, { name: "currentDomainValue" }, (t8) => {
				const { fields } = t8;
				return fields.map((name, index) => /* @__PURE__ */ React.createElement("div", {
					key: `${name}${index}`,
					className: classes.domainFields
				}, /* @__PURE__ */ React.createElement(Field, {
					component: AmbientAutoComplete,
					label: index === 0 && t("Field"),
					name: `${name}[0]`,
					variant: "outlined",
					isMultiple: false,
					disableCloseOnSelect: false,
					options: catalogObjectProperties || [],
					getOptionLabelFunction: _temp8,
					onChange: _temp9
				}), /* @__PURE__ */ React.createElement(Field, {
					component: AmbientAutoComplete,
					label: index === 0 && t("Operator"),
					name: `${name}[1].operator`,
					variant: "outlined",
					isMultiple: false,
					disableCloseOnSelect: false,
					options: DOMAIN_VALUE_OPERATORS || [],
					getOptionLabelFunction: _temp0,
					onChange: _temp1
				}), /* @__PURE__ */ React.createElement(Field, {
					component: FluentTextField,
					label: index === 0 && t("Value"),
					name: `${name}[1].value`,
					fullWidth: true,
					variant: "outlined",
					margin: "normal",
					size: "small",
					onChange: _temp10,
					className: classes.domainValueField
				}), /* @__PURE__ */ React.createElement("span", {
					onClick: () => fields.remove(index),
					style: { cursor: "pointer" },
					className: classes.removeIcon
				}, /* @__PURE__ */ React.createElement(StatusCircleErrorXIcon, null))));
			})));
		};
		$[26] = catalogObjectProperties;
		$[27] = classes.addRemoveButtons;
		$[28] = classes.domainFields;
		$[29] = classes.domainValueField;
		$[30] = classes.removeIcon;
		$[31] = roles;
		$[32] = t;
		$[33] = transactionRuleTypes;
		$[34] = ttstatusesWithNostatusOption;
		$[35] = t6;
	} else t6 = $[35];
	let t7;
	if ($[36] !== handleSubmit || $[37] !== t5 || $[38] !== t6) {
		t7 = /* @__PURE__ */ React.createElement(Form, {
			onSubmit: handleSubmit,
			mutators: t4,
			initialValues: t5,
			render: t6
		});
		$[36] = handleSubmit;
		$[37] = t5;
		$[38] = t6;
		$[39] = t7;
	} else t7 = $[39];
	return t7;
};
var UdpTransactionTypeConfigurationForm_default = forwardRef(UdpTransactionTypeConfigurationForm);
function _temp(t0) {
	const [fieldKey, fieldConfig] = t0;
	const normalizedFieldConfig = fieldConfig && typeof fieldConfig === "object" ? fieldConfig : { value: fieldConfig };
	const operatorOption = DOMAIN_VALUE_OPERATORS.find((option) => option.value === normalizedFieldConfig?.operator) || null;
	return [fieldKey, {
		...normalizedFieldConfig,
		operator: operatorOption
	}];
}
function _temp2() {}
function _temp3(option_0) {
	return option_0.name;
}
function _temp4(value) {
	return value;
}
function _temp5(option_1) {
	return option_1.name;
}
function _temp6(option_2) {
	return option_2.productName;
}
function _temp7(option_3) {
	return option_3.name;
}
function _temp8(option_4) {
	return option_4;
}
function _temp9() {}
function _temp0(option_5) {
	return option_5.name;
}
function _temp1() {}
function _temp10() {}
//#endregion
export { UdpTransactionTypeConfigurationForm_default as default };

//# sourceMappingURL=UdpTransactionTypeConfigurationForm.mjs.map