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 React, { useContext, useEffect, useRef, useCallback } from 'react';
import { RecordView, PisellEmpty, CardMetricItem, Typography } from '@pisell/materials';
import { Context, Provider } from "./model";
import WalletCard from "../components/WalletCard";
import { useWalletCard } from "../hooks/useWalletCard";
import { locales } from '@pisell/utils';
import { useDetail } from "../DetailContext";
import "./index.less";
var Title = Typography.Title;
/**
 * 代金券组件
 * 展示用户的代金券列表和详情
 */
var Voucher = Provider(function (props) {
  var globalState = props.globalState,
    setGlobalState = props.setGlobalState;
  var _globalState$product_ = globalState.product_voucher,
    product_voucher = _globalState$product_ === void 0 ? [] : _globalState$product_;
  var recordViewRef = useRef(null);
  var _useDetail = useDetail(),
    openDetail = _useDetail.openDetail,
    renderDetail = _useDetail.renderDetail;
  // 使用通用钱包卡片钩子
  var _useWalletCard = useWalletCard({
      globalState: globalState,
      setGlobalState: setGlobalState,
      walletContext: useContext(Context),
      cardList: product_voucher,
      cardType: 'product_voucher'
    }),
    platform = _useWalletCard.platform,
    isEmpty = _useWalletCard.isEmpty,
    state = _useWalletCard.state,
    handleRowClick = _useWalletCard.handleRowClick,
    renderCardDetail = _useWalletCard.renderCardDetail,
    formatData = _useWalletCard.formatData;

  // 监听setup状态变化关闭详情
  useEffect(function () {
    if (globalState.setup !== 'detail' && recordViewRef.current) {
      recordViewRef.current.closeDetailModal();
    }
  }, [globalState.setup]);

  // 渲染卡片项
  var renderItem = function renderItem(item) {
    return /*#__PURE__*/React.createElement(WalletCard, {
      balanceTitle: item.tag === 'good_pass' ? locales.getText('page.wallet.goodPass') : undefined,
      item: item,
      currentId: globalState.currentId,
      onClick: function onClick() {
        setGlobalState({
          type: 'setState',
          payload: {
            currentId: item.id
          }
        });
      }
    });
  };
  if (isEmpty) {
    return /*#__PURE__*/React.createElement(PisellEmpty, null);
  }
  // 添加处理后退按钮的方法
  var handleBack = useCallback(function () {
    if (recordViewRef.current) {
      recordViewRef.current.closeDetailModal();
    }
    // 更新状态
    setGlobalState({
      setup: 'list'
    });
  }, [recordViewRef, setGlobalState]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, platform !== 'pc' && /*#__PURE__*/React.createElement(Title, {
    className: "wallet-voucher-title"
  }, locales.getText('page.wallet.voucher')), /*#__PURE__*/React.createElement(CardMetricItem, {
    title: locales.getText('page.wallet.voucherBalance'),
    style: {
      margin: '8px 0 24px'
    },
    hint: false,
    tags: false,
    number: state.total
  }), /*#__PURE__*/React.createElement(RecordView, {
    ref: recordViewRef,
    mobilePadDetailModalDisplay: true,
    leftSpan: 9,
    rightSpan: 15,
    gutter: 20,
    onRowClick: function onRowClick(currentDetail) {
      setGlobalState({
        currentId: currentDetail.id || '',
        currentDetail: currentDetail,
        setup: 'detail' // 设置状态为detail
      });

      // 添加必要属性，确保详情正确显示
      var detailWithProps = _objectSpread(_objectSpread({}, currentDetail), {}, {
        showWalletBar: platform === 'h5',
        // 添加显示WalletBar的标志
        onBack: handleBack // 添加返回处理函数
      });
      openDetail(detailWithProps);
    },
    modalWrapStyle: {
      overflowY: 'auto',
      top: 68,
      height: 'calc(100% - 68px)',
      zIndex: 998
    },
    pcDetailModalDisplay: platform === 'h5' ? true : false,
    padDetailModalDisplay: true,
    isShowClode: false,
    split: false,
    children: null,
    detail: renderCardDetail(),
    emptyButtons: [],
    doubleList: true,
    doubleListSource: formatData(),
    dataSource: product_voucher,
    renderItem: renderItem
  }));
});
export default Voucher;