const C = () => ({
  name: "footnote",
  toolbar: [
    {
      label: "Footnote",
      command: "insertFootnote",
      icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor"><rect x="3" y="4" width="14" height="2" rx="1" /><rect x="3" y="8" width="18" height="2" rx="1" /><rect x="3" y="12" width="16" height="2" rx="1" /><rect x="3" y="16" width="10" height="1.5" rx="0.75" /><text x="19" y="11" font-size="9" font-weight="600" fill="currentColor" font-family="system-ui, sans-serif">1</text></svg>',
      type: "button"
    }
  ],
  commands: {
    insertFootnote: () => (y(), null)
  },
  keymap: {}
}), m = /* @__PURE__ */ new Map();
function l() {
  const e = window.getSelection();
  if (e && e.rangeCount > 0) {
    let o = e.getRangeAt(0).startContainer;
    for (; o && o !== document.body; ) {
      if (o.nodeType === Node.ELEMENT_NODE) {
        const n = o;
        if (n.getAttribute("contenteditable") === "true")
          return n;
      }
      o = o.parentNode;
    }
  }
  const t = document.activeElement;
  if (t) {
    if (t.getAttribute("contenteditable") === "true")
      return t;
    const o = t.closest('[contenteditable="true"]');
    if (o) return o;
  }
  return document.querySelector('[contenteditable="true"]');
}
function g() {
  const e = l();
  if (!e)
    throw new Error("Contenteditable element not found");
  let t = e.querySelector(".rte-footnotes");
  if (!t) {
    t = document.createElement("section"), t.className = "rte-footnotes", t.setAttribute("data-type", "footnotes"), t.setAttribute("contenteditable", "true");
    const o = document.createElement("ol");
    t.appendChild(o), e.appendChild(t);
  }
  return t;
}
const y = (e = "") => {
  const t = window.getSelection();
  if (!t || t.rangeCount === 0) return;
  const o = t.getRangeAt(0), n = `fn-${Date.now()}`, r = h(), i = document.createElement("sup");
  i.className = "rte-footnote-ref", i.setAttribute("data-footnote-id", n), i.setAttribute("data-number", r.toString()), i.textContent = r.toString(), i.style.cursor = "pointer";
  try {
    o.insertNode(i), o.setStartAfter(i), o.collapse(!0), t.removeAllRanges(), t.addRange(o);
  } catch (b) {
    console.error("Error inserting footnote reference:", b);
    return;
  }
  const s = g().querySelector("ol"), c = document.createElement("li");
  c.id = n, c.className = "rte-footnote-item", c.setAttribute("data-type", "footnote"), c.setAttribute("data-number", r.toString());
  const f = document.createElement("div");
  f.className = "rte-footnote-content", f.setAttribute("contenteditable", "true"), f.textContent = e || `Footnote ${r}`, c.appendChild(f);
  const a = document.createElement("a");
  a.className = "rte-footnote-backref", a.href = `#ref-${n}`, a.setAttribute("aria-label", `Back to reference ${r}`), a.textContent = "↩", c.appendChild(a), s == null || s.appendChild(c), m.set(n, {
    id: n,
    number: r,
    content: e
  }), u();
};
function h() {
  const e = l(), t = e == null ? void 0 : e.querySelector(".rte-footnotes");
  return t ? t.querySelectorAll('li[data-type="footnote"]').length + 1 : 1;
}
const p = () => {
  const e = l(), t = e == null ? void 0 : e.querySelector(".rte-footnotes");
  if (!t) return;
  const o = t.querySelectorAll('li[data-type="footnote"]');
  let n = 1;
  o.forEach((r) => {
    const i = r.id;
    r.setAttribute("data-number", n.toString());
    const d = e == null ? void 0 : e.querySelector(`.rte-footnote-ref[data-footnote-id="${i}"]`);
    d && (d.setAttribute("data-number", n.toString()), d.textContent = n.toString());
    const s = m.get(i);
    s && (s.number = n), n++;
  });
}, A = (e) => {
  const t = l(), o = t == null ? void 0 : t.querySelector(`.rte-footnote-ref[data-footnote-id="${e}"]`), n = document.getElementById(e);
  o && o.remove(), n && n.remove(), m.delete(e), p();
};
function u() {
  const e = l();
  e && (e.addEventListener("click", (t) => {
    const o = t.target;
    if (o.classList.contains("rte-footnote-ref")) {
      const n = o.getAttribute("data-footnote-id");
      if (n) {
        const r = document.getElementById(n);
        r && (r.scrollIntoView({ behavior: "smooth", block: "center" }), r.style.backgroundColor = "#fff3cd", setTimeout(() => {
          r.style.backgroundColor = "";
        }, 1500));
      }
    }
  }), e.addEventListener("click", (t) => {
    var n;
    const o = t.target;
    if (o.classList.contains("rte-footnote-backref")) {
      t.preventDefault();
      const r = (n = o.closest("li")) == null ? void 0 : n.id;
      if (r) {
        const i = e.querySelector(`.rte-footnote-ref[data-footnote-id="${r}"]`);
        i && (i.scrollIntoView({ behavior: "smooth", block: "center" }), i.style.backgroundColor = "#fff3cd", setTimeout(() => {
          i.style.backgroundColor = "";
        }, 1500));
      }
    }
  }));
}
typeof window != "undefined" && (document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", u) : u());
export {
  C as FootnotePlugin,
  A as deleteFootnote,
  y as insertFootnoteCommand,
  p as renumberAllFootnotes
};
//# sourceMappingURL=FootnotePlugin.native-BciVc9W6.mjs.map
