import { __exports as m } from "./index-LexicalTablePlugin.prod2.js";
import { __require as R } from "./index-LexicalComposerContext2.js";
import { __require as h } from "./index-LexicalTable2.js";
import { __require as x } from "./index-LexicalUtils.js";
import { __require as w } from "./index-Lexical.js";
import $ from "react";
var C;
function M() {
  if (C) return m;
  C = 1;
  var _ = R(), t = h(), E = x(), n = w(), T = $;
  function b(u) {
    let f = new URLSearchParams();
    f.append("code", u);
    for (let c = 1; c < arguments.length; c++) f.append("v", arguments[c]);
    throw Error(`Minified Lexical error #${u}; visit https://lexical.dev/docs/error?${f} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`);
  }
  return m.TablePlugin = function({ hasCellMerge: u = !0, hasCellBackgroundColor: f = !0, hasTabHandler: c = !0 }) {
    let [o] = _.useLexicalComposerContext();
    return T.useEffect(() => (o.hasNodes([t.TableNode, t.TableCellNode, t.TableRowNode]) || b(10), o.registerCommand(t.INSERT_TABLE_COMMAND, ({ columns: e, rows: l, includeHeaders: s }) => (e = t.$createTableNodeWithDimensions(Number(l), Number(e), s), E.$insertNodeToNearestRoot(e), e = e.getFirstDescendant(), n.$isTextNode(e) && e.select(), !0), n.COMMAND_PRIORITY_EDITOR)), [o]), T.useEffect(
      () => {
        let e = /* @__PURE__ */ new Map(), l = (r) => {
          const i = r.getKey(), d = o.getElementByKey(i);
          d && !e.has(i) && (r = t.applyTableHandlers(r, d, o, c), e.set(i, r));
        };
        o.getEditorState().read(() => {
          let r = n.$nodesOfType(t.TableNode);
          for (let i of r) t.$isTableNode(i) && l(i);
        });
        let s = o.registerMutationListener(t.TableNode, (r) => {
          for (const [i, d] of r) d === "created" ? o.getEditorState().read(() => {
            const a = n.$getNodeByKey(i);
            t.$isTableNode(a) && l(a);
          }) : d === "destroyed" && (r = e.get(i), r !== void 0 && (r.removeListeners(), e.delete(i)));
        });
        return () => {
          s();
          for (let [, r] of e) r.removeListeners();
        };
      },
      [o, c]
    ), T.useEffect(() => {
      if (!u) return o.registerNodeTransform(t.TableCellNode, (e) => {
        if (1 < e.getColSpan() || 1 < e.getRowSpan()) {
          var [, , l] = n.DEPRECATED_$getNodeTriplet(e);
          [e] = n.DEPRECATED_$computeGridMap(l, e, e);
          let r = e.length, i = e[0].length;
          if (l = l.getFirstChild(), !n.DEPRECATED_$isGridRowNode(l)) throw Error("Expected TableNode first child to be a RowNode");
          let d = [];
          for (let a = 0; a < r; a++) {
            if (a !== 0 && (l = l.getNextSibling(), !n.DEPRECATED_$isGridRowNode(l))) throw Error("Expected TableNode first child to be a RowNode");
            let N = null;
            for (let p = 0; p < i; p++) {
              var s = e[a][p];
              let g = s.cell;
              if (s.startRow === a && s.startColumn === p) N = g, d.push(g);
              else if (1 < g.getColSpan() || 1 < g.getRowSpan()) {
                if (!t.$isTableCellNode(g)) throw Error("Expected TableNode cell to be a TableCellNode");
                s = t.$createTableCellNode(g.__headerState), N !== null ? N.insertAfter(s) : E.$insertFirst(l, s);
              }
            }
          }
          for (let a of d) a.setColSpan(1), a.setRowSpan(1);
        }
      });
    }, [o, u]), T.useEffect(
      () => {
        if (!f) return o.registerNodeTransform(t.TableCellNode, (e) => {
          e.getBackgroundColor() !== null && e.setBackgroundColor(null);
        });
      },
      [o, f, u]
    ), null;
  }, m;
}
export {
  M as __require
};
