function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useImperativeHandle, forwardRef } from 'react';
import { Button } from 'antd';
import { PisellModal } from '@pisell/materials';
import { locales, formatAmount } from '@pisell/utils';
import Decimal from 'decimal.js';
import checkoutLocales from "../../locales";
import useEngineContext from "../../../../hooks/useEngineContext";
var SavePayLaterHandler = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _ref$amountSymbol = _ref.amountSymbol,
    amountSymbol = _ref$amountSymbol === void 0 ? '$' : _ref$amountSymbol,
    walletPassData = _ref.walletPassData,
    onSavePayLater = _ref.onSavePayLater;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    showWalletBalanceConfirmModal = _useState2[0],
    setShowWalletBalanceConfirmModal = _useState2[1];
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    walletBalanceDeductAmount = _useState4[0],
    setWalletBalanceDeductAmount = _useState4[1];
  var context = useEngineContext();

  // 创建本地翻译函数，优先使用 checkout 语言包
  var getText = function getText(key, fallback) {
    var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4, _checkoutLocales;
    var currentLang = ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getApp) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.locales) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut2.getLocale) === null || _context$appHelper$ut3 === void 0 ? void 0 : _context$appHelper$ut3.call(_context$appHelper$ut2)) || ((_context$appHelper$ut4 = context.appHelper.utils) === null || _context$appHelper$ut4 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut4.storage) === null || _context$appHelper$ut4 === void 0 ? void 0 : _context$appHelper$ut4.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;
  };

  // 计算钱包应用总金额
  var getWalletTotalAmount = function getWalletTotalAmount() {
    if (!(walletPassData !== null && walletPassData !== void 0 && walletPassData.selectedWallet) || !Array.isArray(walletPassData.selectedWallet)) {
      return 0;
    }

    // 使用 Decimal.js 进行精确计算，避免浮点数累加精度问题
    var totalDecimal = walletPassData.selectedWallet.reduce(function (totalDecimal, wallet) {
      var redeemValue = parseFloat(wallet.redeem) || 0;
      var redeemDecimal = new Decimal(redeemValue);
      return totalDecimal.plus(redeemDecimal);
    }, new Decimal(0));
    return totalDecimal.toNumber();
  };

  // 处理 Save & Pay Later 点击
  var handleSavePayLaterClick = function handleSavePayLaterClick() {
    // 检查是否有选中的钱包余额需要确认
    if (walletPassData !== null && walletPassData !== void 0 && walletPassData.selectedWallet && walletPassData.selectedWallet.length > 0) {
      var amount = getWalletTotalAmount();
      var deductAmount = formatAmount(amount, 2, amountSymbol);
      setWalletBalanceDeductAmount(deductAmount);
      setShowWalletBalanceConfirmModal(true);
    } else {
      // 没有钱包余额选择，直接执行 Save & Pay Later
      onSavePayLater();
    }
  };

  // 处理钱包余额确认弹窗按钮点击
  var handleWalletBalanceConfirm = function handleWalletBalanceConfirm(action) {
    setShowWalletBalanceConfirmModal(false);
    onSavePayLater(action);
  };

  // 暴露方法给父组件
  useImperativeHandle(ref, function () {
    return {
      handleSavePayLaterClick: handleSavePayLaterClick
    };
  });
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(PisellModal, {
    visible: showWalletBalanceConfirmModal,
    onCancel: function onCancel() {
      return handleWalletBalanceConfirm('cancel');
    },
    closable: false,
    mask: true,
    maskClosable: false,
    centered: true,
    width: 530,
    title: getText('pisell2.checkout.wallet-balance-confirm.title'),
    footer: null,
    className: "wallet-balance-confirm-modal",
    styles: {
      header: {
        textAlign: 'center',
        borderBottom: '1px solid #EAECF0',
        paddingBottom: '16px'
      }
    }
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      padding: '40px 0 0 0'
    }
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      textAlign: 'center',
      marginBottom: '32px',
      lineHeight: '1.5'
    }
  }, /*#__PURE__*/React.createElement("span", {
    style: {
      color: '#533795',
      fontWeight: 600,
      fontSize: '18px'
    }
  }, walletBalanceDeductAmount), /*#__PURE__*/React.createElement("span", {
    style: {
      color: '#101828',
      fontSize: '16px'
    }
  }, getText('pisell2.checkout.wallet-balance-confirm.description-before')), /*#__PURE__*/React.createElement("span", {
    style: {
      color: '#533795',
      fontWeight: 600,
      fontSize: '16px'
    }
  }, getText('pisell2.checkout.wallet-balance-confirm.description-after'))), /*#__PURE__*/React.createElement("div", {
    style: {
      display: 'flex',
      gap: '12px',
      flexDirection: 'row'
    }
  }, /*#__PURE__*/React.createElement(Button, {
    onClick: function onClick() {
      return handleWalletBalanceConfirm('cancel');
    },
    style: {
      flex: '0 1 auto',
      minWidth: 'fit-content',
      height: '48px',
      borderRadius: '12px',
      fontSize: '16px',
      fontWeight: 500,
      border: '1px solid #D0D5DD',
      color: '#344054',
      padding: '0 16px',
      whiteSpace: 'nowrap'
    }
  }, getText('pisell2.checkout.wallet-balance-confirm.cancel')), /*#__PURE__*/React.createElement(Button, {
    onClick: function onClick() {
      return handleWalletBalanceConfirm('save-without-deduct');
    },
    style: {
      flex: '1 1 auto',
      height: '48px',
      borderRadius: '12px',
      fontSize: '16px',
      fontWeight: 500,
      border: '1px solid #D0D5DD',
      color: '#344054',
      padding: '0 16px',
      whiteSpace: 'nowrap'
    }
  }, getText('pisell2.checkout.wallet-balance-confirm.save-without-deduct')), /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    style: {
      flex: '1.2 1 auto',
      height: '48px',
      backgroundColor: '#533795',
      borderColor: '#533795',
      borderRadius: '12px',
      fontWeight: 600,
      fontSize: '16px',
      border: 'none',
      boxShadow: '0px 1px 2px rgba(16, 24, 40, 0.05)',
      padding: '0 16px',
      whiteSpace: 'nowrap'
    },
    onClick: function onClick() {
      return handleWalletBalanceConfirm('apply-deduct-now');
    }
  }, getText('pisell2.checkout.wallet-balance-confirm.apply-deduct-now'))))));
});
SavePayLaterHandler.displayName = 'SavePayLaterHandler';
export default SavePayLaterHandler;