import React, { createContext, useContext, useRef, useCallback, useMemo } from 'react';
export var RefManagerContext = /*#__PURE__*/createContext(null);
export var RefManagerProvider = function RefManagerProvider(_ref) {
  var children = _ref.children;
  var refMapRef = useRef(new Map());
  /** 共享状态存储 */
  var sharedStateRef = useRef(new Map());
  /** 状态订阅器存储 */
  var subscribersRef = useRef(new Map());
  var registerRef = useCallback(function (id, ref) {
    if (!id) {
      console.warn('[RefManager] registerRef: id 不能为空');
      return;
    }
    refMapRef.current.set(id, ref);
  }, []);
  var getRef = useCallback(function (id) {
    if (!id) {
      console.warn('[RefManager] getRef: id 不能为空');
      return undefined;
    }
    var ref = refMapRef.current.get(id);
    if (!ref) {
      console.warn("[RefManager] \u7EC4\u4EF6 \"".concat(id, "\" \u672A\u6CE8\u518C"));
    }
    return ref;
  }, []);
  var unregisterRef = useCallback(function (id) {
    if (!id) {
      console.warn('[RefManager] unregisterRef: id 不能为空');
      return;
    }
    refMapRef.current.delete(id);
  }, []);

  // ========== SharedState 管理方法 ==========

  /**
   * 获取共享状态
   */
  var getSharedState = useCallback(function (key) {
    if (!key) {
      console.warn('[RefManager] getSharedState: key 不能为空');
      return undefined;
    }
    return sharedStateRef.current.get(key);
  }, []);

  /**
   * 设置共享状态（会自动通知所有订阅者）
   */
  var setSharedState = useCallback(function (key, value) {
    if (!key) {
      console.warn('[RefManager] setSharedState: key 不能为空');
      return;
    }
    sharedStateRef.current.set(key, value);
    var subscribers = subscribersRef.current.get(key);
    if (subscribers && subscribers.size > 0) {
      subscribers.forEach(function (callback) {
        try {
          callback(value);
        } catch (error) {
          console.error("[RefManager] \u8BA2\u9605\u56DE\u8C03\u6267\u884C\u5931\u8D25 (key: ".concat(key, "):"), error);
        }
      });
    }
  }, []);
  var subscribeSharedState = useCallback(function (key, callback) {
    if (!key) {
      console.warn('[RefManager] subscribeSharedState: key 不能为空');
      return function () {};
    }
    if (!callback) {
      console.warn('[RefManager] subscribeSharedState: callback 不能为空');
      return function () {};
    }
    if (!subscribersRef.current.has(key)) {
      subscribersRef.current.set(key, new Set());
    }
    var subscribers = subscribersRef.current.get(key);
    subscribers.add(callback);
    var currentState = sharedStateRef.current.get(key);
    if (currentState !== undefined) {
      try {
        callback(currentState);
      } catch (error) {}
    }
    return function () {
      subscribers.delete(callback);
      if (subscribers.size === 0) {
        subscribersRef.current.delete(key);
      }
    };
  }, []);
  var contextValue = useMemo(function () {
    return {
      // Ref 管理
      getRef: getRef,
      registerRef: registerRef,
      unregisterRef: unregisterRef,
      // SharedState 管理
      getSharedState: getSharedState,
      setSharedState: setSharedState,
      subscribeSharedState: subscribeSharedState
    };
  }, [getRef, registerRef, unregisterRef, getSharedState, setSharedState, subscribeSharedState]);
  return /*#__PURE__*/React.createElement(RefManagerContext.Provider, {
    value: contextValue
  }, children);
};
export var useRefManagerProvider = function useRefManagerProvider() {
  return useContext(RefManagerContext);
};