const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_types = require("./types.js");
//#region src/components/eftposPay/mx51/utils.ts
/**
* 解析MX51参数
* @param params 接口返回的原始参数
* @param status 支付状态
* @param data 接口返回的数据
* @returns 解析后的结果对象
*/
const parseMX51Params = (params, paymentStatus, data) => {
	if (!params) return null;
	const result = {
		transactionId: (params === null || params === void 0 ? void 0 : params.id) || "",
		status: (params === null || params === void 0 ? void 0 : params.status) || "",
		version: (params === null || params === void 0 ? void 0 : params.version) || 0,
		message: (params === null || params === void 0 ? void 0 : params.message) || "",
		merchantReceipt: params === null || params === void 0 ? void 0 : params.merchant_receipt,
		customerReceipt: params === null || params === void 0 ? void 0 : params.customer_receipt,
		resultAmounts: params === null || params === void 0 ? void 0 : params.result_amounts,
		resultFinancialStatus: params === null || params === void 0 ? void 0 : params.result_financial_status,
		layouts: [],
		details: {},
		detailsText: "",
		autoAction: []
	};
	if (!(params === null || params === void 0 ? void 0 : params.pos_instructions)) return result;
	const posInstructions = (params === null || params === void 0 ? void 0 : params.pos_instructions) || {};
	result.autoAction = (posInstructions === null || posInstructions === void 0 ? void 0 : posInstructions.auto_actions) || [];
	if (!(posInstructions === null || posInstructions === void 0 ? void 0 : posInstructions.action_form)) return result;
	const actionForm = (posInstructions === null || posInstructions === void 0 ? void 0 : posInstructions.action_form) || {};
	if (actionForm === null || actionForm === void 0 ? void 0 : actionForm.details) {
		result.details = require_objectSpread2._objectSpread2({}, (actionForm === null || actionForm === void 0 ? void 0 : actionForm.details) || {});
		const detailsTextArr = [];
		Object.keys((actionForm === null || actionForm === void 0 ? void 0 : actionForm.details) || {}).forEach((key) => {
			var _actionForm$details;
			detailsTextArr.push(`${key}: ${(actionForm === null || actionForm === void 0 || (_actionForm$details = actionForm.details) === null || _actionForm$details === void 0 ? void 0 : _actionForm$details[key]) || ""}`);
		});
		result.detailsText = (detailsTextArr === null || detailsTextArr === void 0 ? void 0 : detailsTextArr.join(", ")) || "";
	}
	if ((actionForm === null || actionForm === void 0 ? void 0 : actionForm.layout) && Array.isArray(actionForm === null || actionForm === void 0 ? void 0 : actionForm.layout)) {
		const layouts = [];
		actionForm.layout.forEach((layout) => {
			const elements = [];
			if ((layout === null || layout === void 0 ? void 0 : layout.elements) && Array.isArray(layout === null || layout === void 0 ? void 0 : layout.elements)) layout.elements.forEach((element) => {
				var _actionForm$propertie;
				const elementKey = (element === null || element === void 0 ? void 0 : element.key) || "";
				const elementLabel = (element === null || element === void 0 ? void 0 : element.label) || "";
				const property = (actionForm === null || actionForm === void 0 || (_actionForm$propertie = actionForm.properties) === null || _actionForm$propertie === void 0 ? void 0 : _actionForm$propertie[elementKey]) || {};
				if (property) {
					const element = {
						_extra: {
							merchantReceipt: params === null || params === void 0 ? void 0 : params.merchant_receipt,
							customerReceipt: params === null || params === void 0 ? void 0 : params.customer_receipt,
							paymentStatus,
							response: data
						},
						key: elementKey,
						name: (property === null || property === void 0 ? void 0 : property.name) || "",
						label: elementLabel,
						submitUrl: (property === null || property === void 0 ? void 0 : property.submit_url) || "",
						action: property === null || property === void 0 ? void 0 : property.action,
						type: (property === null || property === void 0 ? void 0 : property.type) || "button",
						text: property === null || property === void 0 ? void 0 : property.text,
						receipt: elementKey === require_types.EButtonKeyType.PrintMerchantReceipt ? result === null || result === void 0 ? void 0 : result.merchantReceipt : elementKey === require_types.EButtonKeyType.PrintCustomerReceipt ? result === null || result === void 0 ? void 0 : result.customerReceipt : null
					};
					if ((property === null || property === void 0 ? void 0 : property.type) === "image") element.image = {
						name: (property === null || property === void 0 ? void 0 : property.name) || "",
						data: (property === null || property === void 0 ? void 0 : property.data) || "",
						encoding: (property === null || property === void 0 ? void 0 : property.encoding) || ""
					};
					elements.push(element);
				}
			});
			if (elements.length > 0) layouts.push({
				layoutType: layout.type || "unknown",
				elements
			});
		});
		result.layouts = layouts;
	}
	return result;
};
//#endregion
exports.parseMX51Params = parseMX51Params;
