import { f as q, g as X } from "./editorContainerHelpers-C7kdWnS0.mjs";
let h = null, m = null, R = null, x = null, I = null, M = null, S = null;
const C = '[data-theme="dark"], .dark, .editora-theme-dark';
let w = !1, v = null, V = 0, L = 0, E = !1, k = 0, A = 0, B = 0, D = 0;
const _ = () => {
  const e = q(), t = X(e);
  if (!t)
    return alert("Please place your cursor in the editor before inserting a table"), !1;
  const n = window.getSelection();
  if (!n || n.rangeCount === 0) return;
  const l = n.getRangeAt(0), r = document.createElement("table");
  r.className = "rte-table";
  const o = document.createElement("thead"), a = document.createElement("tr");
  for (let d = 0; d < 3; d++) {
    const c = document.createElement("th"), u = document.createElement("p");
    u.appendChild(document.createElement("br")), c.appendChild(u), a.appendChild(c);
  }
  o.appendChild(a);
  const s = document.createElement("tbody");
  for (let d = 0; d < 2; d++) {
    const c = document.createElement("tr");
    for (let u = 0; u < 3; u++) {
      const b = document.createElement("td"), f = document.createElement("p");
      f.appendChild(document.createElement("br")), b.appendChild(f), c.appendChild(b);
    }
    s.appendChild(c);
  }
  r.appendChild(o), r.appendChild(s), l.deleteContents(), l.insertNode(r);
  const i = r.querySelector("th p");
  if (i) {
    const d = document.createRange();
    d.setStart(i, 0), d.collapse(!0), n.removeAllRanges(), n.addRange(d);
  }
  t.focus();
}, P = () => {
  var a;
  const e = p();
  if (!e) return;
  const { table: t, rowIndex: n } = e, l = document.createElement("tr"), r = ((a = t.rows[0]) == null ? void 0 : a.cells.length) || 0;
  for (let s = 0; s < r; s++) {
    const i = document.createElement("td"), d = document.createElement("p");
    d.innerHTML = "<br>", i.appendChild(d), l.appendChild(i);
  }
  const o = t.rows[n];
  o && o.parentElement ? o.parentElement.insertBefore(l, o) : t.appendChild(l), g();
}, z = () => {
  var o;
  const e = p();
  if (!e) return;
  const { table: t, rowIndex: n } = e, l = document.createElement("tr"), r = ((o = t.rows[0]) == null ? void 0 : o.cells.length) || 0;
  for (let a = 0; a < r; a++) {
    const s = document.createElement("td"), i = document.createElement("p");
    i.innerHTML = "<br>", s.appendChild(i), l.appendChild(s);
  }
  n >= t.rows.length - 1 ? t.appendChild(l) : t.insertBefore(l, t.rows[n + 1]), g();
}, W = () => {
  const e = p();
  if (!e) return;
  const { table: t, colIndex: n } = e;
  for (let l = 0; l < t.rows.length; l++) {
    const r = t.rows[l], o = document.createElement("td"), a = document.createElement("p");
    a.innerHTML = "<br>", o.appendChild(a), n === 0 ? r.insertBefore(o, r.cells[0]) : r.insertBefore(o, r.cells[n]);
  }
  g();
}, T = () => {
  const e = p();
  if (!e) return;
  const { table: t, colIndex: n } = e;
  for (let l = 0; l < t.rows.length; l++) {
    const r = t.rows[l], o = document.createElement("td"), a = document.createElement("p");
    a.innerHTML = "<br>", o.appendChild(a), n >= r.cells.length - 1 ? r.appendChild(o) : r.insertBefore(o, r.cells[n + 1]);
  }
  g();
}, O = () => {
  const e = p();
  if (!e || e.rowCount <= 1) return;
  const { table: t, rowIndex: n } = e;
  t.deleteRow(n), g();
}, K = () => {
  const e = p();
  if (!e || e.cellCount <= 1) return;
  const { table: t, colIndex: n } = e;
  for (let l = 0; l < t.rows.length; l++) {
    const r = t.rows[l];
    r.cells[n] && r.deleteCell(n);
  }
  g();
}, Y = () => {
  var o;
  const e = p();
  if (!e) return;
  const { table: t, rowIndex: n } = e, l = t.rows[n];
  if (((o = l.parentElement) == null ? void 0 : o.tagName.toLowerCase()) === "thead") {
    const a = t.querySelector("tbody") || t.appendChild(document.createElement("tbody")), s = t.querySelector("thead");
    s && (a.insertBefore(l, a.firstChild), s.rows.length === 0 && s.remove());
  } else {
    let a = t.querySelector("thead");
    a || (a = document.createElement("thead"), t.insertBefore(a, t.firstChild)), a.appendChild(l);
  }
  g();
}, F = () => {
  var l;
  const e = p();
  if (!e) return;
  const { table: t, colIndex: n } = e;
  for (let r = 0; r < t.rows.length; r++) {
    const o = t.rows[r].cells[n];
    if (o) {
      const a = o.tagName.toLowerCase() === "th" ? "td" : "th", s = document.createElement(a);
      s.innerHTML = o.innerHTML;
      for (let i = 0; i < o.attributes.length; i++) {
        const d = o.attributes[i];
        s.setAttribute(d.name, d.value);
      }
      (l = o.parentNode) == null || l.replaceChild(s, o);
    }
  }
  g();
}, U = () => {
  const e = p();
  if (!e) return;
  e.table.remove(), document.dispatchEvent(new CustomEvent("tableDeleted"));
}, j = () => {
  var u, b;
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return;
  const n = e.getRangeAt(0).startContainer;
  if (!(n.nodeType === Node.TEXT_NODE ? (u = n.parentElement) == null ? void 0 : u.closest("table") : n.closest("table"))) return;
  let r = null;
  if (n.nodeType === Node.TEXT_NODE ? r = (b = n.parentElement) == null ? void 0 : b.closest("td, th") : n.nodeType === Node.ELEMENT_NODE && (r = n.closest("td, th")), !r) return;
  const o = r.parentElement;
  if (!o) return;
  let a = -1;
  for (let f = 0; f < o.cells.length; f++)
    if (o.cells[f] === r) {
      a = f;
      break;
    }
  if (a === -1 || a === o.cells.length - 1) return;
  const s = o.cells[a + 1];
  if (!s) return;
  const i = parseInt(r.getAttribute("colspan") || "1"), d = parseInt(s.getAttribute("colspan") || "1");
  r.setAttribute("colspan", String(i + d)), Array.from(s.childNodes).forEach((f) => {
    r.appendChild(f);
  }), s.remove(), g();
};
function p() {
  var i, d, c;
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return null;
  const n = e.getRangeAt(0).startContainer;
  let l = n.nodeType === Node.TEXT_NODE ? (i = n.parentElement) == null ? void 0 : i.closest("table") : n.closest("table");
  if (!l) return null;
  const r = l;
  let o = 0, a = 0;
  const s = n.nodeType === Node.TEXT_NODE ? (d = n.parentElement) == null ? void 0 : d.closest("td, th") : n.closest("td, th");
  if (s) {
    let u = s.parentElement;
    for (; u && u !== r.rows[o] && (o++, !(o >= r.rows.length)); )
      ;
    const b = u;
    if (b) {
      for (let f = 0; f < b.cells.length; f++)
        if (b.cells[f] === s) {
          a = f;
          break;
        }
    }
  }
  return {
    table: r,
    rowIndex: o,
    colIndex: a,
    rowCount: r.rows.length,
    cellCount: ((c = r.rows[0]) == null ? void 0 : c.cells.length) || 0
  };
}
function g() {
  if (!h || !m) return;
  const e = p();
  if (!e) return;
  const t = e.rowCount > 1, n = e.cellCount > 1;
  N(t, n);
}
function G() {
  R = () => {
    const e = p();
    e ? J(e.table) : y();
  }, x = (e) => {
    const t = e.target, n = t.closest("table"), l = t.closest(".table-toolbar");
    !n && !l && y();
  }, I = () => {
    y();
  }, M = () => {
    m && h && h.style.display !== "none" && H(m);
  }, S = () => {
    m && h && h.style.display !== "none" && H(m);
  }, document.addEventListener("selectionchange", R), document.addEventListener("mousedown", x), document.addEventListener("tableDeleted", I), window.addEventListener("scroll", M, !0), window.addEventListener("resize", S);
}
function H(e) {
  if (!h) return;
  const t = e.getBoundingClientRect(), n = h.getBoundingClientRect(), l = n.height || 40, r = n.width || 280, o = 10;
  let a = t.top - l - o, s = t.left + t.width / 2 - r / 2;
  a < o && (a = t.bottom + o), s < o && (s = o);
  const i = window.innerWidth;
  s + r > i - o && (s = i - r - o);
  const d = window.innerHeight;
  a + l > d - o && (a = d - l - o), h.style.top = a + "px", h.style.left = s + "px";
}
function J(e) {
  m = e, h || (h = Q(), document.body.appendChild(h));
  const t = !!e.closest(C) || document.body.matches(C) || document.documentElement.matches(C);
  h.classList.toggle("rte-theme-dark", t), h.style.display = "flex", h.style.visibility = "hidden", requestAnimationFrame(() => {
    H(e), h && (h.style.visibility = "visible");
  });
  const n = p();
  n && N(n.rowCount > 1, n.cellCount > 1), $(e);
}
function y() {
  if (h && (h.style.display = "none"), m) {
    m.querySelectorAll(".resize-handle").forEach((n) => n.remove());
    const t = m.querySelector(".table-resize-handle");
    t && t.remove();
  }
  m = null;
}
function N(e, t) {
  if (!h) return;
  const n = h.querySelector('[data-action="deleteRow"]'), l = h.querySelector('[data-action="deleteColumn"]');
  n && (n.disabled = !e), l && (l.disabled = !t);
}
function Q() {
  const e = document.createElement("div");
  e.className = "table-toolbar", e.style.cssText = `
    position: fixed;
    z-index: 1000;
    display: none;
  `, e.setAttribute("role", "toolbar"), e.setAttribute("aria-label", "Table editing toolbar");
  const t = (c) => {
    const u = document.createElement("button");
    return u.className = "toolbar-icon-btn", c.danger && u.classList.add("toolbar-icon-btn-danger"), c.delete && u.classList.add("toolbar-icon-btn-delete"), u.innerHTML = c.icon, u.title = c.title, u.setAttribute("aria-label", c.title), u.setAttribute("type", "button"), u.setAttribute("data-action", c.action), u.onclick = () => Z(c.action), u;
  }, n = () => {
    const c = document.createElement("div");
    return c.className = "toolbar-divider", c;
  }, l = (...c) => {
    const u = document.createElement("div");
    return u.className = "toolbar-section", c.forEach((b) => u.appendChild(b)), u;
  }, r = l(
    t({
      icon: ne(),
      title: "Add row above (Ctrl+Shift+R)",
      action: "addRowAbove"
    }),
    t({
      icon: oe(),
      title: "Add row below",
      action: "addRowBelow"
    }),
    t({
      icon: le(),
      title: "Delete row",
      action: "deleteRow",
      danger: !0
    })
  ), o = l(
    t({
      icon: re(),
      title: "Add column left",
      action: "addColumnLeft"
    }),
    t({
      icon: ae(),
      title: "Add column right (Ctrl+Shift+C)",
      action: "addColumnRight"
    }),
    t({
      icon: se(),
      title: "Delete column",
      action: "deleteColumn",
      danger: !0
    })
  ), a = l(
    t({
      icon: ie(),
      title: "Toggle header row",
      action: "toggleHeaderRow"
    }),
    t({
      icon: de(),
      title: "Toggle header column",
      action: "toggleHeaderColumn"
    })
  ), s = l(
    t({
      icon: ue(),
      title: "Merge cells (horizontally)",
      action: "mergeCells"
    })
  ), i = l(
    t({
      icon: ce(),
      title: "Delete table",
      action: "deleteTable",
      delete: !0
    })
  );
  e.appendChild(r), e.appendChild(n()), e.appendChild(o), e.appendChild(n()), e.appendChild(a), e.appendChild(n()), e.appendChild(s), e.appendChild(n()), e.appendChild(i);
  const d = (c) => {
    !h || h.style.display === "none" || (c.ctrlKey || c.metaKey) && c.shiftKey && (c.key === "r" || c.key === "R" ? (c.preventDefault(), z()) : (c.key === "c" || c.key === "C") && (c.preventDefault(), T()));
  };
  return window.addEventListener("keydown", d), e;
}
function Z(e) {
  switch (e) {
    case "addRowAbove":
      P();
      break;
    case "addRowBelow":
      z();
      break;
    case "addColumnLeft":
      W();
      break;
    case "addColumnRight":
      T();
      break;
    case "deleteRow":
      O();
      break;
    case "deleteColumn":
      K();
      break;
    case "toggleHeaderRow":
      Y();
      break;
    case "toggleHeaderColumn":
      F();
      break;
    case "deleteTable":
      U();
      break;
    case "mergeCells":
      j();
      break;
  }
}
function $(e) {
  e.querySelectorAll(".resize-handle").forEach((a) => a.remove());
  const n = e.querySelector(".table-resize-handle");
  n && n.remove();
  const l = e.querySelector("thead tr, tbody tr:first-child");
  if (!l) return;
  const r = l.querySelectorAll("td, th");
  r.forEach((a, s) => {
    if (s === r.length - 1) return;
    const i = document.createElement("div");
    i.className = "resize-handle", i.style.cssText = `
      position: absolute;
      right: -4px;
      top: 0;
      bottom: 0;
      width: 8px;
      background: transparent;
      cursor: col-resize;
      z-index: 10;
      transition: background 0.15s ease;
    `, i.addEventListener("mouseenter", () => {
      w || (i.style.background = "rgba(0, 102, 204, 0.3)");
    }), i.addEventListener("mouseleave", () => {
      w || (i.style.background = "transparent");
    }), i.addEventListener("mousedown", (d) => {
      d.preventDefault(), d.stopPropagation(), ee(d, s);
    }), a.style.position = "relative", a.appendChild(i);
  });
  const o = document.createElement("div");
  o.className = "table-resize-handle", o.addEventListener("mousedown", (a) => {
    a.preventDefault(), a.stopPropagation(), te(a);
  }), e.appendChild(o);
}
function ee(e, t) {
  if (w = !0, v = t, V = e.clientX, !m) return;
  const n = m.querySelector("thead tr, tbody tr:first-child");
  n && n.cells[t] && (L = n.cells[t].offsetWidth), document.body.style.cursor = "col-resize", document.body.style.userSelect = "none";
  const l = (o) => {
    if (!w || v === null || !m) return;
    const a = o.clientX - V, s = Math.max(50, L + a);
    m.querySelectorAll("tr").forEach((d) => {
      d.cells[v] && (d.cells[v].style.width = s + "px");
    });
  }, r = () => {
    w = !1, v = null, document.body.style.cursor = "", document.body.style.userSelect = "", document.removeEventListener("mousemove", l), document.removeEventListener("mouseup", r);
  };
  document.addEventListener("mousemove", l), document.addEventListener("mouseup", r);
}
function te(e) {
  if (!m) return;
  E = !0, k = e.clientX, A = e.clientY, B = m.offsetWidth, D = m.offsetHeight, document.body.style.cursor = "nwse-resize", document.body.style.userSelect = "none";
  const t = (l) => {
    if (!E || !m) return;
    const r = l.clientX - k, o = l.clientY - A, a = Math.max(200, B + r), s = Math.max(100, D + o);
    m.style.width = a + "px", m.style.height = s + "px";
  }, n = () => {
    E = !1, document.body.style.cursor = "", document.body.style.userSelect = "", document.removeEventListener("mousemove", t), document.removeEventListener("mouseup", n);
  };
  document.addEventListener("mousemove", t), document.addEventListener("mouseup", n);
}
function ne() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M2 7h12V5H2v2zm0 4h12V9H2v2zM8 1v3H5v2h3v3h2V6h3V4h-3V1H8z"/>
  </svg>`;
}
function oe() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M2 3h12V1H2v2zm0 4h12V5H2v2zm6 4v3h3v-2h2v-2h-2v-3h-2v3H5v2h3z"/>
  </svg>`;
}
function le() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M2 5h12v2H2V5zm0 4h12v2H2V9zm4-6v2H4v2h2v2h2V7h2V5H8V3H6z"/>
  </svg>`;
}
function re() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M7 2v12h2V2H7zm4 0v12h2V2h-2zM1 8h3v-3H1v3zm3 2H1v3h3v-3z"/>
  </svg>`;
}
function ae() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M2 2v12h2V2H2zm4 0v12h2V2H6zM12 8h3v-3h-3v3zm0 2h3v3h-3v-3z"/>
  </svg>`;
}
function se() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M5 2v12h2V2H5zm4 0v12h2V2H9zm3 2h3V1h-3v3zm3 2h-3v3h3V6zm0 4h-3v3h3v-3z"/>
  </svg>`;
}
function ie() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M2 2h12v3H2V2zm0 5h12v8H2V7zm2 2v4h2V9H4zm4 0v4h2V9H8zm4 0v4h2V9h-2z"/>
  </svg>`;
}
function de() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M2 2v12h3V2H2zm5 0v12h8V2H7zm2 2h4v2H9V4zm0 4h4v2H9V8zm0 4h4v2H9v-2z"/>
  </svg>`;
}
function ce() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M3 1h10v1H3V1zm1 2v11h8V3H4zM6 5h1v6H6V5zm3 0h1v6H9V5z"/>
  </svg>`;
}
function ue() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M2 2h4v3H2V2zm5 0h4v3H7V2zm5 0h2v3h-2V2zm-10 4h4v3H2V6zm5 0h4v3H7V6zm5 0h2v3h-2V6zm-10 4h4v3H2v-3zm5 0h4v3H7v-3zm5 0h2v3h-2v-3z"/>
  </svg>`;
}
if (typeof window != "undefined" && !window.__tablePluginInitialized) {
  window.__tablePluginInitialized = !0;
  const e = () => {
    G();
  };
  document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", e) : setTimeout(e, 100);
}
const me = () => ({
  name: "table",
  toolbar: [
    {
      label: "Insert Table",
      command: "insertTable",
      icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" focusable="false" aria-hidden="true"><rect x="3" y="4" width="18" height="16" rx="2" fill="none" stroke="currentColor" stroke-width="1.8"/><path d="M3 10h18M3 15h18M9 4v16M15 4v16" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/></svg>'
    }
  ],
  commands: {
    insertTable: () => (_(), !0)
  },
  keymap: {
    "Mod-Shift-r": () => (z(), !0),
    "Mod-Shift-c": () => (T(), !0)
  }
});
export {
  me as TablePlugin,
  W as addColumnLeftCommand,
  T as addColumnRightCommand,
  P as addRowAboveCommand,
  z as addRowBelowCommand,
  K as deleteColumnCommand,
  O as deleteRowCommand,
  U as deleteTableCommand,
  _ as insertTableCommand,
  j as mergeCellsCommand,
  F as toggleHeaderColumnCommand,
  Y as toggleHeaderRowCommand
};
//# sourceMappingURL=TablePlugin.native-DrHX-efe.mjs.map
