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 useEngineContext from "../../../hooks/useEngineContext";
import { locales, isMobile } from '@pisell/utils';
import localeTexts from "../locales";
import "./index.less";
import { useDetail } from "../DetailContext";
import WalletCard from "../components/WalletCard";
var Title = Typography.Title;
var RechargeableCard = Provider(function (props) {
  var _utils$getStore, _dvaStore$core;
  var globalState = props.globalState,
    setGlobalState = props.setGlobalState;
  var currentId = globalState.currentId,
    currentDetail = globalState.currentDetail,
    point_card = globalState.point_card;
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var context = useEngineContext();
  var utils = context.appHelper.utils || {};
  var dvaStore = utils === null || utils === void 0 || (_utils$getStore = utils.getStore) === null || _utils$getStore === void 0 || (_utils$getStore = _utils$getStore.call(utils)) === null || _utils$getStore === void 0 ? void 0 : _utils$getStore.getState();
  var recordViewRef = useRef(null);
  var _useDetail = useDetail(),
    openDetail = _useDetail.openDetail,
    renderDetail = _useDetail.renderDetail;
  useEffect(function () {
    document.body.id = 'body';
  }, []);
  var platform = isMobile() ? 'h5' : 'pc';
  var _renderItem = function renderItem(item) {
    return /*#__PURE__*/React.createElement(WalletCard, {
      item: item,
      balanceTitle: locales.getText('page.wallet.pointCard'),
      currentId: currentId,
      onClick: function onClick() {
        setGlobalState({
          type: 'setState',
          payload: {
            currentId: item.id
          }
        });
      }
    });
  };
  useEffect(function () {
    var enabledCards = point_card.filter(function (item) {
      return item.limit_status === 'enable';
    });
    var total = enabledCards.reduce(function (sum, item) {
      return sum + Number(item.balance);
    }, 0);
    dispatch({
      type: 'setState',
      payload: {
        total: total
      }
    });
  }, [point_card]);
  var detail = function detail() {
    if (!(currentDetail !== null && currentDetail !== void 0 && currentDetail.id)) return null;

    // 添加必要属性，确保详情正确显示
    var detailWithProps = _objectSpread(_objectSpread({}, currentDetail), {}, {
      showWalletBar: platform === 'h5',
      // 添加显示WalletBar的标志
      onBack: handleBack // 添加返回处理函数
    });
    return renderDetail(detailWithProps);
  };

  // 添加处理后退按钮的方法
  var handleBack = useCallback(function () {
    if (recordViewRef.current) {
      recordViewRef.current.closeDetailModal();
    }
    // 更新状态
    setGlobalState({
      setup: 'list'
    });
  }, [recordViewRef, setGlobalState]);
  var isEmpty = !point_card.some(function (item) {
    return item.limit_status === 'enable';
  }); //判断可用卡片是否为空

  // 注册多语言
  locales.init(localeTexts, (dvaStore === null || dvaStore === void 0 || (_dvaStore$core = dvaStore.core) === null || _dvaStore$core === void 0 ? void 0 : _dvaStore$core.translateLocale) || 'en');
  var formatData = function formatData() {
    var available = {
      title: locales.getText('page.wallet.available'),
      dataSource: point_card.filter(function (item) {
        return item.limit_status === 'enable';
      })
    };
    var unavailable = {
      title: locales.getText('page.wallet.unavailable'),
      dataSource: point_card.filter(function (item) {
        return item.limit_status === 'disable';
      })
    };
    return [available, unavailable];
  };
  useEffect(function () {
    if (globalState.setup !== 'detail') {
      var _recordViewRef$curren;
      (_recordViewRef$curren = recordViewRef.current) === null || _recordViewRef$curren === void 0 || _recordViewRef$curren.closeDetailModal();
    }
  }, [globalState.setup]);
  return isEmpty ? /*#__PURE__*/React.createElement(PisellEmpty, null) : /*#__PURE__*/React.createElement(React.Fragment, null, platform !== 'pc' && /*#__PURE__*/React.createElement(Title, {
    className: "wallet-pointCard-title"
  }, locales.getText('page.wallet.pointCard')), /*#__PURE__*/React.createElement(CardMetricItem, {
    title: locales.getText('page.wallet.points'),
    style: {
      margin: '8px 0 24px'
    },
    hint: false,
    tags: false,
    number: state.total,
    showCurrencySymbol: false
  }), /*#__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: detail(),
    emptyButtons: [],
    doubleList: true,
    doubleListSource: formatData(),
    dataSource: point_card,
    renderItem: function renderItem(value) {
      return _renderItem(value);
    }
  }));
});
export default RechargeableCard;