var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/eftposPay/mx51/utils.ts
var utils_exports = {};
__export(utils_exports, {
  parseMX51Params: () => parseMX51Params
});
module.exports = __toCommonJS(utils_exports);
var import_types = require("./types");
var parseMX51Params = (params, paymentStatus, data) => {
  if (!params) {
    return null;
  }
  const result = {
    transactionId: (params == null ? void 0 : params.id) || "",
    status: (params == null ? void 0 : params.status) || "",
    version: (params == null ? void 0 : params.version) || 0,
    message: (params == null ? void 0 : params.message) || "",
    merchantReceipt: params == null ? void 0 : params.merchant_receipt,
    customerReceipt: params == null ? void 0 : params.customer_receipt,
    resultAmounts: params == null ? void 0 : params.result_amounts,
    resultFinancialStatus: params == null ? void 0 : params.result_financial_status,
    layouts: [],
    details: {},
    detailsText: "",
    autoAction: []
  };
  if (!(params == null ? void 0 : params.pos_instructions)) {
    return result;
  }
  const posInstructions = (params == null ? void 0 : params.pos_instructions) || {};
  result.autoAction = (posInstructions == null ? void 0 : posInstructions.auto_actions) || [];
  if (!(posInstructions == null ? void 0 : posInstructions.action_form)) {
    return result;
  }
  const actionForm = (posInstructions == null ? void 0 : posInstructions.action_form) || {};
  if (actionForm == null ? void 0 : actionForm.details) {
    result.details = { ...(actionForm == null ? void 0 : actionForm.details) || {} };
    const detailsTextArr = [];
    Object.keys((actionForm == null ? void 0 : actionForm.details) || {}).forEach((key) => {
      var _a;
      detailsTextArr.push(`${key}: ${((_a = actionForm == null ? void 0 : actionForm.details) == null ? void 0 : _a[key]) || ""}`);
    });
    result.detailsText = (detailsTextArr == null ? void 0 : detailsTextArr.join(", ")) || "";
  }
  if ((actionForm == null ? void 0 : actionForm.layout) && Array.isArray(actionForm == null ? void 0 : actionForm.layout)) {
    const layouts = [];
    actionForm.layout.forEach((layout) => {
      const elements = [];
      if ((layout == null ? void 0 : layout.elements) && Array.isArray(layout == null ? void 0 : layout.elements)) {
        layout.elements.forEach((element) => {
          var _a;
          const elementKey = (element == null ? void 0 : element.key) || "";
          const elementLabel = (element == null ? void 0 : element.label) || "";
          const property = ((_a = actionForm == null ? void 0 : actionForm.properties) == null ? void 0 : _a[elementKey]) || {};
          if (property) {
            const element2 = {
              _extra: {
                merchantReceipt: params == null ? void 0 : params.merchant_receipt,
                customerReceipt: params == null ? void 0 : params.customer_receipt,
                paymentStatus,
                response: data
              },
              key: elementKey,
              name: (property == null ? void 0 : property.name) || "",
              label: elementLabel,
              submitUrl: (property == null ? void 0 : property.submit_url) || "",
              action: property == null ? void 0 : property.action,
              type: (property == null ? void 0 : property.type) || "button",
              text: property == null ? void 0 : property.text,
              // 如果是打印收据按钮，需要关联对应的小票信息
              receipt: elementKey === import_types.EButtonKeyType.PrintMerchantReceipt ? result == null ? void 0 : result.merchantReceipt : elementKey === import_types.EButtonKeyType.PrintCustomerReceipt ? result == null ? void 0 : result.customerReceipt : null
            };
            if ((property == null ? void 0 : property.type) === "image") {
              element2.image = {
                name: (property == null ? void 0 : property.name) || "",
                data: (property == null ? void 0 : property.data) || "",
                encoding: (property == null ? void 0 : property.encoding) || ""
              };
            }
            elements.push(element2);
          }
        });
      }
      if (elements.length > 0) {
        layouts.push({
          layoutType: layout.type || "unknown",
          elements
        });
      }
    });
    result.layouts = layouts;
  }
  return result;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  parseMX51Params
});
