var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellDataSourceContainer/provider/RefManager.tsx
var RefManager_exports = {};
__export(RefManager_exports, {
  RefManagerContext: () => RefManagerContext,
  RefManagerProvider: () => RefManagerProvider,
  useRefManagerProvider: () => useRefManagerProvider
});
module.exports = __toCommonJS(RefManager_exports);
var import_react = __toESM(require("react"));
var RefManagerContext = (0, import_react.createContext)(
  null
);
var RefManagerProvider = ({
  children
}) => {
  const refMapRef = (0, import_react.useRef)(/* @__PURE__ */ new Map());
  const sharedStateRef = (0, import_react.useRef)(/* @__PURE__ */ new Map());
  const subscribersRef = (0, import_react.useRef)(
    /* @__PURE__ */ new Map()
  );
  const registerRef = (0, import_react.useCallback)((id, ref) => {
    if (!id) {
      console.warn("[RefManager] registerRef: id 不能为空");
      return;
    }
    refMapRef.current.set(id, ref);
  }, []);
  const getRef = (0, import_react.useCallback)((id) => {
    if (!id) {
      console.warn("[RefManager] getRef: id 不能为空");
      return void 0;
    }
    const ref = refMapRef.current.get(id);
    if (!ref) {
      console.warn(`[RefManager] 组件 "${id}" 未注册`);
    }
    return ref;
  }, []);
  const unregisterRef = (0, import_react.useCallback)((id) => {
    if (!id) {
      console.warn("[RefManager] unregisterRef: id 不能为空");
      return;
    }
    refMapRef.current.delete(id);
  }, []);
  const getSharedState = (0, import_react.useCallback)((key) => {
    if (!key) {
      console.warn("[RefManager] getSharedState: key 不能为空");
      return void 0;
    }
    return sharedStateRef.current.get(key);
  }, []);
  const setSharedState = (0, import_react.useCallback)((key, value) => {
    if (!key) {
      console.warn("[RefManager] setSharedState: key 不能为空");
      return;
    }
    sharedStateRef.current.set(key, value);
    const subscribers = subscribersRef.current.get(key);
    if (subscribers && subscribers.size > 0) {
      subscribers.forEach((callback) => {
        try {
          callback(value);
        } catch (error) {
          console.error(`[RefManager] 订阅回调执行失败 (key: ${key}):`, error);
        }
      });
    }
  }, []);
  const subscribeSharedState = (0, import_react.useCallback)(
    (key, callback) => {
      if (!key) {
        console.warn("[RefManager] subscribeSharedState: key 不能为空");
        return () => {
        };
      }
      if (!callback) {
        console.warn("[RefManager] subscribeSharedState: callback 不能为空");
        return () => {
        };
      }
      if (!subscribersRef.current.has(key)) {
        subscribersRef.current.set(key, /* @__PURE__ */ new Set());
      }
      const subscribers = subscribersRef.current.get(key);
      subscribers.add(callback);
      const currentState = sharedStateRef.current.get(key);
      if (currentState !== void 0) {
        try {
          callback(currentState);
        } catch (error) {
        }
      }
      return () => {
        subscribers.delete(callback);
        if (subscribers.size === 0) {
          subscribersRef.current.delete(key);
        }
      };
    },
    []
  );
  const contextValue = (0, import_react.useMemo)(
    () => ({
      // Ref 管理
      getRef,
      registerRef,
      unregisterRef,
      // SharedState 管理
      getSharedState,
      setSharedState,
      subscribeSharedState
    }),
    [
      getRef,
      registerRef,
      unregisterRef,
      getSharedState,
      setSharedState,
      subscribeSharedState
    ]
  );
  return /* @__PURE__ */ import_react.default.createElement(RefManagerContext.Provider, { value: contextValue }, children);
};
var useRefManagerProvider = () => {
  return (0, import_react.useContext)(RefManagerContext);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  RefManagerContext,
  RefManagerProvider,
  useRefManagerProvider
});
