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 { useEffect, useMemo } from 'react';
import { isMobile } from '@pisell/utils';
import { calculateTotalValue, hasAvailableCards, formatDataToGroups } from "../utils/formatUtils";
import { useDetail } from "../DetailContext";

/**
 * 钱包卡片Hook的参数
 */

/**
 * 钱包卡片Hook
 * 封装了钱包卡片组件的通用逻辑
 */
export function useWalletCard(_ref) {
  var globalState = _ref.globalState,
    setGlobalState = _ref.setGlobalState,
    walletContext = _ref.walletContext,
    cardList = _ref.cardList,
    cardType = _ref.cardType,
    _ref$isDiscountCard = _ref.isDiscountCard,
    isDiscountCard = _ref$isDiscountCard === void 0 ? false : _ref$isDiscountCard,
    _ref$valueField = _ref.valueField,
    valueField = _ref$valueField === void 0 ? 'balance' : _ref$valueField,
    _ref$setupValue = _ref.setupValue,
    setupValue = _ref$setupValue === void 0 ? 'detail' : _ref$setupValue;
  // 解构状态
  var state = walletContext.state,
    dispatch = walletContext.dispatch;
  var currentId = globalState.currentId,
    currentDetail = globalState.currentDetail;

  // 获取详情上下文
  var _useDetail = useDetail(),
    openDetail = _useDetail.openDetail,
    renderDetail = _useDetail.renderDetail;

  // 平台判断
  var platform = useMemo(function () {
    return isMobile() ? 'h5' : 'pc';
  }, []);

  // 计算总值
  useEffect(function () {
    var total = calculateTotalValue(cardList, valueField);
    dispatch({
      type: 'setState',
      payload: {
        total: total
      }
    });
  }, [cardList, dispatch, valueField]);

  // 查找当前详情
  useEffect(function () {
    if (currentId) {
      var detail = cardList.find(function (item) {
        return item.id === currentId;
      });
      if (detail) {
        dispatch({
          type: 'setState',
          payload: {
            currentDetail: detail
          }
        });
      }
    }
  }, [currentId, cardList, dispatch]);

  /**
   * 处理行点击
   * @param detail 详情数据
   */
  var handleRowClick = function handleRowClick(detail) {
    // 更新全局状态
    setGlobalState({
      currentId: detail.id || '',
      currentDetail: detail,
      setup: platform === 'h5' ? setupValue : undefined // 设置状态为detail
    });

    // 准备详情数据
    var detailToOpen = _objectSpread(_objectSpread({}, detail), {}, {
      isDiscoundCard: isDiscountCard,
      showWalletBar: platform === 'h5',
      // 添加显示WalletBar的标志
      onBack: function onBack() {
        // 返回处理函数
        setGlobalState({
          setup: 'list'
        });
      }
    });

    // 打开详情
    openDetail(detailToOpen);
  };

  /**
   * 渲染详情
   */
  var renderCardDetail = function renderCardDetail() {
    if (!(currentDetail !== null && currentDetail !== void 0 && currentDetail.id)) return null;

    // 准备详情数据
    var detailToRender = _objectSpread(_objectSpread({}, currentDetail), {}, {
      isDiscoundCard: isDiscountCard,
      showWalletBar: platform === 'h5',
      // 添加显示WalletBar的标志
      onBack: function onBack() {
        // 返回处理函数
        setGlobalState({
          setup: 'list'
        });
      }
    });
    return renderDetail(detailToRender);
  };

  // 格式化数据
  var formatData = function formatData() {
    return formatDataToGroups(cardList);
  };

  // 检查是否为空
  var isEmpty = !hasAvailableCards(cardList);
  return {
    platform: platform,
    currentId: currentId,
    currentDetail: currentDetail,
    isEmpty: isEmpty,
    state: state,
    dispatch: dispatch,
    handleRowClick: handleRowClick,
    renderCardDetail: renderCardDetail,
    formatData: formatData
  };
}

// 为了向后兼容，保留默认导出
export default useWalletCard;