import { __exports as T } from "./index-LexicalTable.dev2.js";
import { __require as it } from "./index-Lexical.js";
import { __require as st } from "./index-LexicalUtils.js";
var ye;
function ht() {
  if (ye) return T;
  ye = 1;
  var o = it(), S = st();
  class z extends o.INTERNAL_PointSelection {
    constructor(e, t, r) {
      super(t, r), this.gridKey = e;
    }
    getCachedNodes() {
      return this._cachedNodes;
    }
    setCachedNodes(e) {
      this._cachedNodes = e;
    }
    is(e) {
      return x(e) ? this.gridKey === e.gridKey && this.anchor.is(e.anchor) && this.focus.is(e.focus) : !1;
    }
    set(e, t, r) {
      this.dirty = !0, this.gridKey = e, this.anchor.key = t, this.focus.key = r, this._cachedNodes = null;
    }
    clone() {
      return new z(this.gridKey, this.anchor, this.focus);
    }
    isCollapsed() {
      return !1;
    }
    extract() {
      return this.getNodes();
    }
    insertRawText(e) {
    }
    insertText() {
    }
    insertNodes(e) {
      const t = this.focus.getNode();
      if (!o.$isElementNode(t))
        throw Error("Expected GridSelection focus to be an ElementNode");
      o.$normalizeSelection__EXPERIMENTAL(t.select(0, t.getChildrenSize())).insertNodes(e);
    }
    // TODO Deprecate this method. It's confusing when used with colspan|rowspan
    getShape() {
      const e = o.$getNodeByKey(this.anchor.key);
      if (!o.DEPRECATED_$isGridCellNode(e))
        throw Error("Expected GridSelection anchor to be (or a child of) GridCellNode");
      const t = o.DEPRECATED_$getGridCellNodeRect(e);
      if (t === null)
        throw Error("getCellRect: expected to find AnchorNode");
      const r = o.$getNodeByKey(this.focus.key);
      if (!o.DEPRECATED_$isGridCellNode(r))
        throw Error("Expected GridSelection focus to be (or a child of) GridCellNode");
      const i = o.DEPRECATED_$getGridCellNodeRect(r);
      if (i === null)
        throw Error("getCellRect: expected to find focusCellNode");
      const l = Math.min(t.columnIndex, i.columnIndex), a = Math.max(t.columnIndex, i.columnIndex), h = Math.min(t.rowIndex, i.rowIndex), u = Math.max(t.rowIndex, i.rowIndex);
      return {
        fromX: Math.min(l, a),
        fromY: Math.min(h, u),
        toX: Math.max(l, a),
        toY: Math.max(h, u)
      };
    }
    getNodes() {
      const e = this._cachedNodes;
      if (e !== null)
        return e;
      const t = this.anchor.getNode(), r = this.focus.getNode(), i = S.$findMatchingParent(t, o.DEPRECATED_$isGridCellNode), l = S.$findMatchingParent(r, o.DEPRECATED_$isGridCellNode);
      if (!o.DEPRECATED_$isGridCellNode(i))
        throw Error("Expected GridSelection anchor to be (or a child of) GridCellNode");
      if (!o.DEPRECATED_$isGridCellNode(l))
        throw Error("Expected GridSelection focus to be (or a child of) GridCellNode");
      const a = i.getParent();
      if (!o.DEPRECATED_$isGridRowNode(a))
        throw Error("Expected anchorCell to have a parent GridRowNode");
      const h = a.getParent();
      if (!o.DEPRECATED_$isGridNode(h))
        throw Error("Expected tableNode to have a parent GridNode");
      const u = l.getParents()[1];
      if (u !== h) {
        if (h.isParentOf(l)) {
          const p = u.getParent();
          if (p == null)
            throw Error("Expected focusCellParent to have a parent");
          this.set(this.gridKey, l.getKey(), p.getKey());
        } else {
          const p = h.getParent();
          if (p == null)
            throw Error("Expected gridParent to have a parent");
          this.set(this.gridKey, p.getKey(), l.getKey());
        }
        return this.getNodes();
      }
      const [f, w, s] = o.DEPRECATED_$computeGridMap(h, i, l);
      let c = Math.min(w.startColumn, s.startColumn), d = Math.min(w.startRow, s.startRow), g = Math.max(w.startColumn + w.cell.__colSpan - 1, s.startColumn + s.cell.__colSpan - 1), C = Math.max(w.startRow + w.cell.__rowSpan - 1, s.startRow + s.cell.__rowSpan - 1), E = c, m = d, N = c, _ = d;
      function D(p) {
        const {
          cell: M,
          startColumn: $,
          startRow: I
        } = p;
        c = Math.min(c, $), d = Math.min(d, I), g = Math.max(g, $ + M.__colSpan - 1), C = Math.max(C, I + M.__rowSpan - 1);
      }
      for (; c < E || d < m || g > N || C > _; ) {
        if (c < E) {
          const p = _ - m, M = E - 1;
          for (let $ = 0; $ <= p; $++)
            D(f[m + $][M]);
          E = M;
        }
        if (d < m) {
          const p = N - E, M = m - 1;
          for (let $ = 0; $ <= p; $++)
            D(f[M][E + $]);
          m = M;
        }
        if (g > N) {
          const p = _ - m, M = N + 1;
          for (let $ = 0; $ <= p; $++)
            D(f[m + $][M]);
          N = M;
        }
        if (C > _) {
          const p = N - E, M = _ + 1;
          for (let $ = 0; $ <= p; $++)
            D(f[M][E + $]);
          _ = M;
        }
      }
      const b = [h];
      let y = null;
      for (let p = d; p <= C; p++)
        for (let M = c; M <= g; M++) {
          const {
            cell: $
          } = f[p][M], I = $.getParent();
          if (!o.DEPRECATED_$isGridRowNode(I))
            throw Error("Expected GridCellNode parent to be a GridRowNode");
          I !== y && b.push(I), b.push($, ...Oe($)), y = I;
        }
      return o.isCurrentlyReadOnlyMode() || (this._cachedNodes = b), b;
    }
    getTextContent() {
      const e = this.getNodes();
      let t = "";
      for (let r = 0; r < e.length; r++)
        t += e[r].getTextContent();
      return t;
    }
  }
  function x(n) {
    return n instanceof z;
  }
  function q() {
    const n = o.$createPoint("root", 0, "element"), e = o.$createPoint("root", 0, "element");
    return new z("root", n, e);
  }
  function Oe(n) {
    const e = [], t = [n];
    for (; t.length > 0; ) {
      const r = t.pop();
      if (r === void 0)
        throw Error("Stack.length > 0; can't be undefined");
      o.$isElementNode(r) && t.unshift(...r.getChildren()), r !== n && e.push(r);
    }
    return e;
  }
  const ue = /^(\d+(?:\.\d+)?)px$/, A = {
    BOTH: 3,
    COLUMN: 2,
    NO_STATUS: 0,
    ROW: 1
  };
  class B extends o.DEPRECATED_GridCellNode {
    /** @internal */
    /** @internal */
    /** @internal */
    static getType() {
      return "tablecell";
    }
    static clone(e) {
      const t = new B(e.__headerState, e.__colSpan, e.__width, e.__key);
      return t.__rowSpan = e.__rowSpan, t.__backgroundColor = e.__backgroundColor, t;
    }
    static importDOM() {
      return {
        td: (e) => ({
          conversion: he,
          priority: 0
        }),
        th: (e) => ({
          conversion: he,
          priority: 0
        })
      };
    }
    static importJSON(e) {
      const t = e.colSpan || 1, r = e.rowSpan || 1, i = P(e.headerState, t, e.width || void 0);
      return i.__rowSpan = r, i.__backgroundColor = e.backgroundColor || null, i;
    }
    constructor(e = A.NO_STATUS, t = 1, r, i) {
      super(t, i), this.__headerState = e, this.__width = r, this.__backgroundColor = null;
    }
    createDOM(e) {
      const t = document.createElement(this.getTag());
      return this.__width && (t.style.width = `${this.__width}px`), this.__colSpan > 1 && (t.colSpan = this.__colSpan), this.__rowSpan > 1 && (t.rowSpan = this.__rowSpan), this.__backgroundColor !== null && (t.style.backgroundColor = this.__backgroundColor), S.addClassNamesToElement(t, e.theme.tableCell, this.hasHeader() && e.theme.tableCellHeader), t;
    }
    exportDOM(e) {
      const {
        element: t
      } = super.exportDOM(e);
      if (t) {
        const r = t, i = 700, l = this.getParentOrThrow().getChildrenSize();
        r.style.border = "1px solid black", this.__colSpan > 1 && (r.colSpan = this.__colSpan), this.__rowSpan > 1 && (r.rowSpan = this.__rowSpan), r.style.width = `${this.getWidth() || Math.max(90, i / l)}px`, r.style.verticalAlign = "top", r.style.textAlign = "start";
        const a = this.getBackgroundColor();
        a !== null ? r.style.backgroundColor = a : this.hasHeader() && (r.style.backgroundColor = "#f2f3f5");
      }
      return {
        element: t
      };
    }
    exportJSON() {
      return {
        ...super.exportJSON(),
        backgroundColor: this.getBackgroundColor(),
        headerState: this.__headerState,
        type: "tablecell",
        width: this.getWidth()
      };
    }
    getTag() {
      return this.hasHeader() ? "th" : "td";
    }
    setHeaderStyles(e) {
      const t = this.getWritable();
      return t.__headerState = e, this.__headerState;
    }
    getHeaderStyles() {
      return this.getLatest().__headerState;
    }
    setWidth(e) {
      const t = this.getWritable();
      return t.__width = e, this.__width;
    }
    getWidth() {
      return this.getLatest().__width;
    }
    getBackgroundColor() {
      return this.getLatest().__backgroundColor;
    }
    setBackgroundColor(e) {
      this.getWritable().__backgroundColor = e;
    }
    toggleHeaderStyle(e) {
      const t = this.getWritable();
      return (t.__headerState & e) === e ? t.__headerState -= e : t.__headerState += e, t;
    }
    hasHeaderState(e) {
      return (this.getHeaderStyles() & e) === e;
    }
    hasHeader() {
      return this.getLatest().__headerState !== A.NO_STATUS;
    }
    updateDOM(e) {
      return e.__headerState !== this.__headerState || e.__width !== this.__width || e.__colSpan !== this.__colSpan || e.__rowSpan !== this.__rowSpan || e.__backgroundColor !== this.__backgroundColor;
    }
    isShadowRoot() {
      return !0;
    }
    collapseAtStart() {
      return !0;
    }
    canBeEmpty() {
      return !1;
    }
    canIndent() {
      return !1;
    }
  }
  function he(n) {
    const e = n, t = n.nodeName.toLowerCase();
    let r;
    ue.test(e.style.width) && (r = parseFloat(e.style.width));
    const i = P(t === "th" ? A.ROW : A.NO_STATUS, e.colSpan, r);
    i.__rowSpan = e.rowSpan;
    const l = e.style.backgroundColor;
    return l !== "" && (i.__backgroundColor = l), {
      forChild: (a, h) => {
        if (R(h) && !o.$isElementNode(a)) {
          const u = o.$createParagraphNode();
          return o.$isLineBreakNode(a) && a.getTextContent() === `
` ? null : (u.append(a), u);
        }
        return a;
      },
      node: i
    };
  }
  function P(n, e = 1, t) {
    return o.$applyNodeReplacement(new B(n, e, t));
  }
  function R(n) {
    return n instanceof B;
  }
  class Y extends o.DEPRECATED_GridRowNode {
    /** @internal */
    static getType() {
      return "tablerow";
    }
    static clone(e) {
      return new Y(e.__height, e.__key);
    }
    static importDOM() {
      return {
        tr: (e) => ({
          conversion: xe,
          priority: 0
        })
      };
    }
    static importJSON(e) {
      return G(e.height);
    }
    constructor(e, t) {
      super(t), this.__height = e;
    }
    exportJSON() {
      return {
        ...super.exportJSON(),
        type: "tablerow",
        version: 1
      };
    }
    createDOM(e) {
      const t = document.createElement("tr");
      return this.__height && (t.style.height = `${this.__height}px`), S.addClassNamesToElement(t, e.theme.tableRow), t;
    }
    isShadowRoot() {
      return !0;
    }
    setHeight(e) {
      const t = this.getWritable();
      return t.__height = e, this.__height;
    }
    getHeight() {
      return this.getLatest().__height;
    }
    updateDOM(e) {
      return e.__height !== this.__height;
    }
    canBeEmpty() {
      return !1;
    }
    canIndent() {
      return !1;
    }
  }
  function xe(n) {
    const e = n;
    let t;
    return ue.test(e.style.height) && (t = parseFloat(e.style.height)), {
      node: G(t)
    };
  }
  function G(n) {
    return o.$applyNodeReplacement(new Y(n));
  }
  function L(n) {
    return n instanceof Y;
  }
  const Pe = typeof window < "u" && typeof window.document < "u" && typeof window.document.createElement < "u", Ie = (n) => Pe ? (n || window).getSelection() : null;
  class fe {
    constructor(e, t) {
      this.isHighlightingCells = !1, this.anchorX = -1, this.anchorY = -1, this.focusX = -1, this.focusY = -1, this.listenersToRemove = /* @__PURE__ */ new Set(), this.tableNodeKey = t, this.editor = e, this.grid = {
        cells: [],
        columns: 0,
        rows: 0
      }, this.gridSelection = null, this.anchorCellNodeKey = null, this.focusCellNodeKey = null, this.anchorCell = null, this.focusCell = null, this.hasHijackedSelectionStyles = !1, this.trackTableGrid();
    }
    getGrid() {
      return this.grid;
    }
    removeListeners() {
      Array.from(this.listenersToRemove).forEach((e) => e());
    }
    trackTableGrid() {
      const e = new MutationObserver((t) => {
        this.editor.update(() => {
          let r = !1;
          for (let l = 0; l < t.length; l++) {
            const u = t[l].target.nodeName;
            if (u === "TABLE" || u === "TR") {
              r = !0;
              break;
            }
          }
          if (!r)
            return;
          const i = this.editor.getElementByKey(this.tableNodeKey);
          if (!i)
            throw new Error("Expected to find TableElement in DOM");
          this.grid = K(i);
        });
      });
      this.editor.update(() => {
        const t = this.editor.getElementByKey(this.tableNodeKey);
        if (!t)
          throw new Error("Expected to find TableElement in DOM");
        this.grid = K(t), e.observe(t, {
          childList: !0,
          subtree: !0
        });
      });
    }
    clearHighlight() {
      const e = this.editor;
      this.isHighlightingCells = !1, this.anchorX = -1, this.anchorY = -1, this.focusX = -1, this.focusY = -1, this.gridSelection = null, this.anchorCellNodeKey = null, this.focusCellNodeKey = null, this.anchorCell = null, this.focusCell = null, this.hasHijackedSelectionStyles = !1, this.enableHighlightStyle(), e.update(() => {
        const t = o.$getNodeByKey(this.tableNodeKey);
        if (!F(t))
          throw new Error("Expected TableNode.");
        const r = e.getElementByKey(this.tableNodeKey);
        if (!r)
          throw new Error("Expected to find TableElement in DOM");
        const i = K(r);
        V(e, i, null), o.$setSelection(null), e.dispatchCommand(o.SELECTION_CHANGE_COMMAND, void 0);
      });
    }
    enableHighlightStyle() {
      const e = this.editor;
      e.update(() => {
        const t = e.getElementByKey(this.tableNodeKey);
        if (!t)
          throw new Error("Expected to find TableElement in DOM");
        S.removeClassNamesFromElement(t, e._config.theme.tableSelection), t.classList.remove("disable-selection"), this.hasHijackedSelectionStyles = !1;
      });
    }
    disableHighlightStyle() {
      const e = this.editor;
      e.update(() => {
        const t = e.getElementByKey(this.tableNodeKey);
        if (!t)
          throw new Error("Expected to find TableElement in DOM");
        S.addClassNamesToElement(t, e._config.theme.tableSelection), this.hasHijackedSelectionStyles = !0;
      });
    }
    updateTableGridSelection(e) {
      if (e != null && e.gridKey === this.tableNodeKey) {
        const t = this.editor;
        this.gridSelection = e, this.isHighlightingCells = !0, this.disableHighlightStyle(), V(t, this.grid, this.gridSelection);
      } else e == null ? this.clearHighlight() : (this.tableNodeKey = e.gridKey, this.updateTableGridSelection(e));
    }
    setFocusCellForSelection(e, t = !1) {
      const r = this.editor;
      r.update(() => {
        const i = o.$getNodeByKey(this.tableNodeKey);
        if (!F(i))
          throw new Error("Expected TableNode.");
        if (!r.getElementByKey(this.tableNodeKey))
          throw new Error("Expected to find TableElement in DOM");
        const a = e.x, h = e.y;
        if (this.focusCell = e, this.anchorCell !== null) {
          const u = Ie(r._window);
          u && u.setBaseAndExtent(this.anchorCell.elem, 0, this.focusCell.elem, 0);
        }
        if (!this.isHighlightingCells && (this.anchorX !== a || this.anchorY !== h || t))
          this.isHighlightingCells = !0, this.disableHighlightStyle();
        else if (a === this.focusX && h === this.focusY)
          return;
        if (this.focusX = a, this.focusY = h, this.isHighlightingCells) {
          const u = o.$getNearestNodeFromDOMNode(e.elem);
          if (this.gridSelection != null && this.anchorCellNodeKey != null && R(u)) {
            const f = u.getKey();
            this.gridSelection = this.gridSelection.clone() || q(), this.focusCellNodeKey = f, this.gridSelection.set(this.tableNodeKey, this.anchorCellNodeKey, this.focusCellNodeKey), o.$setSelection(this.gridSelection), r.dispatchCommand(o.SELECTION_CHANGE_COMMAND, void 0), V(r, this.grid, this.gridSelection);
          }
        }
      });
    }
    setAnchorCellForSelection(e) {
      this.isHighlightingCells = !1, this.anchorCell = e, this.anchorX = e.x, this.anchorY = e.y, this.editor.update(() => {
        const t = o.$getNearestNodeFromDOMNode(e.elem);
        if (R(t)) {
          const r = t.getKey();
          this.gridSelection = this.gridSelection != null ? this.gridSelection.clone() : q(), this.anchorCellNodeKey = r;
        }
      });
    }
    formatCells(e) {
      this.editor.update(() => {
        const t = o.$getSelection();
        if (!x(t))
          throw Error("Expected grid selection");
        const r = o.$createRangeSelection(), i = r.anchor, l = r.focus;
        t.getNodes().forEach((a) => {
          R(a) && a.getTextContentSize() !== 0 && (i.set(a.getKey(), 0, "element"), l.set(a.getKey(), a.getChildrenSize(), "element"), r.formatText(e));
        }), o.$setSelection(t), this.editor.dispatchCommand(o.SELECTION_CHANGE_COMMAND, void 0);
      });
    }
    clearText() {
      const e = this.editor;
      e.update(() => {
        const t = o.$getNodeByKey(this.tableNodeKey);
        if (!F(t))
          throw new Error("Expected TableNode.");
        const r = o.$getSelection();
        if (!x(r))
          throw Error("Expected grid selection");
        const i = r.getNodes().filter(R);
        if (i.length === this.grid.columns * this.grid.rows) {
          t.selectPrevious(), t.remove(), o.$getRoot().selectStart();
          return;
        }
        i.forEach((l) => {
          if (o.$isElementNode(l)) {
            const a = o.$createParagraphNode(), h = o.$createTextNode();
            a.append(h), l.append(a), l.getChildren().forEach((u) => {
              u !== a && u.remove();
            });
          }
        }), V(e, this.grid, null), o.$setSelection(null), e.dispatchCommand(o.SELECTION_CHANGE_COMMAND, void 0);
      });
    }
  }
  const ge = "__lexicalTableSelection";
  function Fe(n, e, t, r) {
    const i = t.getRootElement();
    if (i === null)
      throw new Error("No root element.");
    const l = new fe(t, n.getKey()), a = t._window || window;
    Ge(e, l), e.addEventListener("mousedown", (s) => {
      setTimeout(() => {
        if (s.button !== 0 || !a)
          return;
        const c = ee(s.target);
        c !== null && (X(s), l.setAnchorCellForSelection(c));
        const d = () => {
          a.removeEventListener("mouseup", d), a.removeEventListener("mousemove", g);
        }, g = (C) => {
          const E = ee(C.target);
          E !== null && (l.anchorX !== E.x || l.anchorY !== E.y) && (C.preventDefault(), l.setFocusCellForSelection(E));
        };
        a.addEventListener("mouseup", d), a.addEventListener("mousemove", g);
      }, 0);
    });
    const h = (s) => {
      s.button === 0 && t.update(() => {
        const c = o.$getSelection(), d = s.target;
        x(c) && c.gridKey === l.tableNodeKey && i.contains(d) && l.clearHighlight();
      });
    };
    a.addEventListener("mousedown", h), l.listenersToRemove.add(() => a.removeEventListener("mousedown", h)), l.listenersToRemove.add(t.registerCommand(o.KEY_ARROW_DOWN_COMMAND, (s) => W(t, s, "down", n, l), o.COMMAND_PRIORITY_HIGH)), l.listenersToRemove.add(t.registerCommand(o.KEY_ARROW_UP_COMMAND, (s) => W(t, s, "up", n, l), o.COMMAND_PRIORITY_HIGH)), l.listenersToRemove.add(t.registerCommand(o.KEY_ARROW_LEFT_COMMAND, (s) => W(t, s, "backward", n, l), o.COMMAND_PRIORITY_HIGH)), l.listenersToRemove.add(t.registerCommand(o.KEY_ARROW_RIGHT_COMMAND, (s) => W(t, s, "forward", n, l), o.COMMAND_PRIORITY_HIGH)), l.listenersToRemove.add(t.registerCommand(o.KEY_ESCAPE_COMMAND, (s) => {
      const c = o.$getSelection();
      if (x(c)) {
        const d = S.$findMatchingParent(c.focus.getNode(), R);
        if (R(d))
          return X(s), d.selectEnd(), !0;
      }
      return !1;
    }, o.COMMAND_PRIORITY_HIGH));
    const u = (s) => () => {
      const c = o.$getSelection();
      if (!v(c, n))
        return !1;
      if (x(c))
        return l.clearText(), !0;
      if (o.$isRangeSelection(c)) {
        const d = S.$findMatchingParent(c.anchor.getNode(), (b) => R(b));
        if (!R(d))
          return !1;
        const g = c.anchor.getNode(), C = c.focus.getNode(), E = n.isParentOf(g), m = n.isParentOf(C);
        if (E && !m || m && !E)
          return l.clearText(), !0;
        const _ = S.$findMatchingParent(c.anchor.getNode(), (b) => o.$isElementNode(b)), D = _ && S.$findMatchingParent(_, (b) => o.$isElementNode(b) && R(b.getParent()));
        if (!o.$isElementNode(D) || !o.$isElementNode(_))
          return !1;
        if (s === o.DELETE_LINE_COMMAND && D.getPreviousSibling() === null)
          return !0;
        if ((s === o.DELETE_CHARACTER_COMMAND || s === o.DELETE_WORD_COMMAND) && c.isCollapsed() && c.anchor.offset === 0 && _ !== D) {
          const b = _.getChildren(), y = o.$createParagraphNode();
          return b.forEach((p) => y.append(p)), _.replace(y), _.getWritable().__parent = d.getKey(), !0;
        }
      }
      return !1;
    };
    [o.DELETE_WORD_COMMAND, o.DELETE_LINE_COMMAND, o.DELETE_CHARACTER_COMMAND].forEach((s) => {
      l.listenersToRemove.add(t.registerCommand(s, u(s), o.COMMAND_PRIORITY_CRITICAL));
    });
    const f = (s) => {
      const c = o.$getSelection();
      if (!v(c, n))
        return !1;
      if (x(c))
        return s.preventDefault(), s.stopPropagation(), l.clearText(), !0;
      if (o.$isRangeSelection(c)) {
        const d = S.$findMatchingParent(c.anchor.getNode(), (g) => R(g));
        if (!R(d))
          return !1;
      }
      return !1;
    };
    l.listenersToRemove.add(t.registerCommand(o.KEY_BACKSPACE_COMMAND, f, o.COMMAND_PRIORITY_CRITICAL)), l.listenersToRemove.add(t.registerCommand(o.KEY_DELETE_COMMAND, f, o.COMMAND_PRIORITY_CRITICAL)), l.listenersToRemove.add(t.registerCommand(o.FORMAT_TEXT_COMMAND, (s) => {
      const c = o.$getSelection();
      if (!v(c, n))
        return !1;
      if (x(c))
        return l.formatCells(s), !0;
      if (o.$isRangeSelection(c)) {
        const d = S.$findMatchingParent(c.anchor.getNode(), (g) => R(g));
        if (!R(d))
          return !1;
      }
      return !1;
    }, o.COMMAND_PRIORITY_CRITICAL)), l.listenersToRemove.add(t.registerCommand(o.CONTROLLED_TEXT_INSERTION_COMMAND, (s) => {
      const c = o.$getSelection();
      if (!v(c, n))
        return !1;
      if (x(c))
        return l.clearHighlight(), !1;
      if (o.$isRangeSelection(c)) {
        const d = S.$findMatchingParent(c.anchor.getNode(), (g) => R(g));
        if (!R(d))
          return !1;
      }
      return !1;
    }, o.COMMAND_PRIORITY_CRITICAL)), r && l.listenersToRemove.add(t.registerCommand(o.KEY_TAB_COMMAND, (s) => {
      const c = o.$getSelection();
      if (!o.$isRangeSelection(c) || !c.isCollapsed() || !v(c, n))
        return !1;
      const d = ne(c.anchor.getNode());
      if (d === null)
        return !1;
      X(s);
      const g = n.getCordsFromCellNode(d, l.grid);
      return Ee(l, n, g.x, g.y, s.shiftKey ? "backward" : "forward"), !0;
    }, o.COMMAND_PRIORITY_CRITICAL)), l.listenersToRemove.add(t.registerCommand(o.FOCUS_COMMAND, (s) => n.isSelected(), o.COMMAND_PRIORITY_HIGH));
    function w(s) {
      const c = n.getCordsFromCellNode(s, l.grid);
      return n.getCellFromCordsOrThrow(c.x, c.y, l.grid);
    }
    return l.listenersToRemove.add(t.registerCommand(o.SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, (s) => {
      const {
        nodes: c,
        selection: d
      } = s, g = o.$INTERNAL_isPointSelection(d), C = o.$isRangeSelection(d), E = C && S.$findMatchingParent(d.anchor.getNode(), (O) => o.DEPRECATED_$isGridCellNode(O)) !== null && S.$findMatchingParent(d.focus.getNode(), (O) => o.DEPRECATED_$isGridCellNode(O)) !== null || g && !C;
      if (c.length !== 1 || !o.DEPRECATED_$isGridNode(c[0]) || !E)
        return !1;
      const {
        anchor: m
      } = d, N = c[0], _ = N.getChildren(), D = N.getFirstChildOrThrow().getChildrenSize(), b = N.getChildrenSize(), y = S.$findMatchingParent(m.getNode(), (O) => o.DEPRECATED_$isGridCellNode(O)), p = y && S.$findMatchingParent(y, (O) => o.DEPRECATED_$isGridRowNode(O)), M = p && S.$findMatchingParent(p, (O) => o.DEPRECATED_$isGridNode(O));
      if (!o.DEPRECATED_$isGridCellNode(y) || !o.DEPRECATED_$isGridRowNode(p) || !o.DEPRECATED_$isGridNode(M))
        return !1;
      const $ = p.getIndexWithinParent(), I = Math.min(M.getChildrenSize() - 1, $ + b - 1), ce = y.getIndexWithinParent(), we = Math.min(p.getChildrenSize() - 1, ce + D - 1), pe = Math.min(ce, we), Te = Math.min($, I), Re = Math.max(ce, we), Se = Math.max($, I), ot = M.getChildren();
      let be = 0, ae, de;
      for (let O = Te; O <= Se; O++) {
        const Me = ot[O];
        if (!o.DEPRECATED_$isGridRowNode(Me))
          return !1;
        const $e = _[be];
        if (!o.DEPRECATED_$isGridRowNode($e))
          return !1;
        const nt = Me.getChildren(), rt = $e.getChildren();
        let De = 0;
        for (let J = pe; J <= Re; J++) {
          const H = nt[J];
          if (!o.DEPRECATED_$isGridCellNode(H))
            return !1;
          const Ae = rt[De];
          if (!o.DEPRECATED_$isGridCellNode(Ae))
            return !1;
          O === Te && J === pe ? ae = H.getKey() : O === Se && J === Re && (de = H.getKey());
          const lt = H.getChildren();
          Ae.getChildren().forEach((k) => {
            o.$isTextNode(k) && o.$createParagraphNode().append(k), H.append(k);
          }), lt.forEach((k) => k.remove()), De++;
        }
        be++;
      }
      if (ae && de) {
        const O = q();
        O.set(c[0].getKey(), ae, de), o.$setSelection(O);
      }
      return !0;
    }, o.COMMAND_PRIORITY_CRITICAL)), l.listenersToRemove.add(t.registerCommand(o.SELECTION_CHANGE_COMMAND, () => {
      const s = o.$getSelection(), c = o.$getPreviousSelection();
      if (o.$isRangeSelection(s)) {
        const {
          anchor: d,
          focus: g
        } = s, C = d.getNode(), E = g.getNode(), m = ne(C), N = ne(E), _ = m && n.is(re(m)), D = N && n.is(re(N)), b = _ !== D, y = _ && D, p = s.isBackward();
        if (b) {
          const M = s.clone();
          M.focus.set(n.getKey(), p ? 0 : n.getChildrenSize(), "element"), o.$setSelection(M), Ce(t, l);
        } else y && (m.is(N) || (l.setAnchorCellForSelection(w(m)), l.setFocusCellForSelection(w(N), !0)));
      }
      return s && !s.is(c) && (x(s) || x(c)) && l.gridSelection && !l.gridSelection.is(c) ? (x(s) && s.gridKey === l.tableNodeKey ? l.updateTableGridSelection(s) : !x(s) && x(c) && c.gridKey === l.tableNodeKey && l.updateTableGridSelection(null), !1) : (l.hasHijackedSelectionStyles && !n.isSelected() ? Ke(t, l) : !l.hasHijackedSelectionStyles && n.isSelected() && Ce(t, l), !1);
    }, o.COMMAND_PRIORITY_CRITICAL)), l;
  }
  function Ge(n, e) {
    n[ge] = e;
  }
  function Le(n) {
    return n[ge];
  }
  function ee(n) {
    let e = n;
    for (; e != null; ) {
      const t = e.nodeName;
      if (t === "TD" || t === "TH") {
        const r = e._cell;
        return r === void 0 ? null : r;
      }
      e = e.parentNode;
    }
    return null;
  }
  function K(n) {
    const e = [], t = {
      cells: e,
      columns: 0,
      rows: 0
    };
    let r = n.firstChild, i = 0, l = 0;
    for (e.length = 0; r != null; ) {
      const a = r.nodeName;
      if (a === "TD" || a === "TH") {
        const f = r, w = {
          elem: f,
          hasBackgroundColor: f.style.backgroundColor !== "",
          highlighted: !1,
          x: i,
          y: l
        };
        r._cell = w;
        let s = e[l];
        s === void 0 && (s = e[l] = []), s[i] = w;
      } else {
        const f = r.firstChild;
        if (f != null) {
          r = f;
          continue;
        }
      }
      const h = r.nextSibling;
      if (h != null) {
        i++, r = h;
        continue;
      }
      const u = r.parentNode;
      if (u != null) {
        const f = u.nextSibling;
        if (f == null)
          break;
        l++, i = 0, r = f;
      }
    }
    return t.columns = i + 1, t.rows = l + 1, t;
  }
  function V(n, e, t) {
    const r = new Set(t ? t.getNodes() : []);
    te(e, (i, l) => {
      const a = i.elem;
      r.has(l) ? (i.highlighted = !0, Ne(n, i)) : (i.highlighted = !1, me(n, i), a.getAttribute("style") || a.removeAttribute("style"));
    });
  }
  function te(n, e) {
    const {
      cells: t
    } = n;
    for (let r = 0; r < t.length; r++) {
      const i = t[r];
      if (i)
        for (let l = 0; l < i.length; l++) {
          const a = i[l];
          if (!a)
            continue;
          const h = o.$getNearestNodeFromDOMNode(a.elem);
          h !== null && e(a, h, {
            x: l,
            y: r
          });
        }
    }
  }
  function Ce(n, e) {
    e.disableHighlightStyle(), te(e.grid, (t) => {
      t.highlighted = !0, Ne(n, t);
    });
  }
  function Ke(n, e) {
    e.enableHighlightStyle(), te(e.grid, (t) => {
      const r = t.elem;
      t.highlighted = !1, me(n, t), r.getAttribute("style") || r.removeAttribute("style");
    });
  }
  const Ee = (n, e, t, r, i) => {
    const l = i === "forward";
    switch (i) {
      case "backward":
      case "forward":
        return t !== (l ? n.grid.columns - 1 : 0) ? Q(e.getCellNodeFromCordsOrThrow(t + (l ? 1 : -1), r, n.grid), l) : r !== (l ? n.grid.rows - 1 : 0) ? Q(e.getCellNodeFromCordsOrThrow(l ? 0 : n.grid.columns - 1, r + (l ? 1 : -1), n.grid), l) : l ? e.selectNext() : e.selectPrevious(), !0;
      case "up":
        return r !== 0 ? Q(e.getCellNodeFromCordsOrThrow(t, r - 1, n.grid), !1) : e.selectPrevious(), !0;
      case "down":
        return r !== n.grid.rows - 1 ? Q(e.getCellNodeFromCordsOrThrow(t, r + 1, n.grid), !0) : e.selectNext(), !0;
      default:
        return !1;
    }
  }, ve = (n, e, t, r, i) => {
    const l = i === "forward";
    switch (i) {
      case "backward":
      case "forward":
        return t !== (l ? n.grid.columns - 1 : 0) && n.setFocusCellForSelection(e.getCellFromCordsOrThrow(t + (l ? 1 : -1), r, n.grid)), !0;
      case "up":
        return r !== 0 ? (n.setFocusCellForSelection(e.getCellFromCordsOrThrow(t, r - 1, n.grid)), !0) : !1;
      case "down":
        return r !== n.grid.rows - 1 ? (n.setFocusCellForSelection(e.getCellFromCordsOrThrow(t, r + 1, n.grid)), !0) : !1;
      default:
        return !1;
    }
  };
  function v(n, e) {
    if (o.$isRangeSelection(n) || x(n)) {
      const t = e.isParentOf(n.anchor.getNode()), r = e.isParentOf(n.focus.getNode());
      return t && r;
    }
    return !1;
  }
  function Q(n, e) {
    e ? n.selectStart() : n.selectEnd();
  }
  const oe = "172,206,247";
  function Ne(n, e) {
    const t = e.elem, r = o.$getNearestNodeFromDOMNode(t);
    if (!R(r))
      throw Error("Expected to find LexicalNode from Table Cell DOMNode");
    r.getBackgroundColor() === null ? t.style.setProperty("background-color", `rgb(${oe})`) : t.style.setProperty("background-image", `linear-gradient(to right, rgba(${oe},0.85), rgba(${oe},0.85))`), t.style.setProperty("caret-color", "transparent");
  }
  function me(n, e) {
    const t = e.elem, r = o.$getNearestNodeFromDOMNode(t);
    if (!R(r))
      throw Error("Expected to find LexicalNode from Table Cell DOMNode");
    r.getBackgroundColor() === null && t.style.removeProperty("background-color"), t.style.removeProperty("background-image"), t.style.removeProperty("caret-color");
  }
  function ne(n) {
    const e = S.$findMatchingParent(n, R);
    return R(e) ? e : null;
  }
  function re(n) {
    const e = S.$findMatchingParent(n, F);
    return F(e) ? e : null;
  }
  function W(n, e, t, r, i) {
    const l = o.$getSelection();
    if (!v(l, r))
      return !1;
    if (o.$isRangeSelection(l) && l.isCollapsed()) {
      if (t === "backward" || t === "forward")
        return !1;
      const {
        anchor: a,
        focus: h
      } = l, u = S.$findMatchingParent(a.getNode(), R), f = S.$findMatchingParent(h.getNode(), R);
      if (!R(u) || !u.is(f))
        return !1;
      const w = re(u);
      if (w !== r && w != null) {
        const N = n.getElementByKey(w.getKey());
        if (N != null)
          return i.grid = K(N), W(n, e, t, w, i);
      }
      const s = n.getElementByKey(u.__key), c = n.getElementByKey(a.key);
      if (c == null || s == null)
        return !1;
      let d;
      if (a.type === "element")
        d = c.getBoundingClientRect();
      else {
        const N = window.getSelection();
        if (N === null || N.rangeCount === 0)
          return !1;
        d = N.getRangeAt(0).getBoundingClientRect();
      }
      const g = t === "up" ? u.getFirstChild() : u.getLastChild();
      if (g == null)
        return !1;
      const C = n.getElementByKey(g.__key);
      if (C == null)
        return !1;
      const E = C.getBoundingClientRect();
      if (t === "up" ? E.top > d.top - d.height : d.bottom + d.height > E.bottom) {
        X(e);
        const N = r.getCordsFromCellNode(u, i.grid);
        if (e.shiftKey) {
          const _ = r.getCellFromCordsOrThrow(N.x, N.y, i.grid);
          i.setAnchorCellForSelection(_), i.setFocusCellForSelection(_, !0);
        } else
          return Ee(i, r, N.x, N.y, t);
        return !0;
      }
    } else if (x(l)) {
      const {
        anchor: a,
        focus: h
      } = l, u = S.$findMatchingParent(a.getNode(), R), f = S.$findMatchingParent(h.getNode(), R), [w] = l.getNodes(), s = n.getElementByKey(w.getKey());
      if (!R(u) || !R(f) || !F(w) || s == null)
        return !1;
      i.updateTableGridSelection(l);
      const c = K(s), d = r.getCordsFromCellNode(u, c), g = r.getCellFromCordsOrThrow(d.x, d.y, c);
      if (i.setAnchorCellForSelection(g), X(e), e.shiftKey) {
        const C = r.getCordsFromCellNode(f, c);
        return ve(i, w, C.x, C.y, t);
      } else
        f.selectEnd();
      return !0;
    }
    return !1;
  }
  function X(n) {
    n.preventDefault(), n.stopImmediatePropagation(), n.stopPropagation();
  }
  class U extends o.DEPRECATED_GridNode {
    /** @internal */
    static getType() {
      return "table";
    }
    static clone(e) {
      return new U(e.__key);
    }
    static importDOM() {
      return {
        table: (e) => ({
          conversion: ke,
          priority: 1
        })
      };
    }
    static importJSON(e) {
      return Z();
    }
    constructor(e) {
      super(e);
    }
    exportJSON() {
      return {
        ...super.exportJSON(),
        type: "table",
        version: 1
      };
    }
    createDOM(e, t) {
      const r = document.createElement("table");
      return S.addClassNamesToElement(r, e.theme.table), r;
    }
    updateDOM() {
      return !1;
    }
    exportDOM(e) {
      return {
        ...super.exportDOM(e),
        after: (t) => {
          if (t) {
            const r = t.cloneNode(), i = document.createElement("colgroup"), l = document.createElement("tbody");
            S.isHTMLElement(t) && l.append(...t.children);
            const a = this.getFirstChildOrThrow();
            if (!L(a))
              throw new Error("Expected to find row node.");
            const h = a.getChildrenSize();
            for (let u = 0; u < h; u++) {
              const f = document.createElement("col");
              i.append(f);
            }
            return r.replaceChildren(i, l), r;
          }
        }
      };
    }
    // TODO 0.10 deprecate
    canExtractContents() {
      return !1;
    }
    canBeEmpty() {
      return !1;
    }
    isShadowRoot() {
      return !0;
    }
    getCordsFromCellNode(e, t) {
      const {
        rows: r,
        cells: i
      } = t;
      for (let l = 0; l < r; l++) {
        const a = i[l];
        if (a == null)
          continue;
        const h = a.findIndex((u) => {
          if (!u) return;
          const {
            elem: f
          } = u;
          return o.$getNearestNodeFromDOMNode(f) === e;
        });
        if (h !== -1)
          return {
            x: h,
            y: l
          };
      }
      throw new Error("Cell not found in table.");
    }
    getCellFromCords(e, t, r) {
      const {
        cells: i
      } = r, l = i[t];
      if (l == null)
        return null;
      const a = l[e];
      return a ?? null;
    }
    getCellFromCordsOrThrow(e, t, r) {
      const i = this.getCellFromCords(e, t, r);
      if (!i)
        throw new Error("Cell not found at cords.");
      return i;
    }
    getCellNodeFromCords(e, t, r) {
      const i = this.getCellFromCords(e, t, r);
      if (i == null)
        return null;
      const l = o.$getNearestNodeFromDOMNode(i.elem);
      return R(l) ? l : null;
    }
    getCellNodeFromCordsOrThrow(e, t, r) {
      const i = this.getCellNodeFromCords(e, t, r);
      if (!i)
        throw new Error("Node at cords not TableCellNode.");
      return i;
    }
    canSelectBefore() {
      return !0;
    }
    canIndent() {
      return !1;
    }
  }
  function He(n, e) {
    const t = n.getElementByKey(e.getKey());
    if (t == null)
      throw new Error("Table Element Not Found");
    return K(t);
  }
  function ke(n) {
    return {
      node: Z()
    };
  }
  function Z() {
    return o.$applyNodeReplacement(new U());
  }
  function F(n) {
    return n instanceof U;
  }
  function Be(n, e, t = !0) {
    const r = Z();
    for (let i = 0; i < n; i++) {
      const l = G();
      for (let a = 0; a < e; a++) {
        let h = A.NO_STATUS;
        typeof t == "object" ? (i === 0 && t.rows && (h |= A.ROW), a === 0 && t.columns && (h |= A.COLUMN)) : t && (i === 0 && (h |= A.ROW), a === 0 && (h |= A.COLUMN));
        const u = P(h), f = o.$createParagraphNode();
        f.append(o.$createTextNode()), u.append(f), l.append(u);
      }
      r.append(l);
    }
    return r;
  }
  function Ye(n) {
    const e = S.$findMatchingParent(n, (t) => R(t));
    return R(e) ? e : null;
  }
  function le(n) {
    const e = S.$findMatchingParent(n, (t) => L(t));
    if (L(e))
      return e;
    throw new Error("Expected table cell to be inside of table row.");
  }
  function ie(n) {
    const e = S.$findMatchingParent(n, (t) => F(t));
    if (F(e))
      return e;
    throw new Error("Expected table cell to be inside of table.");
  }
  function We(n) {
    const e = le(n);
    return ie(e).getChildren().findIndex((r) => r.is(e));
  }
  function Xe(n) {
    return le(n).getChildren().findIndex((t) => t.is(n));
  }
  function _e(n, e) {
    const t = ie(n), {
      x: r,
      y: i
    } = t.getCordsFromCellNode(n, e);
    return {
      above: t.getCellNodeFromCords(r, i - 1, e),
      below: t.getCellNodeFromCords(r, i + 1, e),
      left: t.getCellNodeFromCords(r - 1, i, e),
      right: t.getCellNodeFromCords(r + 1, i, e)
    };
  }
  function Ue(n, e) {
    const t = n.getChildren();
    if (e >= t.length || e < 0)
      throw new Error("Expected table cell to be inside of table row.");
    return t[e].remove(), n;
  }
  function je(n, e, t = !0, r, i) {
    const l = n.getChildren();
    if (e >= l.length || e < 0)
      throw new Error("Table row target index out of range");
    const a = l[e];
    if (L(a))
      for (let h = 0; h < r; h++) {
        const u = a.getChildren(), f = u.length, w = G();
        for (let s = 0; s < f; s++) {
          const c = u[s];
          if (!R(c))
            throw Error("Expected table cell");
          const {
            above: d,
            below: g
          } = _e(c, i);
          let C = A.NO_STATUS;
          const E = d && d.getWidth() || g && g.getWidth() || void 0;
          (d && d.hasHeaderState(A.COLUMN) || g && g.hasHeaderState(A.COLUMN)) && (C |= A.COLUMN);
          const m = P(C, 1, E);
          m.append(o.$createParagraphNode()), w.append(m);
        }
        t ? a.insertAfter(w) : a.insertBefore(w);
      }
    else
      throw new Error("Row before insertion index does not exist.");
    return n;
  }
  function Je(n = !0) {
    const e = o.$getSelection();
    if (!o.$INTERNAL_isPointSelection(e))
      throw Error("Expected a INTERNAL_PointSelection");
    const t = e.focus.getNode(), [r, , i] = o.DEPRECATED_$getNodeTriplet(t), [l, a] = o.DEPRECATED_$computeGridMap(i, r, r), h = l[0].length, {
      startRow: u
    } = a;
    if (n) {
      const f = u + r.__rowSpan - 1, w = l[f], s = G();
      for (let d = 0; d < h; d++) {
        const {
          cell: g,
          startRow: C
        } = w[d];
        C + g.__rowSpan - 1 <= f ? s.append(P(A.NO_STATUS).append(o.$createParagraphNode())) : g.setRowSpan(g.__rowSpan + 1);
      }
      const c = i.getChildAtIndex(f);
      if (!o.DEPRECATED_$isGridRowNode(c))
        throw Error("focusEndRow is not a GridRowNode");
      c.insertAfter(s);
    } else {
      const f = l[u], w = G();
      for (let c = 0; c < h; c++) {
        const {
          cell: d,
          startRow: g
        } = f[c];
        g === u ? w.append(P(A.NO_STATUS).append(o.$createParagraphNode())) : d.setRowSpan(d.__rowSpan + 1);
      }
      const s = i.getChildAtIndex(u);
      if (!o.DEPRECATED_$isGridRowNode(s))
        throw Error("focusEndRow is not a GridRowNode");
      s.insertBefore(w);
    }
  }
  function ze(n, e, t = !0, r, i) {
    const l = n.getChildren(), a = [];
    for (let h = 0; h < l.length; h++) {
      const u = l[h];
      if (L(u))
        for (let f = 0; f < r; f++) {
          const w = u.getChildren();
          if (e >= w.length || e < 0)
            throw new Error("Table column target index out of range");
          const s = w[e];
          if (!R(s))
            throw Error("Expected table cell");
          const {
            left: c,
            right: d
          } = _e(s, i);
          let g = A.NO_STATUS;
          (c && c.hasHeaderState(A.ROW) || d && d.hasHeaderState(A.ROW)) && (g |= A.ROW);
          const C = P(g);
          C.append(o.$createParagraphNode()), a.push({
            newTableCell: C,
            targetCell: s
          });
        }
    }
    return a.forEach(({
      newTableCell: h,
      targetCell: u
    }) => {
      t ? u.insertAfter(h) : u.insertBefore(h);
    }), n;
  }
  function qe(n = !0) {
    const e = o.$getSelection();
    if (!o.$INTERNAL_isPointSelection(e))
      throw Error("Expected a PointSeleciton");
    const t = e.anchor.getNode(), r = e.focus.getNode(), [i] = o.DEPRECATED_$getNodeTriplet(t), [l, , a] = o.DEPRECATED_$getNodeTriplet(r), [h, u, f] = o.DEPRECATED_$computeGridMap(a, l, i), w = h.length, s = n ? Math.max(u.startColumn, f.startColumn) : Math.min(u.startColumn, f.startColumn), c = n ? s + l.__colSpan - 1 : s - 1, d = a.getFirstChild();
    if (!o.DEPRECATED_$isGridRowNode(d))
      throw Error("Expected firstTable child to be a row");
    let g = null;
    function C() {
      const m = P(A.NO_STATUS).append(o.$createParagraphNode());
      return g === null && (g = m), m;
    }
    let E = d;
    e: for (let m = 0; m < w; m++) {
      if (m !== 0) {
        const y = E.getNextSibling();
        if (!o.DEPRECATED_$isGridRowNode(y))
          throw Error("Expected row nextSibling to be a row");
        E = y;
      }
      const N = h[m];
      if (c < 0) {
        se(E, C());
        continue;
      }
      const {
        cell: _,
        startColumn: D,
        startRow: b
      } = N[c];
      if (D + _.__colSpan - 1 <= c) {
        let y = _, p = b, M = c;
        for (; p !== m && y.__rowSpan > 1; )
          if (M -= _.__colSpan, M >= 0) {
            const {
              cell: $,
              startRow: I
            } = N[M];
            y = $, p = I;
          } else {
            E.append(C());
            continue e;
          }
        y.insertAfter(C());
      } else
        _.setColSpan(_.__colSpan + 1);
    }
    g !== null && j(g);
  }
  function Ve(n, e) {
    const t = n.getChildren();
    for (let r = 0; r < t.length; r++) {
      const i = t[r];
      if (L(i)) {
        const l = i.getChildren();
        if (e >= l.length || e < 0)
          throw new Error("Table column target index out of range");
        l[e].remove();
      }
    }
    return n;
  }
  function Qe() {
    const n = o.$getSelection();
    if (!o.$INTERNAL_isPointSelection(n))
      throw Error("Expected a INTERNAL_PointSelection");
    const e = n.anchor.getNode(), t = n.focus.getNode(), [r, , i] = o.DEPRECATED_$getNodeTriplet(e), [l] = o.DEPRECATED_$getNodeTriplet(t), [a, h, u] = o.DEPRECATED_$computeGridMap(i, r, l), {
      startRow: f
    } = h, {
      startRow: w
    } = u, s = w + l.__rowSpan - 1;
    if (a.length === s - f + 1) {
      i.remove();
      return;
    }
    const c = a[0].length, d = a[s + 1], g = i.getChildAtIndex(s + 1);
    for (let C = s; C >= f; C--) {
      for (let m = c - 1; m >= 0; m--) {
        const {
          cell: N,
          startRow: _,
          startColumn: D
        } = a[C][m];
        if (D === m && (C === f && _ < f && N.setRowSpan(N.__rowSpan - (_ - f)), _ >= f && _ + N.__rowSpan - 1 > s)) {
          if (N.setRowSpan(N.__rowSpan - (s - _ + 1)), g === null)
            throw Error("Expected nextRowNode not to be null");
          if (m === 0)
            se(g, N);
          else {
            const {
              cell: b
            } = d[m - 1];
            b.insertAfter(N);
          }
        }
      }
      const E = i.getChildAtIndex(C);
      if (!o.DEPRECATED_$isGridRowNode(E))
        throw Error(`Expected GridNode childAtIndex(${String(C)}) to be RowNode`);
      E.remove();
    }
    if (d !== void 0) {
      const {
        cell: C
      } = d[0];
      j(C);
    } else {
      const C = a[f - 1], {
        cell: E
      } = C[0];
      j(E);
    }
  }
  function Ze() {
    const n = o.$getSelection();
    if (!o.$INTERNAL_isPointSelection(n))
      throw Error("Expected a INTERNAL_PointSelection");
    const e = n.anchor.getNode(), t = n.focus.getNode(), [r, , i] = o.DEPRECATED_$getNodeTriplet(e), [l] = o.DEPRECATED_$getNodeTriplet(t), [a, h, u] = o.DEPRECATED_$computeGridMap(i, r, l), {
      startColumn: f
    } = h, {
      startRow: w,
      startColumn: s
    } = u, c = Math.min(f, s), d = Math.max(f + r.__colSpan - 1, s + l.__colSpan - 1), g = d - c + 1;
    if (a[0].length === d - c + 1) {
      i.selectPrevious(), i.remove();
      return;
    }
    const E = a.length;
    for (let _ = 0; _ < E; _++)
      for (let D = c; D <= d; D++) {
        const {
          cell: b,
          startColumn: y
        } = a[_][D];
        if (y < c) {
          if (D === c) {
            const p = c - y;
            b.setColSpan(b.__colSpan - // Possible overflow right too
            Math.min(g, b.__colSpan - p));
          }
        } else if (y + b.__colSpan - 1 > d) {
          if (D === d) {
            const p = d - y + 1;
            b.setColSpan(b.__colSpan - p);
          }
        } else
          b.remove();
      }
    const m = a[w], N = m[s + l.__colSpan];
    if (N !== void 0) {
      const {
        cell: _
      } = N;
      j(_);
    } else {
      const _ = m[s - 1], {
        cell: D
      } = _;
      j(D);
    }
  }
  function j(n) {
    const e = n.getFirstDescendant();
    e == null ? n.selectStart() : e.getParentOrThrow().selectStart();
  }
  function se(n, e) {
    const t = n.getFirstChild();
    t !== null ? t.insertBefore(e) : n.append(e);
  }
  function et() {
    const n = o.$getSelection();
    if (!o.$INTERNAL_isPointSelection(n))
      throw Error("Expected a INTERNAL_PointSelection");
    const e = n.anchor.getNode(), [t, r, i] = o.DEPRECATED_$getNodeTriplet(e), l = t.__colSpan, a = t.__rowSpan;
    if (l > 1) {
      for (let h = 1; h < l; h++)
        t.insertAfter(P(A.NO_STATUS));
      t.setColSpan(1);
    }
    if (a > 1) {
      const [h, u] = o.DEPRECATED_$computeGridMap(i, t, t), {
        startColumn: f,
        startRow: w
      } = u;
      let s;
      for (let c = 1; c < a; c++) {
        const d = w + c, g = h[d];
        if (s = (s || r).getNextSibling(), !o.DEPRECATED_$isGridRowNode(s))
          throw Error("Expected row next sibling to be a row");
        let C = null;
        for (let E = 0; E < f; E++) {
          const m = g[E], N = m.cell;
          m.startRow === d && (C = N), N.__colSpan > 1 && (E += N.__colSpan - 1);
        }
        if (C === null)
          for (let E = 0; E < l; E++)
            se(s, P(A.NO_STATUS));
        else
          for (let E = 0; E < l; E++)
            C.insertAfter(P(A.NO_STATUS));
      }
      t.setRowSpan(1);
    }
  }
  const tt = o.createCommand("INSERT_TABLE_COMMAND");
  return T.$createGridSelection = q, T.$createTableCellNode = P, T.$createTableNode = Z, T.$createTableNodeWithDimensions = Be, T.$createTableRowNode = G, T.$deleteTableColumn = Ve, T.$deleteTableColumn__EXPERIMENTAL = Ze, T.$deleteTableRow__EXPERIMENTAL = Qe, T.$getElementGridForTableNode = He, T.$getTableCellNodeFromLexicalNode = Ye, T.$getTableColumnIndexFromTableCellNode = Xe, T.$getTableNodeFromLexicalNodeOrThrow = ie, T.$getTableRowIndexFromTableCellNode = We, T.$getTableRowNodeFromTableCellNodeOrThrow = le, T.$insertTableColumn = ze, T.$insertTableColumn__EXPERIMENTAL = qe, T.$insertTableRow = je, T.$insertTableRow__EXPERIMENTAL = Je, T.$isGridSelection = x, T.$isTableCellNode = R, T.$isTableNode = F, T.$isTableRowNode = L, T.$removeTableRowAtIndex = Ue, T.$unmergeCell = et, T.INSERT_TABLE_COMMAND = tt, T.TableCellHeaderStates = A, T.TableCellNode = B, T.TableNode = U, T.TableRowNode = Y, T.TableSelection = fe, T.applyTableHandlers = Fe, T.getCellFromTarget = ee, T.getTableSelectionFromTableElement = Le, T;
}
export {
  ht as __require
};
