import { c as ve, C as Ie, u as Ke, i as ye, k as $e, L as Ye, j as c, l as be, m as ze, I as Ne, E as Xe, n as Se, o as Be, q as Je, R as He, r as We, v as Fe, w as Ue } from "./main-C21LZUoC.js";
import { d as qe } from "./LexicalNestedComposer.prod-DxTOTjO8.js";
import { createEditor as Ge, $getNodeByKey as we, $addUpdateTag as P, $getRoot as F, $createParagraphNode as me, KEY_ESCAPE_COMMAND as Qe, $getSelection as K, $isRangeSelection as G, COMMAND_PRIORITY_LOW as $, KEY_ARROW_RIGHT_COMMAND as Ve, KEY_ARROW_LEFT_COMMAND as Ze, KEY_ARROW_DOWN_COMMAND as et, KEY_ARROW_UP_COMMAND as tt, KEY_TAB_COMMAND as nt, KEY_ENTER_COMMAND as lt, $isNodeSelection as De, FORMAT_TEXT_COMMAND as st, KEY_DELETE_COMMAND as rt, KEY_BACKSPACE_COMMAND as ot, CUT_COMMAND as Re, COPY_COMMAND as je, PASTE_COMMAND as Te, CLICK_COMMAND as ct, $createRangeSelection as it } from "lexical";
import { useRef as W, useState as H, useContext as _e, useMemo as fe, useEffect as V, useCallback as te } from "react";
import { createPortal as at } from "react-dom";
const Q = [];
function ut() {
  const n = it();
  return n.focus.set("root", F().getChildrenSize(), "element"), n;
}
function dt(n) {
  return `<p class="${n.paragraph}"><br></p>`;
}
function xe(n, l) {
  const a = n.querySelector(`[data-id=${l}]`);
  a != null && a.focus();
}
function ft(n) {
  return n.nodeType === 1 && n.hasAttribute("data-table-resize");
}
function mt(n, l) {
  const a = l.parseEditorState(n);
  let u = be.get(n);
  if (u === void 0) {
    u = a.read(() => ze(l, null));
    const E = a.read(() => F().getTextContent());
    be.set(n, u), ye.set(n, E);
  }
  return u;
}
function Ae(n) {
  const l = n.getRootElement();
  return l !== null ? l.ownerDocument : document;
}
function pt(n, l, a, u) {
  return l ? !1 : n === 67 ? Ne ? a : u : !1;
}
function ht(n, l, a, u) {
  return l ? !1 : n === 88 ? Ne ? a : u : !1;
}
function gt(n, l, a, u) {
  return l ? !1 : n === 86 ? Ne ? a : u : !1;
}
function Ce(n) {
  let l = n;
  for (; l !== null; ) {
    const a = l.getAttribute("data-id");
    if (a != null)
      return a;
    l = l.parentElement;
  }
  return null;
}
function xt(n) {
  let l = n;
  for (; l !== null; ) {
    if (l.nodeName === "TH" || l.nodeName === "TD")
      return l.getBoundingClientRect().width;
    l = l.parentElement;
  }
  return 0;
}
function se(n, l, a, u, E, y) {
  for (const C of l) {
    const T = pe(n, C, a);
    if (T !== null && u !== null) {
      const A = u.parseEditorState(T.json);
      u._headless = !0, u.setEditorState(A), u.update(y, { discrete: !0 }), u._headless = !1;
      const k = JSON.stringify(u.getEditorState());
      E((S) => {
        const [R, _] = a.get(C);
        P("history-push"), S.updateCellJSON(R, _, k);
      });
    }
  }
}
function Ct(n) {
  let l = n;
  for (; l !== null; ) {
    const a = l.nodeName;
    if (a === "BUTTON" || a === "INPUT" || a === "TEXTAREA")
      return !0;
    l = l.parentElement;
  }
  return !1;
}
function Oe(n, l, a) {
  const u = a.get(n), E = a.get(l);
  if (u === void 0 || E === void 0)
    return null;
  const y = Math.min(u[0], E[0]), C = Math.max(u[0], E[0]), T = Math.min(u[1], E[1]), A = Math.max(u[1], E[1]);
  return {
    endX: C,
    endY: A,
    startX: y,
    startY: T
  };
}
function Me(n, l, a, u) {
  const E = Oe(l, a, u);
  if (E === null)
    return [];
  const { startX: y, endY: C, endX: T, startY: A } = E, k = [];
  for (let S = y; S <= T; S++)
    for (let R = A; R <= C; R++)
      k.push(n[R].cells[S].id);
  return k;
}
function yt(n, l) {
  const { startX: a, endY: u, endX: E, startY: y } = l, C = [];
  for (let T = y; T <= u; T++) {
    const A = n[T], k = Fe();
    for (let S = a; S <= E; S++) {
      const R = { ...A.cells[S] };
      R.id = Ue(), k.cells.push(R);
    }
    C.push(k);
  }
  return C;
}
function bt({ cellEditor: n }) {
  const { cellEditorConfig: l, cellEditorPlugins: a } = _e(Ie);
  return a === null || l === null ? null : /* @__PURE__ */ c.jsx(
    qe,
    {
      initialEditor: n,
      initialTheme: l.theme,
      initialNodes: l.nodes,
      skipCollabChecks: !0,
      children: a
    }
  );
}
function pe(n, l, a) {
  const u = a.get(l);
  if (u === void 0)
    return null;
  const [E, y] = u;
  return n[y].cells[E];
}
function Nt({
  cell: n,
  rows: l,
  cellCoordMap: a,
  menuElem: u,
  updateCellsByID: E,
  onClose: y,
  updateTableNode: C,
  setSortingOptions: T,
  sortingOptions: A
}) {
  const k = W(null);
  V(() => {
    const e = k.current;
    if (e !== null) {
      const L = u.getBoundingClientRect();
      e.style.top = `${L.y}px`, e.style.left = `${L.x}px`;
    }
  }, [u]), V(() => {
    const e = (L) => {
      const U = k.current;
      U !== null && !U.contains(L.target) && L.stopPropagation();
    };
    return window.addEventListener("click", e), () => window.removeEventListener("click", e);
  }, [y]);
  const S = a.get(n.id);
  if (S === void 0)
    return null;
  const [R, _] = S;
  return (
    // eslint-disable-next-line jsx-a11y/no-static-element-interactions
    /* @__PURE__ */ c.jsxs(
      "div",
      {
        className: "dropdown",
        ref: k,
        onPointerMove: (e) => {
          e.stopPropagation();
        },
        onPointerDown: (e) => {
          e.stopPropagation();
        },
        onPointerUp: (e) => {
          e.stopPropagation();
        },
        onClick: (e) => {
          e.stopPropagation();
        },
        children: [
          /* @__PURE__ */ c.jsx(
            "button",
            {
              className: "item",
              onClick: () => {
                C((e) => {
                  P("history-push"), e.updateCellType(
                    R,
                    _,
                    n.type === "normal" ? "header" : "normal"
                  );
                }), y();
              },
              children: /* @__PURE__ */ c.jsx("span", { className: "text", children: n.type === "normal" ? "Make header" : "Remove header" })
            }
          ),
          /* @__PURE__ */ c.jsx(
            "button",
            {
              className: "item",
              onClick: () => {
                E([n.id], () => {
                  const e = F();
                  e.clear(), e.append(me());
                }), y();
              },
              children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Clear cell" })
            }
          ),
          /* @__PURE__ */ c.jsx("hr", { className: "editor-hr" }),
          n.type === "header" && _ === 0 && /* @__PURE__ */ c.jsxs(c.Fragment, { children: [
            A !== null && A.x === R && /* @__PURE__ */ c.jsx(
              "button",
              {
                className: "item",
                onClick: () => {
                  T(null), y();
                },
                children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Remove sorting" })
              }
            ),
            (A === null || A.x !== R || A.type === "descending") && /* @__PURE__ */ c.jsx(
              "button",
              {
                className: "item",
                onClick: () => {
                  T({ type: "ascending", x: R }), y();
                },
                children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Sort ascending" })
              }
            ),
            (A === null || A.x !== R || A.type === "ascending") && /* @__PURE__ */ c.jsx(
              "button",
              {
                className: "item",
                onClick: () => {
                  T({ type: "descending", x: R }), y();
                },
                children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Sort descending" })
              }
            ),
            /* @__PURE__ */ c.jsx("hr", { className: "editor-hr" })
          ] }),
          /* @__PURE__ */ c.jsx(
            "button",
            {
              className: "item",
              onClick: () => {
                C((e) => {
                  P("history-push"), e.insertRowAt(_);
                }), y();
              },
              children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Insert row above" })
            }
          ),
          /* @__PURE__ */ c.jsx(
            "button",
            {
              className: "item",
              onClick: () => {
                C((e) => {
                  P("history-push"), e.insertRowAt(_ + 1);
                }), y();
              },
              children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Insert row below" })
            }
          ),
          /* @__PURE__ */ c.jsx("hr", { className: "editor-hr" }),
          /* @__PURE__ */ c.jsx(
            "button",
            {
              className: "item",
              onClick: () => {
                C((e) => {
                  P("history-push"), e.insertColumnAt(R);
                }), y();
              },
              children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Insert column left" })
            }
          ),
          /* @__PURE__ */ c.jsx(
            "button",
            {
              className: "item",
              onClick: () => {
                C((e) => {
                  P("history-push"), e.insertColumnAt(R + 1);
                }), y();
              },
              children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Insert column right" })
            }
          ),
          /* @__PURE__ */ c.jsx("hr", {}),
          l[0].cells.length !== 1 && /* @__PURE__ */ c.jsx(
            "button",
            {
              className: "item",
              onClick: () => {
                C((e) => {
                  P("history-push"), e.deleteColumnAt(R);
                }), y();
              },
              children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Delete column" })
            }
          ),
          l.length !== 1 && /* @__PURE__ */ c.jsx(
            "button",
            {
              className: "item",
              onClick: () => {
                C((e) => {
                  P("history-push"), e.deleteRowAt(_);
                }), y();
              },
              children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Delete row" })
            }
          ),
          /* @__PURE__ */ c.jsx(
            "button",
            {
              className: "item",
              onClick: () => {
                C((e) => {
                  P("history-push"), e.selectNext(), e.remove();
                }), y();
              },
              children: /* @__PURE__ */ c.jsx("span", { className: "text", children: "Delete table" })
            }
          )
        ]
      }
    )
  );
}
function Et({
  cell: n,
  cellCoordMap: l,
  cellEditor: a,
  isEditing: u,
  isSelected: E,
  isPrimarySelected: y,
  theme: C,
  updateCellsByID: T,
  updateTableNode: A,
  rows: k,
  setSortingOptions: S,
  sortingOptions: R
}) {
  const [_, e] = H(!1), L = W(null), U = n.type !== "normal", re = n.json, he = U ? "th" : "td", oe = n.width, h = L.current, Y = l.get(n.id), q = R !== null && Y !== void 0 && Y[0] === R.x && Y[1] === 0;
  return V(() => {
    (u || !y) && e(!1);
  }, [u, y]), /* @__PURE__ */ c.jsxs(
    he,
    {
      className: `${C.tableCell} ${U ? C.tableCellHeader : ""} ${E ? C.tableCellSelected : ""}`,
      "data-id": n.id,
      tabIndex: -1,
      style: { width: oe !== null ? oe : void 0 },
      children: [
        y && /* @__PURE__ */ c.jsx(
          "div",
          {
            className: `${C.tableCellPrimarySelected} ${u ? C.tableCellEditing : ""}`
          }
        ),
        y && u ? /* @__PURE__ */ c.jsx(bt, { cellEditor: a }) : /* @__PURE__ */ c.jsxs(c.Fragment, { children: [
          /* @__PURE__ */ c.jsx(
            "div",
            {
              dangerouslySetInnerHTML: {
                __html: re === "" ? dt(C) : mt(re, a)
              }
            }
          ),
          /* @__PURE__ */ c.jsx("div", { className: C.tableCellResizer, "data-table-resize": "true" })
        ] }),
        y && !u && /* @__PURE__ */ c.jsx("div", { className: C.tableCellActionButtonContainer, ref: L, children: /* @__PURE__ */ c.jsx(
          "button",
          {
            className: C.tableCellActionButton,
            onClick: (ce) => {
              e(!_), ce.stopPropagation();
            },
            children: /* @__PURE__ */ c.jsx("i", { className: "chevron-down" })
          }
        ) }),
        _ && h !== null && at(
          /* @__PURE__ */ c.jsx(
            Nt,
            {
              cell: n,
              menuElem: h,
              updateCellsByID: T,
              onClose: () => e(!1),
              updateTableNode: A,
              cellCoordMap: l,
              rows: k,
              setSortingOptions: S,
              sortingOptions: R
            }
          ),
          document.body
        ),
        q && /* @__PURE__ */ c.jsx("div", { className: C.tableCellSortedIndicator })
      ]
    }
  );
}
function Tt({
  nodeKey: n,
  rows: l,
  theme: a
}) {
  const [u, E, y] = ve(n), C = W({
    point: 0,
    size: 0
  }), [T, A] = H(
    null
  ), k = W(null), S = W(null), R = W(null), { cellEditorConfig: _ } = _e(Ie), [e, L] = H(!1), [U, re] = H(!1), [he, oe] = H(!1), [h] = Ke(), Y = W(!1), [q, ce] = H(null), ne = W(null), w = fe(() => {
    const o = /* @__PURE__ */ new Map();
    for (let x = 0; x < l.length; x++) {
      const I = l[x].cells;
      for (let m = 0; m < I.length; m++) {
        const N = I[m];
        o.set(N.id, [m, x]);
      }
    }
    return o;
  }, [l]), D = fe(() => {
    if (T === null)
      return l;
    const o = l.slice(1);
    return o.sort((x, M) => {
      const I = x.cells, m = M.cells, N = T.x, v = ye.get(I[N].json) || "", j = ye.get(m[N].json) || "";
      return v === "" || j === "" ? 1 : T.type === "ascending" ? v.localeCompare(j) : j.localeCompare(v);
    }), o.unshift(l[0]), o;
  }, [l, T]), [d, Z] = H < null | string > null, b = fe(() => {
    if (_ === null)
      return null;
    const o = Ge({
      namespace: _.namespace,
      nodes: _.nodes,
      onError: (x) => _.onError(x, o),
      theme: _.theme
    });
    return o;
  }, [_]), [X, B] = H([]), Ee = fe(
    () => new Set(X),
    [X]
  );
  V(() => {
    const o = ne.current;
    u && document.activeElement === document.body && o !== null && o.focus();
  }, [u]);
  const O = te(
    (o) => {
      h.update(() => {
        const x = we(n);
        $e(x) && o(x);
      });
    },
    [h, n]
  ), ke = () => {
    O((o) => {
      P("history-push"), o.addColumns(1);
    });
  }, Pe = () => {
    O((o) => {
      P("history-push"), o.addRows(1);
    });
  }, ee = te(
    (o, x, M) => {
      const I = D[x].cells[o].id;
      if (S.current = I, M) {
        const m = Me(
          D,
          d,
          I,
          w
        );
        B(m);
      } else
        Z(I), B(Q), xe(ne.current, I);
    },
    [w, d, D]
  ), le = te(() => {
    if (b !== null && d !== null) {
      const o = JSON.stringify(b.getEditorState());
      O((x) => {
        const M = w.get(d);
        if (M === void 0)
          return;
        P("history-push");
        const [I, m] = M;
        x.updateCellJSON(I, m, o);
      });
    }
  }, [w, b, d, O]), ie = te(() => {
    setTimeout(() => {
      var x;
      const o = h.getRootElement();
      o !== null && (o.focus({ preventScroll: !0 }), (x = window.getSelection()) == null || x.removeAllRanges());
    }, 20);
  }, [h]);
  V(() => {
    const o = ne.current;
    if (o === null)
      return;
    const x = Ae(h), M = (j) => {
      const t = j.clientX - m.x, s = j.clientY - m.y;
      return t < 5 || s < 5;
    }, I = (j) => {
      const t = Ce(j.target);
      if (t !== null && h.isEditable() && o.contains(j.target)) {
        if (M(j)) {
          E(!0), Z(null), ie();
          return;
        }
        if (E(!1), ft(j.target)) {
          ce(t), o.style.userSelect = "none", C.current = {
            point: j.clientX,
            size: xt(j.target)
          };
          return;
        }
        Y.current = !0, d !== t ? (e && le(), Z(t), L(!1), S.current = t) : S.current = null, B(Q);
      } else d !== null && !Ct(j.target) && (E(!1), Y.current = !1, e && le(), Z(null), B(Q), L(!1), S.current = null);
    }, m = o.getBoundingClientRect(), N = (j) => {
      if (q !== null) {
        const s = R.current;
        if (s !== null) {
          const { size: r, point: f } = C.current, p = j.clientX - f, g = r + p;
          let i = j.clientX - m.x;
          i < 10 ? i = 10 : i > m.width - 10 ? i = m.width - 10 : g < 20 && (i = f - r + 20 - m.x), s.style.left = `${i}px`;
        }
        return;
      }
      if (!e) {
        const { clientX: s, clientY: r } = j, { width: f, x: p, y: g, height: i } = m, J = s > p + f * 0.9 && s < p + f + 40 && !Y.current;
        re(J);
        const z = j.target === k.current || r > g + i * 0.85 && r < g + i + 5 && !Y.current;
        oe(z);
      }
      if (e || !Y.current || d === null)
        return;
      const t = Ce(j.target);
      if (t !== null && t !== S.current) {
        X.length === 0 && (o.style.userSelect = "none");
        const s = Me(
          D,
          d,
          t,
          w
        );
        s.length === 1 ? B(Q) : B(s), S.current = t;
      }
    }, v = (j) => {
      var t;
      if (q !== null) {
        const { size: s, point: r } = C.current, f = j.clientX - r;
        let p = s + f;
        p < 10 && (p = 10), O((g) => {
          const [i] = w.get(q);
          P("history-push"), g.updateColumnWidth(i, p);
        }), ce(null);
      }
      o !== null && X.length > 1 && Y.current && (o.style.userSelect = "text", (t = window.getSelection()) == null || t.removeAllRanges()), Y.current = !1;
    };
    return x.addEventListener("pointerdown", I), x.addEventListener("pointermove", N), x.addEventListener("pointerup", v), () => {
      x.removeEventListener("pointerdown", I), x.removeEventListener("pointermove", N), x.removeEventListener("pointerup", v);
    };
  }, [
    b,
    h,
    e,
    D,
    le,
    d,
    Ee,
    X,
    w,
    q,
    O,
    E,
    ie
  ]), V(() => {
    if (!e && d !== null) {
      const o = Ae(h), x = (m) => {
        if (m !== null && b !== null) {
          const N = m.json, v = b.parseEditorState(N);
          b.setEditorState(v);
        }
      }, M = (m) => {
        const N = Ce(m.target);
        if (N === d && h.isEditable()) {
          const v = pe(D, N, w);
          x(v), L(!0), B(Q);
        }
      }, I = (m) => {
        const N = m.keyCode;
        if (N === 16 || N === 27 || N === 9 || N === 37 || N === 38 || N === 39 || N === 40 || N === 8 || N === 46 || !h.isEditable())
          return;
        if (N === 13 && m.preventDefault(), !e && d !== null && h.getEditorState().read(() => K() === null) && m.target.contentEditable !== "true") {
          if (pt(N, m.shiftKey, m.metaKey, m.ctrlKey)) {
            h.dispatchCommand(je, m);
            return;
          }
          if (ht(N, m.shiftKey, m.metaKey, m.ctrlKey)) {
            h.dispatchCommand(Re, m);
            return;
          }
          if (gt(N, m.shiftKey, m.metaKey, m.ctrlKey)) {
            h.dispatchCommand(Te, m);
            return;
          }
        }
        if (m.metaKey || m.ctrlKey || m.altKey)
          return;
        const v = pe(D, d, w);
        x(v), L(!0), B(Q);
      };
      return o.addEventListener("dblclick", M), o.addEventListener("keydown", I), () => {
        o.removeEventListener("dblclick", M), o.removeEventListener("keydown", I);
      };
    }
  }, [
    b,
    h,
    e,
    D,
    d,
    w
  ]);
  const ge = te(
    (o, x) => {
      se(D, o, w, b, O, x);
    },
    [w, b, D, O]
  ), ae = te(() => d !== null && !e ? (ge([d, ...X], () => {
    const o = F();
    o.clear(), o.append(me());
  }), !0) : (u && O((o) => {
    P("history-push"), o.selectNext(), o.remove();
  }), !1), [
    e,
    u,
    d,
    X,
    ge,
    O
  ]);
  if (V(() => {
    const o = ne.current;
    if (o === null)
      return;
    const x = (t, s, r, f) => {
      const p = t instanceof KeyboardEvent ? null : t.clipboardData;
      if (t.preventDefault(), p != null)
        p.setData("text/html", s), p.setData("text/plain", f), p.setData("application/x-lexical-editor", r);
      else {
        const g = navigator.clipboard;
        if (g != null) {
          const i = [
            new ClipboardItem({
              "text/html": new Blob([s], {
                type: "text/html"
              })
            })
          ];
          g.write(i);
        }
      }
    }, M = async (t, s) => {
      try {
        return t instanceof DataTransfer ? t.getData(s) : t instanceof ClipboardItem ? await (await t.getType(s)).text() : "";
      } catch {
        return "";
      }
    }, I = async (t) => {
      let s = (t instanceof InputEvent ? null : t.clipboardData) || null;
      if (d !== null && b !== null) {
        if (t.preventDefault(), s === null)
          try {
            s = (await navigator.clipboard.read())[0];
          } catch {
          }
        const r = s !== null ? await M(
          s,
          "application/x-lexical-editor"
        ) : "";
        if (r)
          try {
            const g = JSON.parse(r);
            if (g.namespace === h._config.namespace && Array.isArray(g.nodes)) {
              se(
                D,
                [d],
                w,
                b,
                O,
                () => {
                  const i = F();
                  i.clear(), i.append(me()), i.selectEnd();
                  const J = Xe(
                    g.nodes
                  ), z = K();
                  G(z) && Se(b, J, z);
                }
              );
              return;
            }
          } catch {
          }
        const f = s !== null ? await M(s, "text/html") : "";
        if (f)
          try {
            const i = new DOMParser().parseFromString(f, "text/html"), J = i.querySelector("table");
            if (J != null) {
              const z = Be(J);
              O((ue) => {
                const [de, Le] = w.get(d);
                P("history-push"), ue.mergeRows(de, Le, z);
              });
              return;
            }
            se(
              D,
              [d],
              w,
              b,
              O,
              () => {
                const z = F();
                z.clear(), z.append(me()), z.selectEnd();
                const ue = Je(h, i), de = K();
                G(de) && Se(b, ue, de);
              }
            );
            return;
          } catch {
          }
        const p = s !== null ? await M(s, "text/plain") : "";
        p != null && se(
          D,
          [d],
          w,
          b,
          O,
          () => {
            const g = F();
            g.clear(), g.selectEnd();
            const i = K();
            i !== null && i.insertRawText(p);
          }
        );
      }
    }, m = (t) => {
      if (d !== null && b !== null) {
        const r = pe(D, d, w).json, f = be.get(r) || null;
        if (f === null)
          return;
        const p = b.parseEditorState(r), g = p.read(
          () => F().getTextContent()
        ), i = p.read(() => JSON.stringify(
          He(b, null)
        ));
        x(t, f, i, g);
      }
    }, N = (t) => {
      const s = S.current;
      if (d !== null && b !== null && s !== null) {
        const r = Oe(
          d,
          s,
          w
        );
        if (r === null)
          return;
        const f = We(D, r), p = f.outerHTML, g = f.outerText, i = h.getEditorState().read(() => we(n).exportJSON());
        i.rows = yt(D, r);
        const J = {
          namespace: b._config.namespace,
          nodes: [i]
        }, z = JSON.stringify(J);
        x(t, p, z, g);
      }
    }, v = (t, s) => {
      const r = K();
      return d !== null && !e && r === null && s === h ? (I(t), Y.current = !1, B(Q), !0) : !1;
    }, j = (t, s) => {
      const r = K();
      return d !== null && !e && r === null && s === h ? (X.length === 0 ? m(t) : N(t), !0) : !1;
    };
    return Ye(
      h.registerCommand(
        ct,
        (t) => {
          const s = K();
          return !!De(s);
        },
        $
      ),
      h.registerCommand(
        Te,
        v,
        $
      ),
      h.registerCommand(
        je,
        j,
        $
      ),
      h.registerCommand(
        Re,
        (t, s) => j(t, s) ? (ae(), !0) : !1,
        $
      ),
      h.registerCommand(
        ot,
        ae,
        $
      ),
      h.registerCommand(
        rt,
        ae,
        $
      ),
      h.registerCommand(
        st,
        (t) => d !== null && !e ? (se(
          D,
          [d, ...X],
          w,
          b,
          O,
          () => {
            ut().formatText(t);
          }
        ), !0) : !1,
        $
      ),
      h.registerCommand(
        lt,
        (t, s) => {
          const r = K();
          if (d === null && !e && De(r) && r.has(n) && r.getNodes().length === 1 && s === h) {
            const f = D[0].cells[0].id;
            return Z(f), xe(o, f), t.preventDefault(), t.stopPropagation(), y(), !0;
          }
          return !1;
        },
        $
      ),
      h.registerCommand(
        nt,
        (t) => {
          const s = K();
          if (!e && s === null && d !== null) {
            const r = t.shiftKey, [f, p] = w.get(d);
            t.preventDefault();
            let g = null, i = null;
            if (f === 0 && r ? p !== 0 && (i = p - 1, g = D[i].cells.length - 1) : f === D[p].cells.length - 1 && !r ? p !== D.length - 1 && (i = p + 1, g = 0) : r ? (g = f - 1, i = p) : (g = f + 1, i = p), g !== null && i !== null)
              return ee(g, i, !1), !0;
          }
          return !1;
        },
        $
      ),
      h.registerCommand(
        tt,
        (t, s) => {
          const r = K();
          if (!e && r === null) {
            const f = t.shiftKey, p = f && S.current || d;
            if (p !== null) {
              const [g, i] = w.get(p);
              if (i !== 0)
                return ee(g, i - 1, f), !0;
            }
          }
          return !G(r) || s !== b ? !1 : r.isCollapsed() && r.anchor.getNode().getTopLevelElementOrThrow().getPreviousSibling() === null ? (t.preventDefault(), !0) : !1;
        },
        $
      ),
      h.registerCommand(
        et,
        (t, s) => {
          const r = K();
          if (!e && r === null) {
            const f = t.shiftKey, p = f && S.current || d;
            if (p !== null) {
              const [g, i] = w.get(p);
              if (i !== D.length - 1)
                return ee(g, i + 1, f), !0;
            }
          }
          return !G(r) || s !== b ? !1 : r.isCollapsed() && r.anchor.getNode().getTopLevelElementOrThrow().getNextSibling() === null ? (t.preventDefault(), !0) : !1;
        },
        $
      ),
      h.registerCommand(
        Ze,
        (t, s) => {
          const r = K();
          if (!e && r === null) {
            const f = t.shiftKey, p = f && S.current || d;
            if (p !== null) {
              const [g, i] = w.get(p);
              if (g !== 0)
                return ee(g - 1, i, f), !0;
            }
          }
          return !G(r) || s !== b ? !1 : r.isCollapsed() && r.anchor.offset === 0 ? (t.preventDefault(), !0) : !1;
        },
        $
      ),
      h.registerCommand(
        Ve,
        (t, s) => {
          const r = K();
          if (!e && r === null) {
            const f = t.shiftKey, p = f && S.current || d;
            if (p !== null) {
              const [g, i] = w.get(p);
              if (g !== D[i].cells.length - 1)
                return ee(g + 1, i, f), !0;
            }
          }
          if (!G(r) || s !== b)
            return !1;
          if (r.isCollapsed()) {
            const f = r.anchor;
            if (f.type === "text" && f.offset === f.getNode().getTextContentSize() || f.type === "element" && f.offset === f.getNode().getChildrenSize())
              return t.preventDefault(), !0;
          }
          return !1;
        },
        $
      ),
      h.registerCommand(
        Qe,
        (t, s) => {
          const r = K();
          return !e && r === null && s === h ? (E(!0), Z(null), ie(), !0) : G(r) && e ? (le(), L(!1), d !== null && setTimeout(() => {
            xe(o, d);
          }, 20), !0) : !1;
        },
        $
      )
    );
  }, [
    w,
    b,
    ae,
    y,
    h,
    e,
    ee,
    n,
    d,
    D,
    le,
    ie,
    X,
    E,
    O
  ]), b !== null)
    return /* @__PURE__ */ c.jsxs("div", { style: { position: "relative" }, children: [
      /* @__PURE__ */ c.jsx(
        "table",
        {
          className: `${a.table} ${u ? a.tableSelected : ""}`,
          ref: ne,
          tabIndex: -1,
          children: /* @__PURE__ */ c.jsx("tbody", { children: D.map((o) => /* @__PURE__ */ c.jsx("tr", { className: a.tableRow, children: o.cells.map((x) => {
            const { id: M } = x;
            return /* @__PURE__ */ c.jsx(
              Et,
              {
                cell: x,
                theme: a,
                isSelected: Ee.has(M),
                isPrimarySelected: d === M,
                isEditing: e,
                sortingOptions: T,
                cellEditor: b,
                updateCellsByID: ge,
                updateTableNode: O,
                cellCoordMap: w,
                rows: D,
                setSortingOptions: A
              },
              M
            );
          }) }, o.id)) })
        }
      ),
      U && /* @__PURE__ */ c.jsx("button", { className: a.tableAddColumns, onClick: ke }),
      he && /* @__PURE__ */ c.jsx(
        "button",
        {
          className: a.tableAddRows,
          onClick: Pe,
          ref: k
        }
      ),
      q !== null && /* @__PURE__ */ c.jsx("div", { className: a.tableResizeRuler, ref: R })
    ] });
}
export {
  Tt as default
};
