class NamespacedWidgetSharedState {
  constructor(namespace) {
    this.namespace = namespace;
    this.values = /* @__PURE__ */ new Map();
    this.subscribers = /* @__PURE__ */ new Map();
  }
  get(key) {
    return this.values.get(this.toScopedKey(key));
  }
  set(key, value) {
    const scopedKey = this.toScopedKey(key);
    this.values.set(scopedKey, value);
    const handlers = this.subscribers.get(scopedKey);
    if (!handlers || handlers.size === 0) return;
    for (const handler of handlers) {
      handler(value);
    }
  }
  subscribe(key, handler) {
    const scopedKey = this.toScopedKey(key);
    const handlers = this.subscribers.get(scopedKey) ?? /* @__PURE__ */ new Set();
    handlers.add(handler);
    this.subscribers.set(scopedKey, handlers);
    return () => {
      const current = this.subscribers.get(scopedKey);
      if (!current) return;
      current.delete(handler);
      if (current.size === 0) {
        this.subscribers.delete(scopedKey);
      }
    };
  }
  toScopedKey(key) {
    return `${this.namespace}:${key}`;
  }
}
const storeByNamespace = /* @__PURE__ */ new Map();
function getWidgetSharedState(namespace) {
  const normalized = namespace.trim().length > 0 ? namespace.trim() : "global";
  const existing = storeByNamespace.get(normalized);
  if (existing) return existing;
  const created = new NamespacedWidgetSharedState(normalized);
  storeByNamespace.set(normalized, created);
  return created;
}
export {
  getWidgetSharedState
};
//# sourceMappingURL=WidgetSharedState.js.map
