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 ScrollableList from "../ScrollableList/index";
import React from 'react';
import classnames from 'classnames';
import "./index.less";
import { formatAmount, locales } from '@pisell/utils';
import { Iconfont } from '@pisell/materials';
import { useDebounceFn } from 'ahooks';
import useEngineContext from "../../../../hooks/useEngineContext";
import checkoutLocales from "../../locales";

// 扩展支付方法接口，添加手续费相关属性

// 扩展 PaymentOptionsModule Props 接口

// import CashIcon from './icons/cash.svg';
// import CreditCardIcon from './icons/credit_card.svg';
// import CustomIcon from './icons/custom.svg';
// import EftposIcon from './icons/eftpos.png';

var SHOW_METHODS_CODE = ['EFTPOS_LINKLY', 'EFTPOS', 'EFTPOS_TYRO', 'EFTPOS_WINDCAVE', 'EFTPOS_PAYO'];
function PaymentOptionsModule(_ref) {
  var _context$appHelper, _context$appHelper$ge, _context$appHelper$ge2, _context$appHelper$ut;
  var amountSymbol = _ref.amountSymbol,
    orderInfo = _ref.orderInfo,
    onClick = _ref.onClick,
    _ref$paymentMethods = _ref.paymentMethods,
    paymentMethods = _ref$paymentMethods === void 0 ? [] : _ref$paymentMethods,
    customAmount = _ref.customAmount,
    title = _ref.title,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled;
  var context = useEngineContext();

  // 初始化国际化
  locales.init(checkoutLocales, ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper$ge = _context$appHelper.getApp) === null || _context$appHelper$ge === void 0 || (_context$appHelper$ge = _context$appHelper$ge.call(_context$appHelper)) === null || _context$appHelper$ge === void 0 || (_context$appHelper$ge = _context$appHelper$ge.locales) === null || _context$appHelper$ge === void 0 || (_context$appHelper$ge2 = _context$appHelper$ge.getLocale) === null || _context$appHelper$ge2 === void 0 ? void 0 : _context$appHelper$ge2.call(_context$appHelper$ge)) || ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut = _context$appHelper$ut.storage) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.get('umi_locale')) || 'en');

  // 创建本地翻译函数，优先使用 checkout 语言包
  var getText = function getText(key, fallback) {
    var _context$appHelper2, _context$appHelper2$g, _context$appHelper2$g2, _context$appHelper$ut2, _checkoutLocales;
    var currentLang = ((_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 || (_context$appHelper2$g = _context$appHelper2.getApp) === null || _context$appHelper2$g === void 0 || (_context$appHelper2$g = _context$appHelper2$g.call(_context$appHelper2)) === null || _context$appHelper2$g === void 0 || (_context$appHelper2$g = _context$appHelper2$g.locales) === null || _context$appHelper2$g === void 0 || (_context$appHelper2$g2 = _context$appHelper2$g.getLocale) === null || _context$appHelper2$g2 === void 0 ? void 0 : _context$appHelper2$g2.call(_context$appHelper2$g)) || ((_context$appHelper$ut2 = context.appHelper.utils) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.storage) === null || _context$appHelper$ut2 === void 0 ? void 0 : _context$appHelper$ut2.get('umi_locale')) || 'en';
    var checkoutText = (_checkoutLocales = checkoutLocales[currentLang]) === null || _checkoutLocales === void 0 ? void 0 : _checkoutLocales[key];
    if (checkoutText) {
      return checkoutText;
    }

    // 回退到全局语言包
    return locales.getText(key) || fallback;
  };

  // 获取标题，优先使用props中的title，如果没有则使用国际化的默认值
  var displayTitle = typeof title === 'string' ? title : title ? String(title) : getText('cards.and.digitals', 'Cards & Digitals');
  var filteredPaymentMethods = paymentMethods.filter(function (item) {
    return !(item.type === 'app' && item.code === 'EFTPOS_LINKLY') && (['custom'].includes(item.type) || SHOW_METHODS_CODE.includes(item.code));
  });

  // 计算Surcharge金额
  var calculateSurcharge = function calculateSurcharge(method) {
    if (!(method !== null && method !== void 0 && method.is_surcharge)) return 0;
    var baseAmount = Number(customAmount) || 0;
    if (Number(method.percentage)) {
      return baseAmount * Number(method.percentage) + Number(method.fixed || 0);
    }
    return Number(method.fixed || 0);
  };

  // 计算总金额（包含Surcharge）
  var calculateTotalAmount = function calculateTotalAmount(method) {
    var baseAmount = Number(customAmount) || 0;
    var surcharge = calculateSurcharge(method);
    return baseAmount + surcharge;
  };

  // 防抖处理支付方式点击
  var _useDebounceFn = useDebounceFn(function (method) {
      if (disabled) {
        return;
      }
      if (typeof onClick === 'function') {
        onClick(_objectSpread(_objectSpread({}, method), {}, {
          surcharge: String(formatAmount(calculateSurcharge(method), 2, amountSymbol))
        }));
      }
    }, {
      wait: 500 // 500ms 防抖延迟
    }),
    handlePaymentClick = _useDebounceFn.run;
  var renderPaymentIcon = function renderPaymentIcon(method) {
    if (method.code === 'EFTPOS') {
      return /*#__PURE__*/React.createElement(Iconfont, {
        type: "pisell2-credit-card-02",
        style: {
          fontSize: '36px',
          color: '#7F56DA'
        }
      });
    }
    switch (String(method.type)) {
      case 'credit-card':
      case 'eftpos':
        return /*#__PURE__*/React.createElement(Iconfont, {
          type: "pisell2-credit-card-02",
          style: {
            fontSize: '36px',
            color: '#7F56DA'
          }
        });
      default:
        return /*#__PURE__*/React.createElement(Iconfont, {
          type: "pisell2-a-wallet-021",
          style: {
            fontSize: '36px',
            color: '#7F56DA'
          }
        });
    }
  };
  return /*#__PURE__*/React.createElement("div", {
    className: classnames('payment-options-module', {
      'payment-options-module-disabled': disabled
    })
  }, /*#__PURE__*/React.createElement("div", {
    className: classnames('payment-options-module-title')
  }, /*#__PURE__*/React.createElement("span", null, displayTitle)), /*#__PURE__*/React.createElement("div", {
    className: classnames('payment-options-module-list-container')
  }, /*#__PURE__*/React.createElement(ScrollableList, {
    height: "100%" // 占满剩余空间
    ,
    maxHeight: 400,
    gradientColor: "rgba(139, 92, 246, 0.1)",
    gradientHeight: 50,
    columnCount: 1,
    rowGap: 7,
    containerStyle: {
      width: '100%',
      flex: 1,
      // 使用flex来占用剩余空间
      minHeight: 0,
      // 允许缩小
      position: 'relative' // 确保定位上下文
    }
  }, filteredPaymentMethods.map(function (method) {
    return /*#__PURE__*/React.createElement("button", {
      key: method.id,
      onClick: function onClick(e) {
        e.preventDefault();
        handlePaymentClick(method);
      },
      className: classnames('payment-options-module-payment-method-item', {
        'payment-options-module-pointer': !disabled,
        'payment-options-module-disabled': disabled
      }),
      type: "button",
      disabled: disabled
    }, /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-payment-method-left')
    }, /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-icon-container')
    }, renderPaymentIcon(method)), /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-payment-info')
    }, /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-payment-info-inner')
    }, /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-payment-name')
    }, method.name), !!(method !== null && method !== void 0 && method.is_surcharge) && calculateSurcharge(method) !== 0 && /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-surcharge-text')
    }, getText('pisell2.payment-options.surcharge-label', 'Surcharge'), ":", formatAmount(calculateSurcharge(method), 2, amountSymbol))))), /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-payment-method-right')
    }, /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-amount', 'payment-options-module-theme-color')
    }, formatAmount(calculateTotalAmount(method), 2, amountSymbol)), /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-selection-button', {
        'payment-options-module-selection-button-disabled': disabled
      })
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-a-arrow-circle-right2",
      style: {
        fontSize: '30px',
        color: '#7F56DA'
      }
    }))));
  }))));
}
export default PaymentOptionsModule;