import { TreeNodeQuery } from "./tree-node-query.js";
class Selection {
  constructor(editor, converters) {
    this.editor = editor;
    this.converters = converters;
    this.editor = editor;
    this.isEmpty = this.editor.state.selection.empty;
    this.doc = this.treeNode();
    this.collapsedDecorations = this.markToDecorations(this.editor.state.selection.$from.marks());
  }
  buildTree(node, parent = null, originalIndex, allSelectedNodes) {
    const treeNode = new TreeNodeQuery(node, parent, originalIndex, this.converters);
    if (allSelectedNodes.has(node)) {
      node.descendants((child, pos, parent2, index) => {
        if (allSelectedNodes.has(child)) {
          treeNode.children.push(this.buildTree(child, treeNode, index, allSelectedNodes));
        }
        return false;
      });
    }
    return treeNode;
  }
  treeNode() {
    const allSelectedNodes = /* @__PURE__ */ new Set();
    const ranges = this.editor.state.selection.ranges;
    const state = this.editor.state;
    ranges.forEach(({ $from, $to }) => {
      const from = $from.pos;
      const to = $to.pos;
      state.doc.nodesBetween(from, to, (node) => {
        allSelectedNodes.add(node);
      });
    });
    const rootNode = this.editor.state.doc.copy(this.editor.state.doc.content);
    allSelectedNodes.add(rootNode);
    return this.buildTree(rootNode, null, 0, allSelectedNodes);
  }
  markToDecorations(marks) {
    const dummyTextNode = this.editor.schema.text("dummyText", marks);
    const decorations = this.converters.fromPmNode(dummyTextNode.toJSON())?.textData?.decorations;
    return decorations || [];
  }
}
export {
  Selection
};
//# sourceMappingURL=selection.js.map
