import { __exports as m } from "./index-LexicalTablePlugin.dev2.js";
import { __require as S } from "./index-LexicalComposerContext2.js";
import { __require as D } from "./index-LexicalTable2.js";
import { __require as L } from "./index-LexicalUtils.js";
import { __require as $ } from "./index-Lexical.js";
import h from "react";
var R;
function K() {
  if (R) return m;
  R = 1;
  var x = S(), o = D(), p = L(), a = $(), f = h;
  function w({
    hasCellMerge: N = !0,
    hasCellBackgroundColor: E = !0,
    hasTabHandler: C = !0
  }) {
    const [r] = x.useLexicalComposerContext();
    return f.useEffect(() => {
      if (!r.hasNodes([o.TableNode, o.TableCellNode, o.TableRowNode]))
        throw Error("TablePlugin: TableNode, TableCellNode or TableRowNode not registered on editor");
      return r.registerCommand(o.INSERT_TABLE_COMMAND, ({
        columns: e,
        rows: d,
        includeHeaders: c
      }) => {
        const l = o.$createTableNodeWithDimensions(Number(d), Number(e), c);
        p.$insertNodeToNearestRoot(l);
        const t = l.getFirstDescendant();
        return a.$isTextNode(t) && t.select(), !0;
      }, a.COMMAND_PRIORITY_EDITOR);
    }, [r]), f.useEffect(() => {
      const e = /* @__PURE__ */ new Map(), d = (l) => {
        const t = l.getKey(), i = r.getElementByKey(t);
        if (i && !e.has(t)) {
          const n = o.applyTableHandlers(l, i, r, C);
          e.set(t, n);
        }
      };
      r.getEditorState().read(() => {
        const l = a.$nodesOfType(o.TableNode);
        for (const t of l)
          o.$isTableNode(t) && d(t);
      });
      const c = r.registerMutationListener(o.TableNode, (l) => {
        for (const [t, i] of l)
          if (i === "created")
            r.getEditorState().read(() => {
              const n = a.$getNodeByKey(t);
              o.$isTableNode(n) && d(n);
            });
          else if (i === "destroyed") {
            const n = e.get(t);
            n !== void 0 && (n.removeListeners(), e.delete(t));
          }
      });
      return () => {
        c();
        for (const [, l] of e)
          l.removeListeners();
      };
    }, [r, C]), f.useEffect(() => {
      if (!N)
        return r.registerNodeTransform(o.TableCellNode, (e) => {
          if (e.getColSpan() > 1 || e.getRowSpan() > 1) {
            const [, , d] = a.DEPRECATED_$getNodeTriplet(e), [c] = a.DEPRECATED_$computeGridMap(d, e, e), l = c.length, t = c[0].length;
            let i = d.getFirstChild();
            if (!a.DEPRECATED_$isGridRowNode(i))
              throw Error("Expected TableNode first child to be a RowNode");
            const n = [];
            for (let s = 0; s < l; s++) {
              if (s !== 0 && (i = i.getNextSibling(), !a.DEPRECATED_$isGridRowNode(i)))
                throw Error("Expected TableNode first child to be a RowNode");
              let T = null;
              for (let b = 0; b < t; b++) {
                const g = c[s][b], u = g.cell;
                if (g.startRow === s && g.startColumn === b)
                  T = u, n.push(u);
                else if (u.getColSpan() > 1 || u.getRowSpan() > 1) {
                  if (!o.$isTableCellNode(u))
                    throw Error("Expected TableNode cell to be a TableCellNode");
                  const _ = o.$createTableCellNode(u.__headerState);
                  T !== null ? T.insertAfter(_) : p.$insertFirst(i, _);
                }
              }
            }
            for (const s of n)
              s.setColSpan(1), s.setRowSpan(1);
          }
        });
    }, [r, N]), f.useEffect(() => {
      if (!E)
        return r.registerNodeTransform(o.TableCellNode, (e) => {
          e.getBackgroundColor() !== null && e.setBackgroundColor(null);
        });
    }, [r, E, N]), null;
  }
  return m.TablePlugin = w, m;
}
export {
  K as __require
};
