const K = {
  latex: [
    { name: "Fraction", formula: "\\frac{a}{b}", description: "Simple fraction" },
    { name: "Square Root", formula: "\\sqrt{x}", description: "Square root" },
    { name: "Power", formula: "x^{2}", description: "Exponent/power" },
    { name: "Subscript", formula: "x_{sub}", description: "Subscript" },
    { name: "Integral", formula: "\\int_{a}^{b} f(x) \\, dx", description: "Definite integral" },
    { name: "Summation", formula: "\\sum_{i=1}^{n} x_{i}", description: "Summation" },
    { name: "Limit", formula: "\\lim_{x \\to 0} f(x)", description: "Limit" },
    { name: "Derivative", formula: "\\frac{d}{dx} f(x)", description: "Derivative" },
    { name: "Matrix 2x2", formula: "\\begin{pmatrix} a & b \\\\ c & d \\end{pmatrix}", description: "2x2 matrix" },
    { name: "System", formula: "\\begin{cases} x + y = 1 \\\\ 2x - y = 0 \\end{cases}", description: "System of equations" }
  ],
  mathml: [
    { name: "Fraction", formula: "<mfrac><mi>a</mi><mi>b</mi></mfrac>", description: "Simple fraction" },
    { name: "Square Root", formula: "<msqrt><mi>x</mi></msqrt>", description: "Square root" },
    { name: "Power", formula: "<msup><mi>x</mi><mn>2</mn></msup>", description: "Exponent/power" },
    { name: "Subscript", formula: "<msub><mi>x</mi><mi>sub</mi></msub>", description: "Subscript" },
    { name: "Parentheses", formula: '<mfenced open="(" close=")"><mi>a</mi><mo>+</mo><mi>b</mi></mfenced>', description: "Grouped expression" }
  ]
};
let M = null, v = null, R = !1, y = null, m = null;
const $ = '[data-theme="dark"], .dark, .editora-theme-dark', U = () => new Promise((e, t) => {
  if (window.katex) {
    e(window.katex);
    return;
  }
  if (R) {
    const l = setInterval(() => {
      window.katex && (clearInterval(l), e(window.katex));
    }, 100);
    return;
  }
  R = !0;
  const r = document.createElement("link");
  r.rel = "stylesheet", r.href = "https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css", document.head.appendChild(r);
  const a = document.createElement("script");
  a.src = "https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js", a.onload = () => e(window.katex), a.onerror = t, document.head.appendChild(a);
}), j = () => {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return null;
  const t = e.getRangeAt(0).startContainer, r = t.nodeType === Node.ELEMENT_NODE ? t : t.parentElement;
  return (r == null ? void 0 : r.closest(".rte-content, .editora-content")) || null;
}, W = (e) => e ? e.nodeType === Node.ELEMENT_NODE ? e : e.parentElement : null, _ = (e) => {
  const t = W(e);
  return (t == null ? void 0 : t.closest('.rte-content, .editora-content, [contenteditable="true"]')) || null;
}, G = (e, t, r) => {
  if (t !== r) {
    if (typeof window.execEditorCommand == "function") {
      window.execEditorCommand("recordDomTransaction", e, t, r);
      return;
    }
    if (typeof window.executeEditorCommand == "function")
      try {
        window.executeEditorCommand("recordDomTransaction", { editor: e, beforeHTML: t, afterHTML: r });
      } catch (a) {
      }
  }
}, X = (e) => {
  if (!(e instanceof Element)) return null;
  const t = e.closest(".math-formula, .math-block");
  return t instanceof HTMLElement ? t : null;
}, h = () => {
  m && (m.classList.remove("math-object-selected"), m.removeAttribute("data-rte-selected-object"), m = null);
}, Z = (e) => {
  if (m === e) return;
  h(), m = e, e.classList.add("math-object-selected"), e.setAttribute("data-rte-selected-object", "true");
  const t = window.getSelection();
  if (!t) return;
  const r = document.createRange();
  r.selectNode(e), t.removeAllRanges(), t.addRange(r);
}, Q = (e, t) => {
  const r = window.getSelection();
  if (!r) return;
  const a = document.createRange();
  t && t.parentNode === e ? a.setStartBefore(t) : (a.selectNodeContents(e), a.collapse(!1)), a.collapse(!0), r.removeAllRanges(), r.addRange(a);
}, J = (e) => {
  if (e.childNodes.length > 0) return;
  const t = document.createElement("p");
  t.appendChild(document.createElement("br")), e.appendChild(t);
}, V = () => {
  if (!m || !m.isConnected)
    return h(), !1;
  const e = m, t = _(e);
  if (!t)
    return h(), !1;
  const r = t.innerHTML, a = e.nextSibling;
  e.remove(), J(t), h(), Q(t, a);
  const l = t.innerHTML;
  return G(t, r, l), t.dispatchEvent(new window.Event("input", { bubbles: !0 })), r !== l;
}, Y = () => {
  if (document.getElementById("math-object-selection-styles")) return;
  const e = document.createElement("style");
  e.id = "math-object-selection-styles", e.textContent = `
    .math-object-selected {
      outline: 2px solid #2563eb !important;
      outline-offset: 2px;
      box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.18);
    }
  `, document.head.appendChild(e);
}, ee = () => {
  typeof window == "undefined" || window.__mathPluginObjectSelectionInitialized || (window.__mathPluginObjectSelectionInitialized = !0, Y(), document.addEventListener("pointerdown", (e) => {
    var r, a;
    const t = X(e.target);
    if (!t) {
      const l = e.target;
      (r = l == null ? void 0 : l.closest) != null && r.call(l, ".math-dialog-overlay") || h();
      return;
    }
    e.preventDefault(), (a = _(t)) == null || a.focus(), Z(t);
  }, !0), document.addEventListener("keydown", (e) => {
    if (m) {
      if (e.key === "Escape") {
        h();
        return;
      }
      e.key !== "Backspace" && e.key !== "Delete" || (e.preventDefault(), e.stopPropagation(), V());
    }
  }, !0));
}, te = (e) => {
  const t = e || j();
  if (t != null && t.closest($)) return !0;
  const r = window.getSelection();
  if (r && r.rangeCount > 0) {
    const l = r.getRangeAt(0).startContainer, n = l.nodeType === Node.ELEMENT_NODE ? l : l.parentElement;
    if (n != null && n.closest($)) return !0;
  }
  const a = document.activeElement;
  return a != null && a.closest($) ? !0 : document.body.matches($) || document.documentElement.matches($);
}, z = async (e, t) => {
  const r = t || (v == null ? void 0 : v.closest(".rte-content, .editora-content")) || j() || y;
  y = r || null;
  const a = window.getSelection();
  if (a && a.rangeCount > 0) {
    const o = a.getRangeAt(0);
    M = r && r.contains(o.commonAncestorContainer) ? o.cloneRange() : null;
  }
  await U();
  const l = te(r), n = l ? {
    overlay: "rgba(0, 0, 0, 0.62)",
    dialogBg: "#1f2937",
    border: "#3b4657",
    panelBg: "#222d3a",
    fieldBg: "#111827",
    fieldBorder: "#4b5563",
    text: "#e2e8f0",
    muted: "#94a3b8",
    templateBtnBg: "#273244",
    templateBtnHover: "#334155",
    templateBtnText: "#dbe7f7",
    templateSubText: "#9fb0c6",
    previewBg: "#111827",
    previewText: "#cbd5e1",
    cancelBg: "#334155",
    cancelText: "#e2e8f0",
    cancelBorder: "#4b5563",
    insertBg: "#3b82f6",
    insertHover: "#2563eb",
    invalid: "#f87171"
  } : {
    overlay: "rgba(0, 0, 0, 0.5)",
    dialogBg: "#ffffff",
    border: "#e1e5e9",
    panelBg: "#f8f9fa",
    fieldBg: "#ffffff",
    fieldBorder: "#ced4da",
    text: "#1f2937",
    muted: "#6c757d",
    templateBtnBg: "#ffffff",
    templateBtnHover: "#f8f9fa",
    templateBtnText: "#1f2937",
    templateSubText: "#6c757d",
    previewBg: "#f8f9fa",
    previewText: "#6c757d",
    cancelBg: "#ffffff",
    cancelText: "#1f2937",
    cancelBorder: "#ced4da",
    insertBg: "#007bff",
    insertHover: "#0069d9",
    invalid: "#cc0000"
  }, d = document.createElement("div");
  d.className = "math-dialog-overlay", d.style.cssText = `position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: ${n.overlay}; display: flex; align-items: center; justify-content: center; z-index: 99999;`;
  const c = document.createElement("div");
  c.style.cssText = `background: ${n.dialogBg}; border: 1px solid ${n.border}; border-radius: 8px; width: 90%; max-width: 600px; max-height: 90vh; overflow: hidden; display: flex; flex-direction: column; box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2); color: ${n.text};`;
  let u = (e == null ? void 0 : e.format) || "latex", S = (e == null ? void 0 : e.formula) || "", I = (e == null ? void 0 : e.inline) !== !1, g = null, L = "";
  c.innerHTML = `
    <div style="display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid ${n.border}; background: ${n.panelBg};">
      <h2 style="margin: 0; font-size: 18px; font-weight: 600; color: ${n.text};">${e ? "Edit" : "Insert"} Math Formula</h2>
      <button class="close-btn" style="background: none; border: none; font-size: 28px; cursor: pointer; color: ${n.muted}; padding: 0; width: 30px; height: 30px; line-height: 1;">×</button>
    </div>
    
    <div style="padding: 20px; overflow-y: auto; flex: 1;">
      <div style="margin-bottom: 20px;">
        <label style="display: block; font-weight: 600; margin-bottom: 8px; font-size: 14px; color: ${n.text};">Format:</label>
        <div style="display: flex; gap: 16px;">
          <label style="cursor: pointer; color: ${n.text};"><input type="radio" name="format" value="latex" ${u === "latex" ? "checked" : ""} style="margin-right: 6px;"> LaTeX</label>
          <label style="cursor: pointer; color: ${n.text};"><input type="radio" name="format" value="mathml" ${u === "mathml" ? "checked" : ""} style="margin-right: 6px;"> MathML</label>
        </div>
      </div>

      <div style="margin-bottom: 20px;">
        <label style="display: block; font-weight: 600; margin-bottom: 8px; font-size: 14px; color: ${n.text};">Quick Templates:</label>
        <div id="templates-grid" style="display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; max-height: 200px; overflow-y: auto;"></div>
      </div>

      <div style="margin-bottom: 20px;">
        <label style="cursor: pointer; color: ${n.text};"><input type="checkbox" id="inline-cb" ${I ? "checked" : ""} style="margin-right: 8px;"> Inline math</label>
      </div>

      <div style="margin-bottom: 20px;">
        <label style="display: block; font-weight: 600; margin-bottom: 8px; font-size: 14px; color: ${n.text};">Formula:</label>
        <textarea id="formula-input" rows="4" style="width: 100%; min-height: 112px; padding: 10px 12px; border: 1px solid ${n.fieldBorder}; border-radius: 6px; font-family: 'Courier New', monospace; font-size: 14px; line-height: 1.45; background: ${n.fieldBg}; color: ${n.text}; box-sizing: border-box; overflow-x: hidden; overflow-y: auto; resize: vertical;">${S}</textarea>
      </div>

      <div style="margin-bottom: 20px;">
        <label style="display: block; font-weight: 600; margin-bottom: 8px; font-size: 14px; color: ${n.text};">Preview:</label>
        <div id="preview-area" style="min-height: 60px; padding: 15px; border: 1px solid ${n.fieldBorder}; border-radius: 4px; background: ${n.previewBg}; display: flex; align-items: center; justify-content: center; color: ${n.previewText};"></div>
      </div>
    </div>

    <div style="display: flex; justify-content: flex-end; gap: 10px; padding: 16px 20px; border-top: 1px solid ${n.border}; background: ${n.panelBg};">
      <button class="cancel-btn" style="padding: 10px 20px; background: ${n.cancelBg}; color: ${n.cancelText}; border: 1px solid ${n.cancelBorder}; border-radius: 4px; cursor: pointer; font-size: 14px;">Cancel</button>
      <button id="insert-btn" style="padding: 10px 20px; background: ${n.insertBg}; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px;" disabled>${e ? "Update" : "Insert"}</button>
    </div>
  `, d.appendChild(c), document.body.appendChild(d);
  const p = c.querySelector("#formula-input"), w = c.querySelector("#preview-area"), T = c.querySelector("#templates-grid"), P = c.querySelectorAll('input[name="format"]'), q = c.querySelector("#inline-cb"), f = c.querySelector("#insert-btn"), b = c.querySelector(".close-btn"), E = c.querySelector(".cancel-btn"), F = (o) => encodeURIComponent(o), D = (o) => {
    try {
      return decodeURIComponent(o);
    } catch (s) {
      return o;
    }
  }, A = () => {
    const o = K[u];
    T.innerHTML = o.map((s) => `
      <button type="button" data-formula="${F(s.formula)}" title="${s.description}" style="padding: 8px; border: 1px solid ${n.fieldBorder}; border-radius: 4px; background: ${n.templateBtnBg}; cursor: pointer; text-align: left; transition: background-color 0.16s ease;">
        <div style="font-weight: 600; font-size: 12px; color: ${n.templateBtnText};">${s.name}</div>
        <div style="font-size: 10px; color: ${n.templateSubText}; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">${s.formula.substring(0, 20)}...</div>
      </button>
    `).join("");
  }, O = () => {
    const o = p.value.trim(), s = `${u}:${o}`;
    if (s !== L) {
      if (L = s, !o) {
        w.innerHTML = `<span style="color: ${n.previewText};">Enter a formula to see preview</span>`, f.disabled = !0;
        return;
      }
      f.disabled = !1;
      try {
        if (u === "latex") {
          const i = window.katex;
          w.innerHTML = i.renderToString(o, { displayMode: !1, throwOnError: !1 });
        } else
          o.trim().startsWith("<math") ? w.innerHTML = o : w.innerHTML = `<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">${o}</math>`;
      } catch (i) {
        w.innerHTML = `<span style="color: ${n.invalid};">Invalid formula</span>`;
      }
    }
  }, B = () => {
    g !== null && cancelAnimationFrame(g), g = requestAnimationFrame(() => {
      g = null, O();
    });
  }, H = (o) => {
    o.key === "Escape" && (o.preventDefault(), o.stopPropagation(), k());
  }, k = () => {
    document.removeEventListener("keydown", H, !0), g !== null && (cancelAnimationFrame(g), g = null), d.parentNode && d.parentNode.removeChild(d);
  };
  b.onmouseover = () => {
    b.style.color = "#f8fafc", b.style.background = l ? "#334155" : "#e5e7eb", b.style.borderRadius = "4px";
  }, b.onmouseout = () => {
    b.style.color = n.muted, b.style.background = "none";
  }, E.onmouseover = () => {
    E.style.background = l ? "#475569" : "#f3f4f6";
  }, E.onmouseout = () => {
    E.style.background = n.cancelBg;
  }, f.onmouseover = () => {
    f.disabled || (f.style.background = n.insertHover);
  }, f.onmouseout = () => {
    f.style.background = n.insertBg;
  }, T.addEventListener("mouseover", (o) => {
    const i = o.target.closest("button[data-formula]");
    i && (i.style.background = n.templateBtnHover);
  }), T.addEventListener("mouseout", (o) => {
    const i = o.target.closest("button[data-formula]");
    i && (i.style.background = n.templateBtnBg);
  }), T.addEventListener("click", (o) => {
    const i = o.target.closest("button[data-formula]");
    i && (p.value = D(i.getAttribute("data-formula") || ""), S = p.value, B());
  });
  const N = () => {
    const o = p.value.trim();
    if (!o) return;
    const s = { formula: o, format: u, inline: q.checked }, i = s.inline ? document.createElement("span") : document.createElement("div");
    if (i.className = s.inline ? "math-formula" : "math-block", i.setAttribute("data-math-formula", o), i.setAttribute("data-math-format", u), i.contentEditable = "false", i.style.cssText = s.inline ? "background: #f0f8ff; border: 1px solid #b8daff; border-radius: 4px; padding: 2px 6px; margin: 0 2px; color: #004085; display: inline-block; cursor: pointer;" : "background: #f8f9fa; border: 1px solid #dee2e6; border-radius: 4px; padding: 12px; margin: 8px 0; text-align: center; display: block; cursor: pointer;", u === "latex") {
      const x = window.katex;
      try {
        i.innerHTML = x.renderToString(o, {
          displayMode: !s.inline,
          throwOnError: !1
        });
      } catch (ne) {
        i.textContent = s.inline ? `$${o}$` : `$$${o}$$`;
      }
    } else if (o.trim().startsWith("<math"))
      i.innerHTML = o;
    else {
      const x = `<math xmlns="http://www.w3.org/1998/Math/MathML" display="${s.inline ? "inline" : "block"}">${o}</math>`;
      i.innerHTML = x;
    }
    if (v)
      v.replaceWith(i);
    else if (M)
      M.deleteContents(), M.insertNode(i);
    else if (y && y.isConnected) {
      const x = document.createRange();
      x.selectNodeContents(y), x.collapse(!1), x.insertNode(i);
    }
    const C = i.closest(".rte-content, .editora-content") || y;
    C == null || C.dispatchEvent(new Event("input", { bubbles: !0 })), k();
  };
  b.addEventListener("click", k), E.addEventListener("click", k), f.addEventListener("click", N), d.addEventListener("click", (o) => {
    o.target === d && k();
  }), document.addEventListener("keydown", H, !0), P.forEach((o) => {
    o.addEventListener("change", (s) => {
      u = s.target.value, L = "", A(), B();
    });
  }), p.addEventListener("input", () => {
    S = p.value, B();
  }), p.addEventListener("keydown", (o) => {
    (o.ctrlKey || o.metaKey) && o.key === "Enter" && (o.preventDefault(), N());
  }), A(), B(), p.focus();
};
if (typeof window != "undefined" && !window.__mathPluginDoubleClickInitialized) {
  window.__mathPluginDoubleClickInitialized = !0;
  const e = (r) => {
    const l = r.target.closest(".math-formula, .math-block");
    if (l) {
      r.preventDefault(), r.stopPropagation(), r.stopImmediatePropagation(), h(), v = l;
      const n = l.getAttribute("data-math-formula") || "", d = l.getAttribute("data-math-format") || "latex", c = l.classList.contains("math-formula");
      z({ formula: n, format: d, inline: c });
    }
  }, t = () => {
    document.addEventListener("dblclick", e, { capture: !0 });
  };
  document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", t) : setTimeout(t, 100);
}
const oe = () => ({
  name: "math",
  init: ee,
  toolbar: [
    {
      label: "Insert Math",
      command: "insertMath",
      icon: '<svg width="24" height="24" focusable="false"><path fill-rule="evenodd" clip-rule="evenodd" d="M9 4.8c.1-.5.5-.8 1-.8h10a1 1 0 1 1 0 2h-9.2L8.3 19.2a1 1 0 0 1-1.7.4l-3.4-4.2a1 1 0 0 1 1.6-1.2l2 2.5L9 4.8Zm9.7 5.5c.4.4.4 1 0 1.4L17 13.5l1.8 1.8a1 1 0 0 1-1.4 1.4L15.5 15l-1.8 1.8a1 1 0 0 1-1.4-1.4l1.8-1.8-1.8-1.8a1 1 0 0 1 1.4-1.4l1.8 1.8 1.8-1.8a1 1 0 0 1 1.4 0Z"></path></svg>'
    }
  ],
  commands: {
    insertMath: (e, t) => {
      const r = (t == null ? void 0 : t.contentElement) instanceof HTMLElement ? t.contentElement : null;
      return z(void 0, r), !0;
    }
  },
  keymap: {
    "Mod-Shift-m": "insertMath"
  }
});
export {
  oe as MathPlugin
};
//# sourceMappingURL=MathPlugin.native-NwWPHVmj.mjs.map
