var _excluded = ["buttonGroupList", "showWalletBar", "onBack"];
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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, { createContext, useContext, useState, useRef, useEffect, useMemo } from 'react';
import Detail from "./Detail";
import { locales, isMobile } from '@pisell/utils';
import useEngineContext from "../../hooks/useEngineContext";
import localeTexts from "./Detail/locales";
import { request } from "../../utils";

// 定义详情上下文类型

// 创建上下文
var DetailContext = /*#__PURE__*/createContext(null);
/**
 * 详情提供者组件
 * 管理钱包详情的显示状态和数据
 */
export var DetailProvider = function DetailProvider(_ref) {
  var _context$appHelper, _utils$getStore;
  var children = _ref.children;
  // 状态管理
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    currentDetail = _useState2[0],
    setCurrentDetail = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    isOpen = _useState4[0],
    setIsOpen = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    isInitialized = _useState6[0],
    setIsInitialized = _useState6[1];

  // 引用
  var detailRef = useRef(null);

  // 获取上下文
  var context = useEngineContext();
  var utils = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _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();

  // 初始化多语言和请求
  useEffect(function () {
    var _dvaStore$core;
    locales.init(localeTexts, (dvaStore === null || dvaStore === void 0 || (_dvaStore$core = dvaStore.core) === null || _dvaStore$core === void 0 ? void 0 : _dvaStore$core.translateLocale) || 'en');
    request.setRequest(utils === null || utils === void 0 ? void 0 : utils.request);
    setIsInitialized(true);
  }, []);

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

  /**
   * 打开详情
   * @param detail 详情数据
   */
  var openDetail = function openDetail(detail) {
    setCurrentDetail(detail);
    setIsOpen(true);
  };

  /**
   * 关闭详情
   */
  var closeDetail = function closeDetail() {
    setIsOpen(false);
  };

  /**
   * 添加钱包卡
   */
  var addWalletCard = function addWalletCard() {
    var _detailRef$current;
    (_detailRef$current = detailRef.current) === null || _detailRef$current === void 0 || _detailRef$current.addWalletCard();
  };

  /**
   * 设置显示模态框
   * @param show 是否显示
   */
  var setShowModal = function setShowModal(show) {
    setIsOpen(show);
  };

  /**
   * 渲染详情组件
   * @param detail 详情数据
   * @returns 详情组件
   */
  var renderDetail = function renderDetail(detail) {
    if (!detail) return null;

    // 从detail对象中提取出特殊属性
    var _detail$buttonGroupLi = detail.buttonGroupList,
      buttonGroupList = _detail$buttonGroupLi === void 0 ? [] : _detail$buttonGroupLi,
      showWalletBar = detail.showWalletBar,
      onBack = detail.onBack,
      restDetail = _objectWithoutProperties(detail, _excluded);
    return /*#__PURE__*/React.createElement(Detail, {
      detail: restDetail,
      showWalletBar: showWalletBar,
      onBack: onBack,
      addWalletCallback: function addWalletCallback(addWallet) {
        // 允许Detail组件接收回调函数
      },
      buttonGroupList: buttonGroupList
    });
  };

  // 上下文值
  var contextValue = useMemo(function () {
    return {
      openDetail: openDetail,
      closeDetail: closeDetail,
      currentDetail: currentDetail,
      isOpen: isOpen,
      addWalletCard: addWalletCard,
      setShowModal: setShowModal,
      renderDetail: renderDetail,
      isInitialized: isInitialized
    };
  }, [currentDetail, isOpen, isInitialized]);

  // 添加全局引用以便Detail组件访问
  useEffect(function () {
    // 使用全局对象暴露上下文
    window.__WALLET_DETAIL_CONTEXT__ = contextValue;
    return function () {
      delete window.__WALLET_DETAIL_CONTEXT__;
    };
  }, [contextValue]);
  return /*#__PURE__*/React.createElement(DetailContext.Provider, {
    value: contextValue
  }, children);
};

/**
 * 使用详情上下文的钩子
 * @returns DetailContext上下文值
 * @throws 如果不在DetailProvider内部使用，将抛出错误
 */
export var useDetail = function useDetail() {
  var context = useContext(DetailContext);
  if (!context) {
    throw new Error('useDetail必须在DetailProvider内部使用');
  }
  return context;
};