import { Field } from "../../../../utilities/form/Field.mjs";
import { AmbientTextField } from "../../../../UI/inputs/textField/AmbientTextField.mjs";
import { AmbientAutoComplete } from "../../../../UI/inputs/textField/AmbientAutoComplete.mjs";
import { useTransactionEngine } from "../../../../utilities/transactions/useTransactionEngine.mjs";
import React from "react";
import { FormControlLabel, Switch } from "@material-ui/core";
import { useTranslation } from "react-i18next";
//#region src/udp/pages/UdpTransactionsPage/components/DynamicTransactionDomainFormFields.jsx
const EditTransactionDetailsFormFields = (props) => {
	const { transaction, fieldAttributes, transactionTypeId } = props;
	const { t } = useTranslation();
	const { getHiddenFields, getRequiredFields } = useTransactionEngine(transactionTypeId);
	const fieldComponentMap = {
		string: AmbientTextField,
		datetime: AmbientTextField,
		number: AmbientTextField,
		bool: Switch,
		array: AmbientAutoComplete
	};
	const getEntityName = (optionValuesArr, entityId) => {
		return (optionValuesArr?.find((entity) => entity.id == entityId))?.name;
	};
	const numberDataTypes = [
		"int",
		"short",
		"long"
	];
	const removeTimeFormatDate = (date) => {
		if (date) {
			const parsedDate = new Date(date);
			if (parsedDate.getTime()) {
				const year = parsedDate.getFullYear();
				const month = parsedDate.getMonth() + 1;
				const day = parsedDate.getDate().toString().padStart(2, "0");
				return `${year}-${month.toString().padStart(2, "0")}-${day}`;
			}
			return parsedDate;
		}
		return date;
	};
	const addSpacingInHeaderName = (headerName) => {
		let formattedHeaderName = "";
		for (var i = 0; i < headerName.length; i++) if (i == 0) formattedHeaderName += headerName[i].toUpperCase();
		else if (i !== 0 && headerName[i].match(/[A-Z]/) && i != headerName.length - 1) formattedHeaderName += " " + headerName[i];
		else formattedHeaderName += headerName[i];
		return formattedHeaderName;
	};
	const dataTypeComponentMap = (transaction_0, attribute) => {
		const fieldName = attribute.name;
		let fieldDataType = attribute.type.toLowerCase();
		fieldDataType = numberDataTypes.includes(fieldDataType) ? "number" : fieldDataType;
		const isAsscoiationLabel = attribute.associationLabel;
		let fieldComponentProps = null;
		switch (fieldDataType) {
			case "number":
				fieldComponentProps = {
					initialValue: transaction_0?.entityBusinessObject[fieldName],
					type: "number",
					InputProps: { inputProps: { min: 0 } }
				};
				break;
			case "datetime":
				fieldComponentProps = {
					initialValue: removeTimeFormatDate(transaction_0?.entityBusinessObject[fieldName]),
					type: "date"
				};
				break;
			case "array":
				fieldComponentProps = {
					options: attribute?.options?.map((option) => option.id),
					getOptionLabelFunction: (option_0) => getEntityName(attribute?.options, option_0) || option_0,
					initialValue: transaction_0?.entityBusinessObject[fieldName]
				};
				break;
			case "bool":
				fieldComponentProps = {
					type: "checkbox",
					initialValue: false
				};
				break;
			default: fieldComponentProps = { initialValue: transaction_0?.entityBusinessObject[fieldName] };
		}
		const formattedFieldName = addSpacingInHeaderName(fieldName);
		const formLabel = isAsscoiationLabel ? formattedFieldName + " Name" : formattedFieldName;
		const isRequired = getRequiredFields(transaction_0).includes(fieldName) || attribute.required || !attribute.nullable;
		const isHidden = getHiddenFields(transaction_0).includes(fieldName) || attribute.isPrimaryKey || !attribute.extendedProperties.defaultVisible || attribute?.hidden;
		let fieldComponent = /* @__PURE__ */ React.createElement(Field, {
			component: fieldComponentMap[fieldDataType] ?? AmbientTextField,
			label: t(formLabel),
			name: fieldName,
			fullWidth: true,
			variant: "outlined",
			margin: "normal",
			disabled: attribute.isReadOnly,
			required: isRequired && fieldDataType != "bool",
			...fieldComponentProps
		});
		return /* @__PURE__ */ React.createElement(React.Fragment, null, !isHidden && (fieldDataType == "bool" ? /* @__PURE__ */ React.createElement(FormControlLabel, {
			control: fieldComponent,
			label: formLabel
		}) : fieldComponent));
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, fieldAttributes.map((attribute_0) => {
		return /* @__PURE__ */ React.createElement(React.Fragment, null, dataTypeComponentMap(transaction, attribute_0));
	}));
};
//#endregion
export { EditTransactionDetailsFormFields as default };

//# sourceMappingURL=DynamicTransactionDomainFormFields.mjs.map