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, forwardRef, useImperativeHandle, useEffect, useMemo } from 'react';
import { PisellLoading } from '@pisell/materials';
import { locales } from '@pisell/utils';
import { useWalletPass } from "../../hooks/useWalletPass";
import WalletPassHeader from "./components/WalletPassHeader";
import WalletPassCardDisplay from "./components/WalletPassCardDisplay";
import WalletModal from "./components/WalletModal";
import DiscountWallet from "./components/DiscountWallet";
import { formatMachineCodeServer2CardList } from "./utils";
import { WALLET_PASS_CODE_ARRAY } from "../../constants";
import "./index.less";
var WalletPassModule = /*#__PURE__*/forwardRef(function (props, ref) {
  var _locales$getText;
  var orderInfo = props.orderInfo,
    onSelectChange = props.onSelectChange,
    customAmount = props.customAmount,
    paymentItems = props.paymentItems,
    clientId = props.clientId;
  var _useWalletPass = useWalletPass({
      onSelectChange: onSelectChange,
      customAmount: customAmount,
      clientId: clientId
    }),
    handleSelectWallet = _useWalletPass.handleSelectWallet,
    cardData = _useWalletPass.cardData,
    selectedWalletIds = _useWalletPass.selectedWalletIds,
    expiredWalletIds = _useWalletPass.expiredWalletIds,
    expiredWalletData = _useWalletPass.expiredWalletData,
    disabledWalletData = _useWalletPass.disabledWalletData,
    clearAllSelectedWallet = _useWalletPass.clearAllSelectedWallet,
    clearDisabledWalletData = _useWalletPass.clearDisabledWalletData,
    handleBestSaving = _useWalletPass.handleBestSaving,
    loading = _useWalletPass.loading,
    handleSearchIdentificationCode = _useWalletPass.handleSearchIdentificationCode,
    availableDiscountList = _useWalletPass.availableDiscountList,
    selectedDiscountList = _useWalletPass.selectedDiscountList,
    handleSelectDiscount = _useWalletPass.handleSelectDiscount,
    customerName = _useWalletPass.customerName,
    handleConfirmEditAmount = _useWalletPass.handleConfirmEditAmount,
    handleBeforeEditAmount = _useWalletPass.handleBeforeEditAmount;

  // 过期卡券弹窗状态
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    expiredModalVisible = _useState2[0],
    setExpiredModalVisible = _useState2[1];

  // 不可用卡券弹窗状态
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    disabledModalVisible = _useState4[0],
    setDisabledModalVisible = _useState4[1];

  // 显示模式状态：true为卡片模式，false为文字模式
  var _useState5 = useState(true),
    _useState6 = _slicedToArray(_useState5, 2),
    isCardMode = _useState6[0],
    setIsCardMode = _useState6[1];

  // 处理过期卡券点击事件
  var handleExpiredClick = function handleExpiredClick() {
    setExpiredModalVisible(true);
  };

  // 关闭过期卡券弹窗
  var handleExpiredModalClose = function handleExpiredModalClose() {
    setExpiredModalVisible(false);
  };

  // 关闭不可用卡券弹窗
  var handleDisabledModalClose = function handleDisabledModalClose() {
    setDisabledModalVisible(false);
    // 清空不可用钱包数据，防止重复显示
    clearDisabledWalletData();
  };

  // 切换显示模式
  var toggleDisplayMode = function toggleDisplayMode() {
    setIsCardMode(!isCardMode);
  };
  var handleDetail = function handleDetail(record, index) {};

  // 格式化不可用钱包数据
  var formattedDisabledWalletData = React.useMemo(function () {
    return formatMachineCodeServer2CardList(disabledWalletData || [], [], {
      error: true,
      showStatus: false
    });
  }, [disabledWalletData]);

  // 监听 disabledWalletData 变化，当有不可用钱包时自动显示弹窗
  useEffect(function () {
    if (disabledWalletData && disabledWalletData.length > 0) {
      // 只有在弹窗未显示时才打开，避免重复打开
      if (!disabledModalVisible) {
        setDisabledModalVisible(true);
      }
    }
  }, [disabledWalletData, disabledModalVisible]);
  useImperativeHandle(ref, function () {
    return {
      handleSearchIdentificationCode: handleSearchIdentificationCode,
      getWalletPassData: function getWalletPassData(select) {
        return {
          selectedWallet: select.length ? cardData.filter(function (card) {
            return selectedWalletIds.includes(card.id);
          }) : [],
          availableDiscountList: availableDiscountList,
          selectedDiscountList: selectedDiscountList
        };
      }
    };
  });
  var disabledTitle = useMemo(function () {
    if (disabledWalletData && disabledWalletData.length === 1) {
      var _disabledWalletData$;
      return ((_disabledWalletData$ = disabledWalletData[0]) === null || _disabledWalletData$ === void 0 || (_disabledWalletData$ = _disabledWalletData$.unified_messages) === null || _disabledWalletData$ === void 0 ? void 0 : _disabledWalletData$.title) || '';
    }
    return locales.getText('wallet-pass-module-disabled-modal-title');
  }, [disabledWalletData]);
  var selectedCount = useMemo(function () {
    return selectedWalletIds.length + selectedDiscountList.length;
  }, [selectedWalletIds.length, selectedDiscountList.length]);
  var discountDisabled = useMemo(function () {
    return !!(paymentItems !== null && paymentItems !== void 0 && paymentItems.some(function (item) {
      return !WALLET_PASS_CODE_ARRAY.includes(item.code);
    }));
  }, [paymentItems]);
  if (cardData.length === 0 && availableDiscountList.length === 0) {
    return null;
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "wallet-pass-module-container"
  }, /*#__PURE__*/React.createElement(PisellLoading, {
    delay: 300,
    loading: loading,
    fullscreen: false,
    mask: {
      backgroundColor: 'rgba(255, 255, 255, 0.5)',
      width: '100%',
      height: '100%'
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "wallet-pass-module"
  }, /*#__PURE__*/React.createElement(WalletPassHeader, {
    customerName: customerName,
    selectedCount: selectedCount,
    expiredCount: expiredWalletIds.length,
    isCardMode: isCardMode,
    onExpiredClick: handleExpiredClick,
    onBestSaving: handleBestSaving,
    onClearAll: clearAllSelectedWallet,
    onToggleDisplayMode: toggleDisplayMode
  }), /*#__PURE__*/React.createElement(WalletPassCardDisplay, {
    isCardMode: isCardMode,
    cardData: cardData,
    selectedWalletIds: selectedWalletIds,
    onSelectWallet: handleSelectWallet,
    onDetail: handleDetail,
    onConfirmEditAmount: handleConfirmEditAmount,
    onBeforeEditAmount: handleBeforeEditAmount
  }), /*#__PURE__*/React.createElement(DiscountWallet, {
    isCardMode: isCardMode,
    cardData: availableDiscountList,
    selectedWalletIds: selectedDiscountList,
    onSelectWallet: handleSelectDiscount,
    onDetail: handleDetail,
    discountDisabled: discountDisabled
  }), /*#__PURE__*/React.createElement(WalletModal, {
    visible: expiredModalVisible,
    walletData: expiredWalletData,
    onClose: handleExpiredModalClose,
    title: (_locales$getText = locales.getText('wallet-pass-module-expired-modal-title')) === null || _locales$getText === void 0 ? void 0 : _locales$getText((expiredWalletData === null || expiredWalletData === void 0 ? void 0 : expiredWalletData.length) || 0)
  }), /*#__PURE__*/React.createElement(WalletModal, {
    visible: disabledModalVisible,
    walletData: formattedDisabledWalletData,
    onClose: handleDisabledModalClose,
    title: disabledTitle
  }))));
});
export default WalletPassModule;