"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/editor/src/components/collab-sidebar/board-store.js
var board_store_exports = {};
__export(board_store_exports, {
  createBoardStore: () => createBoardStore
});
module.exports = __toCommonJS(board_store_exports);
function createBoardStore() {
  const listeners = /* @__PURE__ */ new Set();
  const blockRefs = /* @__PURE__ */ new Map();
  const floatingRefs = /* @__PURE__ */ new Map();
  const idByElement = /* @__PURE__ */ new WeakMap();
  const heights = {};
  let snapshot = {};
  function emit() {
    snapshot = { ...heights };
    for (const listener of listeners) {
      listener();
    }
  }
  const observer = new window.ResizeObserver((entries) => {
    let changed = false;
    for (const entry of entries) {
      const id = idByElement.get(entry.target);
      const newHeight = entry.borderBoxSize[0].blockSize;
      if (heights[id] !== newHeight) {
        heights[id] = newHeight;
        changed = true;
      }
    }
    if (changed) {
      emit();
    }
  });
  return {
    subscribe(listener) {
      listeners.add(listener);
      return () => {
        listeners.delete(listener);
        if (listeners.size === 0) {
          observer.disconnect();
        }
      };
    },
    getSnapshot() {
      return snapshot;
    },
    registerThread(id, blockEl, floatingEl) {
      blockRefs.set(id, blockEl);
      const prev = floatingRefs.get(id);
      if (prev && prev !== floatingEl) {
        observer.unobserve(prev);
        idByElement.delete(prev);
      }
      if (floatingEl) {
        floatingRefs.set(id, floatingEl);
        idByElement.set(floatingEl, id);
        observer.observe(floatingEl);
      }
      emit();
    },
    unregisterThread(id) {
      blockRefs.delete(id);
      const prev = floatingRefs.get(id);
      if (prev) {
        observer.unobserve(prev);
        idByElement.delete(prev);
        floatingRefs.delete(id);
      }
      delete heights[id];
    },
    getBlockRects() {
      return Object.fromEntries(
        Array.from(blockRefs).flatMap(
          ([id, el]) => el ? [[id, el.getBoundingClientRect()]] : []
        )
      );
    },
    getFirstBlockElement() {
      return blockRefs.values().next().value ?? null;
    }
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createBoardStore
});
//# sourceMappingURL=board-store.cjs.map
