import ScrollableList from "../ScrollableList/index";
import PaymentMethodItem from "./PaymentMethodItem";
import React from 'react';
import classnames from 'classnames';
import "./index.less";
import { locales } from '@pisell/utils';
import { useDebounceFn } from 'ahooks';
import useEngineContext from "../../../../hooks/useEngineContext";
import checkoutLocales from "../../locales";

// 扩展 PaymentOptionsModule Props 接口

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,
    _ref$showCashPayment = _ref.showCashPayment,
    showCashPayment = _ref$showCashPayment === void 0 ? false : _ref$showCashPayment;
  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));
  });

  // 防抖处理支付方式点击
  var _useDebounceFn = useDebounceFn(function (method) {
      if (disabled) {
        return;
      }
      if (typeof onClick === 'function') {
        onClick(method);
      }
    }, {
      wait: 500 // 500ms 防抖延迟
    }),
    handlePaymentClick = _useDebounceFn.run;
  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: 314,
    gradientColor: "rgba(139, 92, 246, 0.1)",
    gradientHeight: 50,
    columnCount: showCashPayment ? 1 : 2,
    rowGap: 7,
    columnGap: 8,
    containerStyle: {
      width: '100%',
      flex: 1,
      // 使用flex来占用剩余空间
      minHeight: 0,
      // 允许缩小
      position: 'relative',
      // 确保定位上下文
      paddingBottom: 8
    }
  }, filteredPaymentMethods.map(function (method) {
    return /*#__PURE__*/React.createElement(PaymentMethodItem, {
      key: method.id,
      method: method,
      amountSymbol: amountSymbol,
      customAmount: customAmount,
      disabled: disabled,
      onClick: handlePaymentClick,
      getText: getText
    });
  }))));
}
export default PaymentOptionsModule;