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 w = '[data-theme="dark"], .dark, .editora-theme-dark';
let g = !1, b = null, V = 0, L = 0, C = !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 r = n.getRangeAt(0), o = document.createElement("table");
  o.className = "rte-table";
  const l = document.createElement("thead"), a = document.createElement("tr");
  for (let c = 0; c < 3; c++) {
    const i = document.createElement("th"), u = document.createElement("p");
    u.appendChild(document.createElement("br")), i.appendChild(u), a.appendChild(i);
  }
  l.appendChild(a);
  const s = document.createElement("tbody");
  for (let c = 0; c < 2; c++) {
    const i = document.createElement("tr");
    for (let u = 0; u < 3; u++) {
      const v = document.createElement("td"), T = document.createElement("p");
      T.appendChild(document.createElement("br")), v.appendChild(T), i.appendChild(v);
    }
    s.appendChild(i);
  }
  o.appendChild(l), o.appendChild(s), r.deleteContents(), r.insertNode(o);
  const d = o.querySelector("th p");
  if (d) {
    const c = document.createRange();
    c.setStart(d, 0), c.collapse(!0), n.removeAllRanges(), n.addRange(c);
  }
  t.focus();
}, P = () => {
  const e = f();
  if (!e) return;
  const { table: t, rowIndex: n } = e, r = document.createElement("tr"), o = t.rows[0]?.cells.length || 0;
  for (let a = 0; a < o; a++) {
    const s = document.createElement("td"), d = document.createElement("p");
    d.innerHTML = "<br>", s.appendChild(d), r.appendChild(s);
  }
  const l = t.rows[n];
  l && l.parentElement ? l.parentElement.insertBefore(r, l) : t.appendChild(r), p();
}, H = () => {
  const e = f();
  if (!e) return;
  const { table: t, rowIndex: n } = e, r = document.createElement("tr"), o = t.rows[0]?.cells.length || 0;
  for (let l = 0; l < o; l++) {
    const a = document.createElement("td"), s = document.createElement("p");
    s.innerHTML = "<br>", a.appendChild(s), r.appendChild(a);
  }
  n >= t.rows.length - 1 ? t.appendChild(r) : t.insertBefore(r, t.rows[n + 1]), p();
}, W = () => {
  const e = f();
  if (!e) return;
  const { table: t, colIndex: n } = e;
  for (let r = 0; r < t.rows.length; r++) {
    const o = t.rows[r], l = document.createElement("td"), a = document.createElement("p");
    a.innerHTML = "<br>", l.appendChild(a), n === 0 ? o.insertBefore(l, o.cells[0]) : o.insertBefore(l, o.cells[n]);
  }
  p();
}, z = () => {
  const e = f();
  if (!e) return;
  const { table: t, colIndex: n } = e;
  for (let r = 0; r < t.rows.length; r++) {
    const o = t.rows[r], l = document.createElement("td"), a = document.createElement("p");
    a.innerHTML = "<br>", l.appendChild(a), n >= o.cells.length - 1 ? o.appendChild(l) : o.insertBefore(l, o.cells[n + 1]);
  }
  p();
}, O = () => {
  const e = f();
  if (!e || e.rowCount <= 1) return;
  const { table: t, rowIndex: n } = e;
  t.deleteRow(n), p();
}, K = () => {
  const e = f();
  if (!e || e.cellCount <= 1) return;
  const { table: t, colIndex: n } = e;
  for (let r = 0; r < t.rows.length; r++) {
    const o = t.rows[r];
    o.cells[n] && o.deleteCell(n);
  }
  p();
}, Y = () => {
  const e = f();
  if (!e) return;
  const { table: t, rowIndex: n } = e, r = t.rows[n];
  if (r.parentElement?.tagName.toLowerCase() === "thead") {
    const l = t.querySelector("tbody") || t.appendChild(document.createElement("tbody")), a = t.querySelector("thead");
    a && (l.insertBefore(r, l.firstChild), a.rows.length === 0 && a.remove());
  } else {
    let l = t.querySelector("thead");
    l || (l = document.createElement("thead"), t.insertBefore(l, t.firstChild)), l.appendChild(r);
  }
  p();
}, F = () => {
  const e = f();
  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 l = o.tagName.toLowerCase() === "th" ? "td" : "th", a = document.createElement(l);
      a.innerHTML = o.innerHTML;
      for (let s = 0; s < o.attributes.length; s++) {
        const d = o.attributes[s];
        a.setAttribute(d.name, d.value);
      }
      o.parentNode?.replaceChild(a, o);
    }
  }
  p();
}, U = () => {
  const e = f();
  if (!e) return;
  e.table.remove(), document.dispatchEvent(new CustomEvent("tableDeleted"));
}, j = () => {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return;
  const n = e.getRangeAt(0).startContainer;
  if (!(n.nodeType === Node.TEXT_NODE ? n.parentElement?.closest("table") : n.closest("table"))) return;
  let o = null;
  if (n.nodeType === Node.TEXT_NODE ? o = n.parentElement?.closest("td, th") : n.nodeType === Node.ELEMENT_NODE && (o = n.closest("td, th")), !o) return;
  const l = o.parentElement;
  if (!l) return;
  let a = -1;
  for (let u = 0; u < l.cells.length; u++)
    if (l.cells[u] === o) {
      a = u;
      break;
    }
  if (a === -1 || a === l.cells.length - 1) return;
  const s = l.cells[a + 1];
  if (!s) return;
  const d = parseInt(o.getAttribute("colspan") || "1"), c = parseInt(s.getAttribute("colspan") || "1");
  o.setAttribute("colspan", String(d + c)), Array.from(s.childNodes).forEach((u) => {
    o.appendChild(u);
  }), s.remove(), p();
};
function f() {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return null;
  const n = e.getRangeAt(0).startContainer;
  let r = n.nodeType === Node.TEXT_NODE ? n.parentElement?.closest("table") : n.closest("table");
  if (!r) return null;
  const o = r;
  let l = 0, a = 0;
  const s = n.nodeType === Node.TEXT_NODE ? n.parentElement?.closest("td, th") : n.closest("td, th");
  if (s) {
    let d = s.parentElement;
    for (; d && d !== o.rows[l] && (l++, !(l >= o.rows.length)); )
      ;
    const c = d;
    if (c) {
      for (let i = 0; i < c.cells.length; i++)
        if (c.cells[i] === s) {
          a = i;
          break;
        }
    }
  }
  return {
    table: o,
    rowIndex: l,
    colIndex: a,
    rowCount: o.rows.length,
    cellCount: o.rows[0]?.cells.length || 0
  };
}
function p() {
  if (!h || !m) return;
  const e = f();
  if (!e) return;
  const t = e.rowCount > 1, n = e.cellCount > 1;
  N(t, n);
}
function G() {
  R = () => {
    const e = f();
    e ? J(e.table) : E();
  }, x = (e) => {
    const t = e.target, n = t.closest("table"), r = t.closest(".table-toolbar");
    !n && !r && E();
  }, I = () => {
    E();
  }, M = () => {
    m && h && h.style.display !== "none" && y(m);
  }, S = () => {
    m && h && h.style.display !== "none" && y(m);
  }, document.addEventListener("selectionchange", R), document.addEventListener("mousedown", x), document.addEventListener("tableDeleted", I), window.addEventListener("scroll", M, !0), window.addEventListener("resize", S);
}
function y(e) {
  if (!h) return;
  const t = e.getBoundingClientRect(), n = h.getBoundingClientRect(), r = n.height || 40, o = n.width || 280, l = 10;
  let a = t.top - r - l, s = t.left + t.width / 2 - o / 2;
  a < l && (a = t.bottom + l), s < l && (s = l);
  const d = window.innerWidth;
  s + o > d - l && (s = d - o - l);
  const c = window.innerHeight;
  a + r > c - l && (a = c - r - l), 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(w) || document.body.matches(w) || document.documentElement.matches(w);
  h.classList.toggle("rte-theme-dark", t), h.style.display = "flex", h.style.visibility = "hidden", requestAnimationFrame(() => {
    y(e), h && (h.style.visibility = "visible");
  });
  const n = f();
  n && N(n.rowCount > 1, n.cellCount > 1), $(e);
}
function E() {
  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"]'), r = h.querySelector('[data-action="deleteColumn"]');
  n && (n.disabled = !e), r && (r.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 = (i) => {
    const u = document.createElement("button");
    return u.className = "toolbar-icon-btn", i.danger && u.classList.add("toolbar-icon-btn-danger"), i.delete && u.classList.add("toolbar-icon-btn-delete"), u.innerHTML = i.icon, u.title = i.title, u.setAttribute("aria-label", i.title), u.setAttribute("type", "button"), u.setAttribute("data-action", i.action), u.onclick = () => Z(i.action), u;
  }, n = () => {
    const i = document.createElement("div");
    return i.className = "toolbar-divider", i;
  }, r = (...i) => {
    const u = document.createElement("div");
    return u.className = "toolbar-section", i.forEach((v) => u.appendChild(v)), u;
  }, o = r(
    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
    })
  ), l = r(
    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 = r(
    t({
      icon: ie(),
      title: "Toggle header row",
      action: "toggleHeaderRow"
    }),
    t({
      icon: de(),
      title: "Toggle header column",
      action: "toggleHeaderColumn"
    })
  ), s = r(
    t({
      icon: ue(),
      title: "Merge cells (horizontally)",
      action: "mergeCells"
    })
  ), d = r(
    t({
      icon: ce(),
      title: "Delete table",
      action: "deleteTable",
      delete: !0
    })
  );
  e.appendChild(o), e.appendChild(n()), e.appendChild(l), e.appendChild(n()), e.appendChild(a), e.appendChild(n()), e.appendChild(s), e.appendChild(n()), e.appendChild(d);
  const c = (i) => {
    !h || h.style.display === "none" || (i.ctrlKey || i.metaKey) && i.shiftKey && (i.key === "r" || i.key === "R" ? (i.preventDefault(), H()) : (i.key === "c" || i.key === "C") && (i.preventDefault(), z()));
  };
  return window.addEventListener("keydown", c), e;
}
function Z(e) {
  switch (e) {
    case "addRowAbove":
      P();
      break;
    case "addRowBelow":
      H();
      break;
    case "addColumnLeft":
      W();
      break;
    case "addColumnRight":
      z();
      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 r = e.querySelector("thead tr, tbody tr:first-child");
  if (!r) return;
  const o = r.querySelectorAll("td, th");
  o.forEach((a, s) => {
    if (s === o.length - 1) return;
    const d = document.createElement("div");
    d.className = "resize-handle", d.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;
    `, d.addEventListener("mouseenter", () => {
      g || (d.style.background = "rgba(0, 102, 204, 0.3)");
    }), d.addEventListener("mouseleave", () => {
      g || (d.style.background = "transparent");
    }), d.addEventListener("mousedown", (c) => {
      c.preventDefault(), c.stopPropagation(), ee(c, s);
    }), a.style.position = "relative", a.appendChild(d);
  });
  const l = document.createElement("div");
  l.className = "table-resize-handle", l.addEventListener("mousedown", (a) => {
    a.preventDefault(), a.stopPropagation(), te(a);
  }), e.appendChild(l);
}
function ee(e, t) {
  if (g = !0, b = 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 r = (l) => {
    if (!g || b === null || !m) return;
    const a = l.clientX - V, s = Math.max(50, L + a);
    m.querySelectorAll("tr").forEach((c) => {
      c.cells[b] && (c.cells[b].style.width = s + "px");
    });
  }, o = () => {
    g = !1, b = null, document.body.style.cursor = "", document.body.style.userSelect = "", document.removeEventListener("mousemove", r), document.removeEventListener("mouseup", o);
  };
  document.addEventListener("mousemove", r), document.addEventListener("mouseup", o);
}
function te(e) {
  if (!m) return;
  C = !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 = (r) => {
    if (!C || !m) return;
    const o = r.clientX - k, l = r.clientY - A, a = Math.max(200, B + o), s = Math.max(100, D + l);
    m.style.width = a + "px", m.style.height = s + "px";
  }, n = () => {
    C = !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 < "u" && !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": () => (H(), !0),
    "Mod-Shift-c": () => (z(), !0)
  }
});
export {
  me as TablePlugin
};
