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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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 visaIcon from "./payment/visa.png";
import creditCardIcon from "./payment/credit_card.png";

/**
 * @description: visa/master手续费是否一致
 * @param {any} creditCardConfig
 * @return {*}
 */
export var isCreditCardSurchargeSame = function isCreditCardSurchargeSame(creditCardConfig) {
  try {
    // 将配置项转换为map key为credit_card_type_area  value为config对象
    var configMap = {};
    creditCardConfig.forEach(function (config) {
      var key = "".concat((config === null || config === void 0 ? void 0 : config.credit_card_type) || '-', "_").concat((config === null || config === void 0 ? void 0 : config.area) || '-');
      configMap[key] = config;
    });

    // 获取配置项
    var visaDomestic = configMap['visa_domestic'];
    var visaWorld = configMap['visa_world'];
    var masterDomestic = configMap['master_domestic'];
    var masterWorld = configMap['master_world'];
    var formatFee = function formatFee(config) {
      var _ref, _config$percent, _config$amount;
      if (!config) return null;

      // 后端字段不统一, 做下兼容
      var percent = parseFloat((_ref = (_config$percent = config.percent) !== null && _config$percent !== void 0 ? _config$percent : config.percentage) !== null && _ref !== void 0 ? _ref : '0');
      var amount = parseFloat((_config$amount = config.amount) !== null && _config$amount !== void 0 ? _config$amount : '0');
      return {
        percentStr: percent,
        amountStr: amount
      };
    };
    var visaDomesticFee = formatFee(visaDomestic);
    var visaWorldFee = formatFee(visaWorld);
    var masterDomesticFee = formatFee(masterDomestic);
    var masterWorldFee = formatFee(masterWorld);

    // 判断手续费是否一致的辅助函数
    var isFeeSame = function isFeeSame(fee1, fee2) {
      if (!fee1 || !fee2) return false;

      // 判断百分比和固定金额是否一致
      return fee1.percentStr === fee2.percentStr && fee1.amountStr === fee2.amountStr;
    };

    // 判断国内和国际手续费是否一致
    var isDomesticSame = isFeeSame(visaDomesticFee, masterDomesticFee);
    var isWorldSame = isFeeSame(visaWorldFee, masterWorldFee);
    var isAllSame = isDomesticSame && isWorldSame;
    return isAllSame;
  } catch (error) {
    return false;
  }
};

/**
 * @description: 计算手续费 百分比 * amount + 固定金额
 * @param {any} config
 * @param {number} amount
 * @return {*}
 */
var totalFee = function totalFee(config, amount) {
  var _config$percent2;
  // 后端字段不统一?, 做下兼容
  var percentageFee = parseFloat((_config$percent2 = config.percent) !== null && _config$percent2 !== void 0 ? _config$percent2 : config.percentage) * Number(amount);
  var totalFee = percentageFee + parseFloat(config.amount);
  return totalFee;
};

/**
 * @description: 格式化visa/master手续费
 * @param {any} item 支付列表项
 * @param {number} amount 订单金额
 * @param {boolean} isSame 手续费是否一致
 * @return {*}
 */
export var formatCreditCardSurcharge = function formatCreditCardSurcharge(item, amount, isSame) {
  try {
    var surcharge_config = item.surcharge_config,
      currency_symbol = item.currency_symbol;

    // 格式化手续费列表
    var formatSurchargeList = function formatSurchargeList(list) {
      return list === null || list === void 0 ? void 0 : list.map(function (config) {
        var _ref2, _config$percent3;
        // 后端字段不统一, 做下兼容
        var percentageFee = parseFloat((_ref2 = (_config$percent3 = config === null || config === void 0 ? void 0 : config.percent) !== null && _config$percent3 !== void 0 ? _config$percent3 : config === null || config === void 0 ? void 0 : config.percentage) !== null && _ref2 !== void 0 ? _ref2 : 0);
        var percent = (percentageFee * 100).toFixed(2);
        var amountFee = parseFloat(config === null || config === void 0 ? void 0 : config.amount).toFixed(2);
        var _totalFee = totalFee(config, amount).toFixed(2);
        var areaText = (config === null || config === void 0 ? void 0 : config.area) === 'world' ? 'tocPay.text.rest-of-world' : 'tocPay.text.domestic';
        return _objectSpread(_objectSpread({}, config), {}, {
          area_text: areaText,
          surcharge_text: "".concat(percent, "% + ").concat(currency_symbol).concat(amountFee),
          surcharge_fee: "".concat(currency_symbol).concat(_totalFee)
        });
      });
    };
    if (isSame) {
      // 手续费一致 返回visa和master的其中一条手续费列表
      var visaResult = surcharge_config.filter(function (config) {
        return config.credit_card_type === 'visa';
      });
      return [{
        icons: [visaIcon, creditCardIcon],
        name: 'Visa / Mastercard',
        lists: formatSurchargeList(visaResult)
      }];
    } else {
      // 手续费不一致 分别返回visa和master的手续费列表
      var _visaResult = surcharge_config.filter(function (config) {
        return config.credit_card_type === 'visa';
      });
      var masterResult = surcharge_config.filter(function (config) {
        return config.credit_card_type === 'master';
      });
      return [{
        icons: [visaIcon],
        name: 'Visa',
        lists: formatSurchargeList(_visaResult)
      }, {
        icons: [creditCardIcon],
        name: 'Mastercard',
        lists: formatSurchargeList(masterResult)
      }];
    }
  } catch (error) {
    console.error(error);
    return [];
  }
};

/**
 * @description: 格式化visa/master手续费列表
 * @param {any} item
 * @param {number} amount
 * @return {*}
 */
export var formatCreditCardSurchargeList = function formatCreditCardSurchargeList(item, amount) {
  var isSame = isCreditCardSurchargeSame((item === null || item === void 0 ? void 0 : item.surcharge_config) || []);
  return formatCreditCardSurcharge(item || {}, amount, isSame);
};

/**
 * @description: 格式化visa/master手续费文案，只显示百分比区间
 * @return {*}
 */
export var formatCreditCardSurchargeText = function formatCreditCardSurchargeText(surcharge_config, surcharge_text) {
  try {
    if (!Array.isArray(surcharge_config) || surcharge_config.length === 0) {
      return surcharge_text;
    }

    // 找出最小百分比和最大百分比
    var percentList = surcharge_config.map(function (config) {
      var _ref3, _config$percent4;
      var percent = parseFloat((_ref3 = (_config$percent4 = config.percent) !== null && _config$percent4 !== void 0 ? _config$percent4 : config.percentage) !== null && _ref3 !== void 0 ? _ref3 : '0') * 100;
      return Number(percent.toFixed(2));
    });
    var minPercent = Math.min.apply(Math, _toConsumableArray(percentList));
    var maxPercent = Math.max.apply(Math, _toConsumableArray(percentList));

    // 如果最大值和最小值相等，则直接使用其中一个
    if (minPercent === maxPercent) {
      return "".concat(surcharge_text).concat(minPercent, "%");
    }
    return "".concat(surcharge_text).concat(minPercent, "% ~ ").concat(maxPercent, "%");
  } catch (error) {
    return surcharge_text;
  }
};

/**
 * @title: 计算支付列表的手续费
 * @description:
 * @param {any} paymentList
 * @param {number} amount
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-08-18 09:30
 */
export var calculateSurcharge = function calculateSurcharge(paymentList, amount) {
  return paymentList.map(function (payment) {
    var surcharge_config = payment.surcharge_config,
      currency_symbol = payment.currency_symbol,
      surcharge_text = payment.surcharge_text;

    // 没有手续费展示空
    if (!surcharge_config || surcharge_config.length === 0) {
      payment.surcharge = '';
      return payment;
    }

    // 分别计算出每一项的手续费  百分比 * amount + 固定金额
    var fees = surcharge_config.map(function (config) {
      return totalFee(config, amount);
    });

    // 取出最小值和最大值
    var minFee = Math.min.apply(Math, _toConsumableArray(fees));
    var maxFee = Math.max.apply(Math, _toConsumableArray(fees));

    // 如果手续费为0 则展示开孔
    if (minFee === 0 && maxFee === 0) {
      payment.surcharge = '';
      // 一致则只展示最小值
    } else if (minFee === maxFee) {
      payment.surcharge = "".concat(surcharge_text).concat(currency_symbol).concat(minFee.toFixed(2));
    } else {
      // 不一致展示区间
      payment.surcharge = "".concat(surcharge_text).concat(currency_symbol).concat(minFee.toFixed(2), " ~ ").concat(currency_symbol).concat(maxFee.toFixed(2));
    }

    // visa/master手续费特殊处理
    if (payment.service === 'mw.credit_card') {
      payment.surcharge = formatCreditCardSurchargeText(surcharge_config, surcharge_text);
      payment.surcharge_config_result = formatCreditCardSurchargeList(payment, amount);
    }
    return payment;
  });
};