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, useMemo, useRef } from 'react';
import { RecordView, PisellEmpty, PisellText, Typography } from '@pisell/materials';
import { Context, Provider } from "./model";
import { useDetail } from "../DetailContext";
import WalletCard from "../components/WalletCard";
import useEngineContext from "../../../hooks/useEngineContext";
import { locales, isMobile } from '@pisell/utils';
import localeTexts from "../locales";
import "./index.less";
var Title = Typography.Title;
var Amount = PisellText.Amount;
var RechargeableCard = Provider(function (props) {
  var _utils$getStore, _dvaStore$core;
  var globalState = props.globalState,
    setGlobalState = props.setGlobalState;
  var currentId = globalState.currentId,
    _globalState$product_ = globalState.product_discount_card,
    product_discount_card = _globalState$product_ === void 0 ? [] : _globalState$product_,
    currentDetail = globalState.currentDetail;
  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 platform = useMemo(function () {
    return isMobile() ? 'h5' : 'pc';
  }, []);
  var _useDetail = useDetail(),
    openDetail = _useDetail.openDetail,
    renderDetail = _useDetail.renderDetail,
    closeDetail = _useDetail.closeDetail;

  // 添加RecordView引用
  var recordViewRef = useRef(null);

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

  // 监听全局状态变化，同步到组件内部状态
  useEffect(function () {
    if (globalState.setup && globalState.setup !== (state === null || state === void 0 ? void 0 : state.setup)) {
      dispatch({
        type: 'setState',
        payload: {
          setup: globalState.setup
        }
      });

      // 添加打印，便于调试
    }
  }, [globalState.setup]);

  // 添加处理后退按钮的方法
  var handleBack = function handleBack() {
    if (recordViewRef.current) {
      recordViewRef.current.closeDetailModal();
    }
    // 关闭详情
    closeDetail();

    // 更新组件内部状态
    dispatch({
      type: 'setState',
      payload: {
        setup: 'list'
      }
    });

    // 更新全局状态
    setGlobalState({
      setup: 'list'
    });

    // 添加打印，便于调试
  };
  var _renderItem = function renderItem(item) {
    return /*#__PURE__*/React.createElement(WalletCard, {
      item: item,
      currentId: currentId,
      isDiscountCard: true,
      onClick: function onClick() {
        setGlobalState({
          currentId: item.id
        });
      }
    });
  };
  useEffect(function () {
    if (currentId) {
      dispatch({
        type: 'setState',
        payload: {
          currentDetail: product_discount_card.find(function (item) {
            return item.id === currentId;
          })
        }
      });
    }
  }, [currentId]);
  useEffect(function () {
    var enabledCards = product_discount_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
      }
    });
  }, [product_discount_card]);
  var isEmpty = !product_discount_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 fileterArr = product_discount_card.filter(function () {});
    var available = {
      title: locales.getText('page.wallet.available'),
      dataSource: product_discount_card.filter(function (item) {
        return item.limit_status === 'enable';
      })
    };
    var unavailable = {
      title: locales.getText('page.wallet.unavailable'),
      dataSource: product_discount_card.filter(function (item) {
        return item.limit_status === 'disable';
      })
    };
    return [available, unavailable];
  };
  var detail = function detail() {
    if (!(currentDetail !== null && currentDetail !== void 0 && currentDetail.id)) return null;

    // 打印当前状态

    // 添加必要属性，确保详情正确显示
    var detailWithBar = _objectSpread(_objectSpread({}, currentDetail), {}, {
      isDiscoundCard: true,
      showWalletBar: platform === 'h5',
      // 添加显示WalletBar的标志
      onBack: handleBack // 添加返回处理函数
    });
    return renderDetail(detailWithBar);
  };
  return isEmpty ? /*#__PURE__*/React.createElement(PisellEmpty, null) : /*#__PURE__*/React.createElement(React.Fragment, null, platform !== 'pc' && /*#__PURE__*/React.createElement(Title, {
    className: "wallet-discountCard-title"
  }, locales.getText('page.wallet.discountCard')), /*#__PURE__*/React.createElement(RecordView, {
    ref: recordViewRef,
    leftSpan: 9,
    rightSpan: 15,
    gutter: 20,
    onRowClick: function onRowClick(currentDetail) {
      // 设置全局状态
      setGlobalState({
        currentId: currentDetail.id || '',
        currentDetail: currentDetail,
        setup: 'detail' // 设置状态为detail
      });

      // 设置组件内部状态
      dispatch({
        type: 'setState',
        payload: {
          setup: 'detail'
        }
      });

      // 添加打印，便于调试

      // 添加必要属性，确保详情正确显示
      openDetail(_objectSpread(_objectSpread({}, currentDetail), {}, {
        isDiscoundCard: true,
        showWalletBar: platform === 'h5',
        // 添加显示WalletBar的标志
        onBack: handleBack // 添加返回处理函数
      }));
    },
    pcDetailModalDisplay: platform === 'h5' ? true : false,
    padDetailModalDisplay: true,
    mobilePadDetailModalDisplay: true,
    modalWrapStyle: {
      overflowY: 'auto',
      top: 0,
      height: '100%',
      zIndex: 998,
      position: 'fixed',
      left: 0,
      right: 0
    },
    split: false,
    detail: detail(),
    children: null,
    emptyButtons: [],
    doubleList: true,
    doubleListSource: formatData(),
    dataSource: product_discount_card,
    renderItem: function renderItem(value) {
      return _renderItem(value);
    },
    isShowClode: false
  }));
});
export default RechargeableCard;