import { f as N, g as q } from "./editorContainerHelpers-C7kdWnS0.mjs";
let h = null, m = null, T = null, R = null, x = null, I = null, V = null, w = !1, v = null, S = 0, M = 0, C = !1, L = 0, k = 0, A = 0, B = 0;
const X = () => {
  const e = N(), t = q(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 i = 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);
    }
    i.appendChild(c);
  }
  r.appendChild(o), r.appendChild(i), l.deleteContents(), l.insertNode(r);
  const s = r.querySelector("th p");
  if (s) {
    const d = document.createRange();
    d.setStart(s, 0), d.collapse(!0), n.removeAllRanges(), n.addRange(d);
  }
  t.focus();
}, _ = () => {
  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 i = 0; i < r; i++) {
    const s = document.createElement("td"), d = document.createElement("p");
    d.innerHTML = "<br>", s.appendChild(d), l.appendChild(s);
  }
  const o = t.rows[n];
  o && o.parentElement ? o.parentElement.insertBefore(l, o) : t.appendChild(l), g();
}, H = () => {
  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 i = document.createElement("td"), s = document.createElement("p");
    s.innerHTML = "<br>", i.appendChild(s), l.appendChild(i);
  }
  n >= t.rows.length - 1 ? t.appendChild(l) : t.insertBefore(l, t.rows[n + 1]), g();
}, P = () => {
  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();
}, z = () => {
  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();
}, W = () => {
  const e = p();
  if (!e || e.rowCount <= 1) return;
  const { table: t, rowIndex: n } = e;
  t.deleteRow(n), g();
}, O = () => {
  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();
}, K = () => {
  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")), i = t.querySelector("thead");
    i && (a.insertBefore(l, a.firstChild), i.rows.length === 0 && i.remove());
  } else {
    let a = t.querySelector("thead");
    a || (a = document.createElement("thead"), t.insertBefore(a, t.firstChild)), a.appendChild(l);
  }
  g();
}, Y = () => {
  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", i = document.createElement(a);
      i.innerHTML = o.innerHTML;
      for (let s = 0; s < o.attributes.length; s++) {
        const d = o.attributes[s];
        i.setAttribute(d.name, d.value);
      }
      (l = o.parentNode) == null || l.replaceChild(i, o);
    }
  }
  g();
}, F = () => {
  const e = p();
  if (!e) return;
  e.table.remove(), document.dispatchEvent(new CustomEvent("tableDeleted"));
}, G = () => {
  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 i = o.cells[a + 1];
  if (!i) return;
  const s = parseInt(r.getAttribute("colspan") || "1"), d = parseInt(i.getAttribute("colspan") || "1");
  r.setAttribute("colspan", String(s + d)), Array.from(i.childNodes).forEach((f) => {
    r.appendChild(f);
  }), i.remove(), g();
};
function p() {
  var s, 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 ? (s = n.parentElement) == null ? void 0 : s.closest("table") : n.closest("table");
  if (!l) return null;
  const r = l;
  let o = 0, a = 0;
  const i = n.nodeType === Node.TEXT_NODE ? (d = n.parentElement) == null ? void 0 : d.closest("td, th") : n.closest("td, th");
  if (i) {
    let u = i.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] === i) {
          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;
  D(t, n);
}
function j() {
  T = () => {
    const e = p();
    e ? U(e.table) : y();
  }, R = (e) => {
    const t = e.target, n = t.closest("table"), l = t.closest(".table-toolbar");
    !n && !l && y();
  }, x = () => {
    y();
  }, I = () => {
    m && h && h.style.display !== "none" && E(m);
  }, V = () => {
    m && h && h.style.display !== "none" && E(m);
  }, document.addEventListener("selectionchange", T), document.addEventListener("mousedown", R), document.addEventListener("tableDeleted", x), window.addEventListener("scroll", I, !0), window.addEventListener("resize", V);
}
function E(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, i = t.left + t.width / 2 - r / 2;
  a < o && (a = t.bottom + o), i < o && (i = o);
  const s = window.innerWidth;
  i + r > s - o && (i = s - r - o);
  const d = window.innerHeight;
  a + l > d - o && (a = d - l - o), h.style.top = a + "px", h.style.left = i + "px";
}
function U(e) {
  m = e, h || (h = Z(), document.body.appendChild(h)), h.style.display = "flex", h.style.visibility = "hidden", requestAnimationFrame(() => {
    E(e), h && (h.style.visibility = "visible");
  });
  const t = p();
  t && D(t.rowCount > 1, t.cellCount > 1), Q(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 D(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 Z() {
  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 = () => J(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: te(),
      title: "Add row above (Ctrl+Shift+R)",
      action: "addRowAbove"
    }),
    t({
      icon: ne(),
      title: "Add row below",
      action: "addRowBelow"
    }),
    t({
      icon: oe(),
      title: "Delete row",
      action: "deleteRow",
      danger: !0
    })
  ), o = l(
    t({
      icon: le(),
      title: "Add column left",
      action: "addColumnLeft"
    }),
    t({
      icon: re(),
      title: "Add column right (Ctrl+Shift+C)",
      action: "addColumnRight"
    }),
    t({
      icon: ae(),
      title: "Delete column",
      action: "deleteColumn",
      danger: !0
    })
  ), a = l(
    t({
      icon: ie(),
      title: "Toggle header row",
      action: "toggleHeaderRow"
    }),
    t({
      icon: se(),
      title: "Toggle header column",
      action: "toggleHeaderColumn"
    })
  ), i = l(
    t({
      icon: ce(),
      title: "Merge cells (horizontally)",
      action: "mergeCells"
    })
  ), s = l(
    t({
      icon: de(),
      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(i), e.appendChild(n()), e.appendChild(s);
  const d = (c) => {
    !h || h.style.display === "none" || (c.ctrlKey || c.metaKey) && c.shiftKey && (c.key === "r" || c.key === "R" ? (c.preventDefault(), H()) : (c.key === "c" || c.key === "C") && (c.preventDefault(), z()));
  };
  return window.addEventListener("keydown", d), e;
}
function J(e) {
  switch (e) {
    case "addRowAbove":
      _();
      break;
    case "addRowBelow":
      H();
      break;
    case "addColumnLeft":
      P();
      break;
    case "addColumnRight":
      z();
      break;
    case "deleteRow":
      W();
      break;
    case "deleteColumn":
      O();
      break;
    case "toggleHeaderRow":
      K();
      break;
    case "toggleHeaderColumn":
      Y();
      break;
    case "deleteTable":
      F();
      break;
    case "mergeCells":
      G();
      break;
  }
}
function Q(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, i) => {
    if (i === r.length - 1) return;
    const s = document.createElement("div");
    s.className = "resize-handle", s.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;
    `, s.addEventListener("mouseenter", () => {
      w || (s.style.background = "rgba(0, 102, 204, 0.3)");
    }), s.addEventListener("mouseleave", () => {
      w || (s.style.background = "transparent");
    }), s.addEventListener("mousedown", (d) => {
      d.preventDefault(), d.stopPropagation(), $(d, i);
    }), a.style.position = "relative", a.appendChild(s);
  });
  const o = document.createElement("div");
  o.className = "table-resize-handle", o.addEventListener("mousedown", (a) => {
    a.preventDefault(), a.stopPropagation(), ee(a);
  }), e.appendChild(o);
}
function $(e, t) {
  if (w = !0, v = t, S = e.clientX, !m) return;
  const n = m.querySelector("thead tr, tbody tr:first-child");
  n && n.cells[t] && (M = 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 - S, i = Math.max(50, M + a);
    m.querySelectorAll("tr").forEach((d) => {
      d.cells[v] && (d.cells[v].style.width = i + "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 ee(e) {
  if (!m) return;
  C = !0, L = e.clientX, k = e.clientY, A = m.offsetWidth, B = m.offsetHeight, document.body.style.cursor = "nwse-resize", document.body.style.userSelect = "none";
  const t = (l) => {
    if (!C || !m) return;
    const r = l.clientX - L, o = l.clientY - k, a = Math.max(200, A + r), i = Math.max(100, B + o);
    m.style.width = a + "px", m.style.height = i + "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 te() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M2 7h12V5H2v2zm0 4h12V9H2v2zM8 1v3H5v2h3v3h2V6h3V4h-3V1H8z"/>
  </svg>`;
}
function ne() {
  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 oe() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M2 5h12v2H2V5zm0 4h12v2H2V9zm4-6v2H4v2h2v2h2V7h2V5H8V3H6z"/>
  </svg>`;
}
function le() {
  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 re() {
  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 ae() {
  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 se() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M2 2v12h3V2H2zm5 0v12h8V2H7zm2 2h4v2H9V4zm0 4h4v2H9V8zm0 4h4v2H9v-2z"/>
  </svg>`;
}
function de() {
  return `<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M3 1h10v1H3V1zm1 2v11h8V3H4zM6 5h1v6H6V5zm3 0h1v6H9V5z"/>
  </svg>`;
}
function ce() {
  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 = () => {
    j();
  };
  document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", e) : setTimeout(e, 100);
}
const he = () => ({
  name: "table",
  toolbar: [
    {
      label: "Insert Table",
      command: "insertTable",
      icon: '<svg width="24px" height="24px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M12 10V20M3 15L21 15M3 10H21M6.2 20H17.8C18.9201 20 19.4802 20 19.908 19.782C20.2843 19.5903 20.5903 19.2843 20.782 18.908C21 18.4802 21 17.9201 21 16.8V7.2C21 6.0799 21 5.51984 20.782 5.09202C20.5903 4.71569 20.2843 4.40973 19.908 4.21799C19.4802 4 18.9201 4 17.8 4H6.2C5.0799 4 4.51984 4 4.09202 4.21799C3.71569 4.40973 3.40973 4.71569 3.21799 5.09202C3 5.51984 3 6.07989 3 7.2V16.8C3 17.9201 3 18.4802 3.21799 18.908C3.40973 19.2843 3.71569 19.5903 4.09202 19.782C4.51984 20 5.07989 20 6.2 20Z" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>'
    }
  ],
  commands: {
    insertTable: () => (X(), !0)
  },
  keymap: {
    "Mod-Shift-r": () => (H(), !0),
    "Mod-Shift-c": () => (z(), !0)
  }
});
export {
  he as TablePlugin,
  P as addColumnLeftCommand,
  z as addColumnRightCommand,
  _ as addRowAboveCommand,
  H as addRowBelowCommand,
  O as deleteColumnCommand,
  W as deleteRowCommand,
  F as deleteTableCommand,
  X as insertTableCommand,
  G as mergeCellsCommand,
  Y as toggleHeaderColumnCommand,
  K as toggleHeaderRowCommand
};
//# sourceMappingURL=TablePlugin.native-EEWXn1-s.mjs.map
