const T = {
  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 u = null, h = null, w = !1;
const $ = () => new Promise((n, d) => {
  if (window.katex) {
    n(window.katex);
    return;
  }
  if (w) {
    const i = setInterval(() => {
      window.katex && (clearInterval(i), n(window.katex));
    }, 100);
    return;
  }
  w = !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 o = document.createElement("script");
  o.src = "https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js", o.onload = () => n(window.katex), o.onerror = d, document.head.appendChild(o);
}), k = async (n) => {
  const d = window.getSelection();
  d && d.rangeCount > 0 && (u = d.getRangeAt(0).cloneRange()), await $();
  const r = document.createElement("div");
  r.style.cssText = "position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.5); display: flex; align-items: center; justify-content: center; z-index: 99999;";
  const o = document.createElement("div");
  o.style.cssText = "background: white; 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);";
  let i = (n == null ? void 0 : n.format) || "latex", s = (n == null ? void 0 : n.formula) || "", f = (n == null ? void 0 : n.inline) !== !1;
  o.innerHTML = `
    <div style="display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid #e1e5e9; background: #f8f9fa;">
      <h2 style="margin: 0; font-size: 18px; font-weight: 600;">${n ? "Edit" : "Insert"} Math Formula</h2>
      <button class="close-btn" style="background: none; border: none; font-size: 28px; cursor: pointer; color: #6c757d; 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;">Format:</label>
        <div style="display: flex; gap: 16px;">
          <label style="cursor: pointer;"><input type="radio" name="format" value="latex" ${i === "latex" ? "checked" : ""} style="margin-right: 6px;"> LaTeX</label>
          <label style="cursor: pointer;"><input type="radio" name="format" value="mathml" ${i === "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;">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;"><input type="checkbox" id="inline-cb" ${f ? "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;">Formula:</label>
        <textarea id="formula-input" rows="4" style="width: 100%; padding: 10px; border: 1px solid #ced4da; border-radius: 4px; font-family: 'Courier New', monospace; font-size: 14px;">${s}</textarea>
      </div>

      <div style="margin-bottom: 20px;">
        <label style="display: block; font-weight: 600; margin-bottom: 8px; font-size: 14px;">Preview:</label>
        <div id="preview-area" style="min-height: 60px; padding: 15px; border: 1px solid #dee2e6; border-radius: 4px; background: #f8f9fa; display: flex; align-items: center; justify-content: center; color: #6c757d;"></div>
      </div>
    </div>

    <div style="display: flex; justify-content: flex-end; gap: 10px; padding: 16px 20px; border-top: 1px solid #e1e5e9; background: #f8f9fa;">
      <button class="cancel-btn" style="padding: 10px 20px; background: #fff; border: 1px solid #ced4da; border-radius: 4px; cursor: pointer; font-size: 14px;">Cancel</button>
      <button id="insert-btn" style="padding: 10px 20px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px;" disabled>${n ? "Update" : "Insert"}</button>
    </div>
  `, r.appendChild(o), document.body.appendChild(r);
  const l = o.querySelector("#formula-input"), c = o.querySelector("#preview-area"), g = o.querySelector("#templates-grid"), M = o.querySelectorAll('input[name="format"]'), L = o.querySelector("#inline-cb"), x = o.querySelector("#insert-btn"), E = o.querySelector(".close-btn"), S = o.querySelector(".cancel-btn"), y = () => {
    const e = T[i];
    g.innerHTML = e.map((t) => `
      <button type="button" data-formula="${t.formula.replace(/"/g, "&quot;")}" title="${t.description}" style="padding: 8px; border: 1px solid #ced4da; border-radius: 4px; background: #fff; cursor: pointer; text-align: left;">
        <div style="font-weight: 600; font-size: 12px;">${t.name}</div>
        <div style="font-size: 10px; color: #6c757d; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">${t.formula.substring(0, 20)}...</div>
      </button>
    `).join(""), g.querySelectorAll("button").forEach((t) => {
      t.addEventListener("click", () => {
        l.value = t.getAttribute("data-formula") || "", s = l.value, m();
      });
    });
  }, m = () => {
    const e = l.value.trim();
    if (!e) {
      c.innerHTML = '<span style="color: #6c757d;">Enter a formula to see preview</span>', x.disabled = !0;
      return;
    }
    x.disabled = !1;
    try {
      if (i === "latex") {
        const t = window.katex;
        c.innerHTML = t.renderToString(e, { displayMode: !1, throwOnError: !1 });
      } else
        e.trim().startsWith("<math") ? c.innerHTML = e : c.innerHTML = `<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">${e}</math>`;
    } catch (t) {
      c.innerHTML = '<span style="color: #cc0000;">Invalid formula</span>';
    }
  }, p = () => document.body.removeChild(r), v = () => {
    const e = l.value.trim();
    if (!e) return;
    const t = { formula: e, format: i, inline: L.checked }, a = t.inline ? document.createElement("span") : document.createElement("div");
    if (a.className = t.inline ? "math-formula" : "math-block", a.setAttribute("data-math-formula", e), a.setAttribute("data-math-format", i), a.contentEditable = "false", a.style.cssText = t.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;", i === "latex") {
      const b = window.katex;
      try {
        a.innerHTML = b.renderToString(e, {
          displayMode: !t.inline,
          throwOnError: !1
        });
      } catch (q) {
        a.textContent = t.inline ? `$${e}$` : `$$${e}$$`;
      }
    } else if (e.trim().startsWith("<math"))
      a.innerHTML = e;
    else {
      const b = `<math xmlns="http://www.w3.org/1998/Math/MathML" display="${t.inline ? "inline" : "block"}">${e}</math>`;
      a.innerHTML = b;
    }
    h ? h.replaceWith(a) : u && (u.deleteContents(), u.insertNode(a)), p();
  };
  E.addEventListener("click", p), S.addEventListener("click", p), x.addEventListener("click", v), r.addEventListener("click", (e) => {
    e.target === r && p();
  }), M.forEach((e) => {
    e.addEventListener("change", (t) => {
      i = t.target.value, y(), m();
    });
  }), l.addEventListener("input", () => {
    s = l.value, m();
  }), l.addEventListener("keydown", (e) => {
    (e.ctrlKey || e.metaKey) && e.key === "Enter" && (e.preventDefault(), v());
  }), y(), m(), l.focus();
};
if (typeof window != "undefined" && !window.__mathPluginDoubleClickInitialized) {
  window.__mathPluginDoubleClickInitialized = !0;
  const n = (r) => {
    const i = r.target.closest(".math-formula, .math-block");
    if (i) {
      r.preventDefault(), r.stopPropagation(), r.stopImmediatePropagation(), h = i;
      const s = i.getAttribute("data-math-formula") || "", f = i.getAttribute("data-math-format") || "latex", l = i.classList.contains("math-formula");
      k({ formula: s, format: f, inline: l });
    }
  }, d = () => {
    document.addEventListener("dblclick", n, { capture: !0 });
  };
  document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", d) : setTimeout(d, 100);
}
const C = () => ({
  name: "math",
  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: () => (k(), !0)
  },
  keymap: {
    "Mod-Shift-m": "insertMath"
  }
});
export {
  C as MathPlugin
};
//# sourceMappingURL=MathPlugin.native-DE_ii-LA.mjs.map
