import { TransactionActionTypeEnums, TransactionEventTypeEnums } from "../../enums/unitySystemEnums.mjs";
import { useUser } from "../auth/useUser.mjs";
import { useTransactionStore } from "../../stores/transactionStore.mjs";
import { c } from "react-compiler-runtime";
import "react";
import jsonLogic from "json-logic-js";
//#region src/utilities/transactions/useTransactionEngine.js
const useTransactionEngine = (transactionTypeId) => {
	const $ = c(54);
	const user = useUser();
	const transactionTypesMap = useTransactionStore(_temp);
	const transactionType = transactionTypesMap ? transactionTypesMap[transactionTypeId] : null;
	let t0;
	if ($[0] !== user?.roleIds) {
		t0 = (rule, udpTransaction) => {
			const udpTransactionEvaluationData = {
				currentStatusId: udpTransaction?.currentStatusId,
				currentDomainValue: udpTransaction?.entityBusinessObject,
				tenantUserRoleIds: user?.roleIds || []
			};
			return jsonLogic.apply(rule, udpTransactionEvaluationData);
		};
		$[0] = user?.roleIds;
		$[1] = t0;
	} else t0 = $[1];
	user?.roleIds;
	const evaluateRule = t0;
	let t1;
	if ($[2] !== evaluateRule || $[3] !== transactionType?.udpTransactionTypeUdpTransactionTypeConfiguration) {
		t1 = (udpTransaction_0, t2) => {
			const ruleTypeId = t2 === void 0 ? null : t2;
			return transactionType?.udpTransactionTypeUdpTransactionTypeConfiguration?.reduce((actions, config) => {
				const ruleLogic = config?.expressionLogic;
				if (!ruleTypeId) return actions;
				if (config.transactionRuleTypeId === ruleTypeId && config.enabled && ruleLogic) {
					if (evaluateRule(ruleLogic, udpTransaction_0)) {
						const nestedActions = (config?.udpTransactionTypeConfigurationUdpTransactionTypeConfigurationTransactionAction || []).map(_temp2).filter(_temp3);
						if (nestedActions.length > 0) actions = actions.concat(nestedActions);
					}
					return actions;
				}
				return actions;
			}, []);
		};
		$[2] = evaluateRule;
		$[3] = transactionType?.udpTransactionTypeUdpTransactionTypeConfiguration;
		$[4] = t1;
	} else t1 = $[4];
	const getTransactionTypeTransactionActions = t1;
	const hasRevisions = transactionType?.hasRevision;
	let t2;
	if ($[5] !== transactionType?.udpTransactionTypeUdpTransactionTypeStatus) {
		t2 = transactionType?.udpTransactionTypeUdpTransactionTypeStatus?.some(_temp4);
		$[5] = transactionType?.udpTransactionTypeUdpTransactionTypeStatus;
		$[6] = t2;
	} else t2 = $[6];
	const hasClosedStatus = t2;
	let t3;
	if ($[7] !== transactionType?.archiveOnCancel || $[8] !== transactionType?.udpTransactionTypeUdpTransactionTypeStatus) {
		t3 = transactionType?.archiveOnCancel || transactionType?.udpTransactionTypeUdpTransactionTypeStatus?.some(_temp5);
		$[7] = transactionType?.archiveOnCancel;
		$[8] = transactionType?.udpTransactionTypeUdpTransactionTypeStatus;
		$[9] = t3;
	} else t3 = $[9];
	const canCancel = t3;
	const transactionTypeStatuses = transactionType?.udpTransactionTypeUdpTransactionTypeStatus;
	let t4;
	bb0: {
		if (Array.isArray(transactionType?.domainFields)) {
			let t5;
			if ($[10] !== transactionType?.domainFields) {
				t5 = transactionType?.domainFields || [];
				$[10] = transactionType?.domainFields;
				$[11] = t5;
			} else t5 = $[11];
			t4 = t5;
			break bb0;
		} else if (transactionType?.domainFields && Object.keys(transactionType.domainFields).length) {
			let t5;
			if ($[12] !== transactionType.domainFields) {
				t5 = Object.entries(transactionType.domainFields).map(_temp6).sort(_temp7);
				$[12] = transactionType.domainFields;
				$[13] = t5;
			} else t5 = $[13];
			const orderedColumns = t5;
			let t6;
			if ($[14] !== orderedColumns) {
				t6 = orderedColumns || [];
				$[14] = orderedColumns;
				$[15] = t6;
			} else t6 = $[15];
			t4 = t6;
			break bb0;
		}
		let t5;
		if ($[16] === Symbol.for("react.memo_cache_sentinel")) {
			t5 = [];
			$[16] = t5;
		} else t5 = $[16];
		t4 = t5;
	}
	const transactionTypeGridColumns = t4;
	const transactionTypeColumnDefProperties = transactionType?.pageConfiguration?.columnDefProperties;
	let t5;
	if ($[17] !== transactionType?.pageConfiguration) {
		t5 = transactionType?.pageConfiguration || {};
		$[17] = transactionType?.pageConfiguration;
		$[18] = t5;
	} else t5 = $[18];
	let other;
	if ($[19] !== t5) {
		const { columnDefProperties, ...t6 } = t5;
		other = t6;
		$[19] = t5;
		$[20] = other;
	} else other = $[20];
	const transactionTypePageConfiguration = other;
	let statusColorsMap;
	if ($[21] !== transactionType?.udpTransactionTypeUdpTransactionTypeStatus) {
		statusColorsMap = {};
		transactionType?.udpTransactionTypeUdpTransactionTypeStatus?.forEach((status_1) => {
			statusColorsMap[status_1.name] = status_1.color || "default";
		});
		$[21] = transactionType?.udpTransactionTypeUdpTransactionTypeStatus;
		$[22] = statusColorsMap;
	} else statusColorsMap = $[22];
	const transactionTypeStatusColorsMap = statusColorsMap;
	let t6;
	if ($[23] !== getTransactionTypeTransactionActions) {
		t6 = (udpTransaction_1) => {
			const transactionActions_0 = getTransactionTypeTransactionActions(udpTransaction_1, TransactionEventTypeEnums.Form);
			if (!transactionActions_0) return [];
			let hiddenFields = [];
			transactionActions_0.filter(_temp8).forEach((action_2) => {
				if (action_2.parameters?.hiddenFields) hiddenFields = hiddenFields.concat(action_2.parameters.hiddenFields);
			});
			return hiddenFields;
		};
		$[23] = getTransactionTypeTransactionActions;
		$[24] = t6;
	} else t6 = $[24];
	const getHiddenFields = t6;
	let t7;
	if ($[25] !== getTransactionTypeTransactionActions) {
		t7 = (udpTransaction_2) => {
			const transactionActions_1 = getTransactionTypeTransactionActions(udpTransaction_2, TransactionEventTypeEnums.Form);
			if (!transactionActions_1) return [];
			let requiredFields = [];
			transactionActions_1.filter(_temp9).forEach((action_4) => {
				if (action_4.parameters?.mandatoryFields) requiredFields = requiredFields.concat(action_4.parameters.mandatoryFields);
			});
			return requiredFields;
		};
		$[25] = getTransactionTypeTransactionActions;
		$[26] = t7;
	} else t7 = $[26];
	const getRequiredFields = t7;
	let t8;
	if ($[27] !== transactionType?.udpTransactionTypeUdpTransactionUiAction) {
		t8 = (uiActionType) => {
			return (transactionType?.udpTransactionTypeUdpTransactionUiAction?.find((action_5) => action_5.transactionUiActionTypeId === uiActionType))?.parameters || null;
		};
		$[27] = transactionType?.udpTransactionTypeUdpTransactionUiAction;
		$[28] = t8;
	} else t8 = $[28];
	const getUiActionRenderParameters = t8;
	const formatLabel = _temp1;
	let t9;
	if ($[29] !== transactionType) {
		t9 = (udpTransaction_3) => {
			if (!transactionType?.domainFieldLabel) return null;
			return formatLabel(transactionType.domainFieldLabel, udpTransaction_3?.entityBusinessObject || {});
		};
		$[29] = transactionType;
		$[30] = t9;
	} else t9 = $[30];
	const getTransactionTypeLabel = t9;
	let t10;
	bb1: {
		if (!hasClosedStatus) {
			let t11;
			if ($[31] === Symbol.for("react.memo_cache_sentinel")) {
				t11 = [];
				$[31] = t11;
			} else t11 = $[31];
			t10 = t11;
			break bb1;
		}
		let t11;
		if ($[32] !== transactionType?.udpTransactionTypeUdpTransactionTypeStatus) {
			t11 = transactionType?.udpTransactionTypeUdpTransactionTypeStatus?.filter(_temp10);
			$[32] = transactionType?.udpTransactionTypeUdpTransactionTypeStatus;
			$[33] = t11;
		} else t11 = $[33];
		t10 = t11;
	}
	const closedStatuses = t10;
	let t11;
	if ($[34] !== closedStatuses) {
		t11 = (udpTransaction_4) => {
			if (!closedStatuses || closedStatuses.length === 0) return false;
			return closedStatuses.some((closedStatus) => closedStatus.transactionTypeStatusId === udpTransaction_4?.currentStatusId);
		};
		$[34] = closedStatuses;
		$[35] = t11;
	} else t11 = $[35];
	const checkIfTransactionIsClosed = t11;
	let t12;
	if ($[36] !== canCancel || $[37] !== checkIfTransactionIsClosed || $[38] !== closedStatuses || $[39] !== evaluateRule || $[40] !== getHiddenFields || $[41] !== getRequiredFields || $[42] !== getTransactionTypeLabel || $[43] !== getTransactionTypeTransactionActions || $[44] !== getUiActionRenderParameters || $[45] !== hasClosedStatus || $[46] !== hasRevisions || $[47] !== transactionType || $[48] !== transactionTypeColumnDefProperties || $[49] !== transactionTypeGridColumns || $[50] !== transactionTypePageConfiguration || $[51] !== transactionTypeStatusColorsMap || $[52] !== transactionTypeStatuses) {
		t12 = {
			currentTransactionType: transactionType,
			evaluateRule,
			getTransactionTypeTransactionActions,
			hasRevisions,
			getHiddenFields,
			getRequiredFields,
			closedStatuses,
			hasClosedStatus,
			transactionTypeGridColumns,
			transactionTypeColumnDefProperties,
			transactionTypePageConfiguration,
			getUiActionRenderParameters,
			checkIfTransactionIsClosed,
			getTransactionTypeLabel,
			transactionTypeStatusColorsMap,
			transactionTypeStatuses,
			canCancel
		};
		$[36] = canCancel;
		$[37] = checkIfTransactionIsClosed;
		$[38] = closedStatuses;
		$[39] = evaluateRule;
		$[40] = getHiddenFields;
		$[41] = getRequiredFields;
		$[42] = getTransactionTypeLabel;
		$[43] = getTransactionTypeTransactionActions;
		$[44] = getUiActionRenderParameters;
		$[45] = hasClosedStatus;
		$[46] = hasRevisions;
		$[47] = transactionType;
		$[48] = transactionTypeColumnDefProperties;
		$[49] = transactionTypeGridColumns;
		$[50] = transactionTypePageConfiguration;
		$[51] = transactionTypeStatusColorsMap;
		$[52] = transactionTypeStatuses;
		$[53] = t12;
	} else t12 = $[53];
	return t12;
};
function _temp(state) {
	return state.transactionTypesMap;
}
function _temp2(action) {
	return action.udpTransactionTypeConfigurationTransactionActionUdpTransactionTypeTransactionAction;
}
function _temp3(action_0) {
	return action_0;
}
function _temp4(status) {
	return status.isClosedStatus;
}
function _temp5(status_0) {
	return status_0.isCancelledStatus;
}
function _temp6(t0) {
	const [propertyName, value] = t0;
	return {
		propertyName,
		...value
	};
}
function _temp7(a, b) {
	return a.order - b.order;
}
function _temp8(action_1) {
	return action_1.transactionActionId === TransactionActionTypeEnums.Set_Fields_Hidden;
}
function _temp9(action_3) {
	return action_3.transactionActionId === TransactionActionTypeEnums.Set_Mandatory_Fields;
}
function _temp0(field) {
	return field.trim();
}
function _temp1(label, obj) {
	const keys = (label?.split("+"))?.map(_temp0);
	if (keys) {
		const skippableKeys = [];
		const updatedKeys = keys.map((key, index) => {
			if (key.charAt(0) === "\"") {
				skippableKeys.push(index);
				key = key.replace(/"/g, "");
			}
			return key;
		});
		const values = [];
		for (let j = 0; j < keys.length; j++) {
			const property = keys[j];
			if (property) {
				const splitProperties = property.split(".");
				if (splitProperties.length > 1) {
					let constructedValue = null;
					splitProperties.forEach((property_0) => {
						const matches = property_0.match(/\[(.*?)\]/);
						let objIndex = null;
						let arrIndex = null;
						if (matches) {
							arrIndex = parseInt(matches[1]);
							objIndex = matches.index;
						}
						if (typeof arrIndex === "number" && objIndex) {
							const formattedProperty = property_0.substring(0, objIndex);
							if (!constructedValue) {
								if (obj[formattedProperty]) constructedValue = obj[formattedProperty][arrIndex];
							} else if (constructedValue[formattedProperty]) constructedValue = constructedValue[formattedProperty][arrIndex];
						} else if (!constructedValue) constructedValue = obj[property_0];
						else constructedValue = constructedValue[property_0];
					});
					if (typeof constructedValue === "boolean" || typeof constructedValue === "number") constructedValue = constructedValue.toString();
					values.push(constructedValue);
					continue;
				}
			}
			let value_0 = obj[property];
			if (typeof value_0 === "boolean" || typeof value_0 === "number") value_0 = value_0.toString();
			values.push(value_0);
		}
		let displayName = updatedKeys.join("+");
		for (let k = 0; k < updatedKeys.length; k++) if (!skippableKeys.includes(k)) if (values[k]) displayName = displayName.replace(`${updatedKeys[k]}`, values[k]);
		else displayName = displayName.replace(`${updatedKeys[k]}`, "");
		return displayName.replaceAll("+", " ");
	} else return label;
}
function _temp10(status_2) {
	return status_2.isClosedStatus;
}
//#endregion
export { useTransactionEngine };

//# sourceMappingURL=useTransactionEngine.mjs.map