function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { EButtonKeyType } from "./types";

/**
 * 解析MX51参数
 * @param params 接口返回的原始参数
 * @param status 支付状态
 * @param data 接口返回的数据
 * @returns 解析后的结果对象
 */
export var parseMX51Params = function parseMX51Params(params, paymentStatus, data) {
  if (!params) {
    return null;
  }
  // 默认值
  var 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: []
  };

  // 检查是否存在 pos_instructions
  if (!(params !== null && params !== void 0 && params.pos_instructions)) {
    return result;
  }
  var posInstructions = (params === null || params === void 0 ? void 0 : params.pos_instructions) || {};

  // 检查是否有自动操作
  result.autoAction = (posInstructions === null || posInstructions === void 0 ? void 0 : posInstructions.auto_actions) || [];

  // 检查是否存在 action_form
  if (!(posInstructions !== null && posInstructions !== void 0 && posInstructions.action_form)) {
    return result;
  }
  var actionForm = (posInstructions === null || posInstructions === void 0 ? void 0 : posInstructions.action_form) || {};

  // 解析详情信息
  if (actionForm !== null && actionForm !== void 0 && actionForm.details) {
    result.details = _objectSpread({}, (actionForm === null || actionForm === void 0 ? void 0 : actionForm.details) || {});
    var detailsTextArr = [];
    Object.keys((actionForm === null || actionForm === void 0 ? void 0 : actionForm.details) || {}).forEach(function (key) {
      var _actionForm$details;
      detailsTextArr.push("".concat(key, ": ").concat((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 && actionForm.layout && Array.isArray(actionForm === null || actionForm === void 0 ? void 0 : actionForm.layout)) {
    var layouts = [];

    // 遍历所有布局
    actionForm.layout.forEach(function (layout) {
      var elements = [];
      if (layout !== null && layout !== void 0 && layout.elements && Array.isArray(layout === null || layout === void 0 ? void 0 : layout.elements)) {
        // 遍历布局中的所有元素
        layout.elements.forEach(function (element) {
          var _actionForm$propertie;
          var elementKey = (element === null || element === void 0 ? void 0 : element.key) || '';
          var elementLabel = (element === null || element === void 0 ? void 0 : element.label) || '';

          // 从 properties 中获取对应的属性
          var property = (actionForm === null || actionForm === void 0 || (_actionForm$propertie = actionForm.properties) === null || _actionForm$propertie === void 0 ? void 0 : _actionForm$propertie[elementKey]) || {};
          if (property) {
            var _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: 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 === EButtonKeyType.PrintMerchantReceipt ? result === null || result === void 0 ? void 0 : result.merchantReceipt : elementKey === EButtonKeyType.PrintCustomerReceipt ? result === null || result === void 0 ? void 0 : result.customerReceipt : null
            };

            // 只在 type 为 'image' 时添加 image 字段
            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: elements
        });
      }
    });
    result.layouts = layouts;
  }
  return result;
};