const C = ".rte-content, .editora-content", x = '.rte-footnotes[data-type="footnotes"]', p = ".rte-footnote-ref[data-footnote-id]", h = 'li.rte-footnote-item[data-type="footnote"]', k = "__editoraCommandEditorRoot";
let w = !1, T = !1, N = 0;
function _() {
  if (w || typeof document == "undefined") return;
  w = !0;
  const t = document.createElement("style");
  t.id = "editora-footnote-plugin-styles", t.textContent = `
    .rte-footnote-ref {
      display: inline-block;
      font-size: 0.72em;
      line-height: 1;
      vertical-align: super;
      margin-left: 1px;
      color: #1f4dbd;
      cursor: pointer;
      user-select: none;
      border-radius: 4px;
      padding: 0 2px;
      outline: none;
      font-weight: 600;
    }

    .rte-footnote-ref:focus,
    .rte-footnote-ref:focus-visible,
    .rte-footnote-ref.rte-footnote-selected {
      background: rgba(31, 77, 189, 0.12);
      box-shadow: 0 0 0 2px rgba(31, 77, 189, 0.24);
    }

    .rte-footnotes {
      margin-top: 16px;
      padding-top: 10px;
      border-top: 1px solid #d1d5db;
    }

    .rte-footnotes ol {
      margin: 0;
      padding-left: 24px;
    }

    .rte-footnote-item {
      margin: 0 0 8px;
      color: inherit;
    }

    .rte-footnote-content {
      display: inline;
      outline: none;
    }

    .rte-footnote-backref {
      margin-left: 8px;
      color: #1f4dbd;
      text-decoration: none;
      font-size: 0.9em;
      user-select: none;
    }

    .rte-footnote-backref:hover,
    .rte-footnote-backref:focus {
      text-decoration: underline;
    }

    .rte-footnote-highlighted {
      animation: rte-footnote-flash 1s ease;
    }

    @keyframes rte-footnote-flash {
      0% { background-color: rgba(255, 234, 143, 0.9); }
      100% { background-color: transparent; }
    }

    :is([data-theme="dark"], .dark, .editora-theme-dark) .rte-footnote-ref {
      color: #8ab4ff;
    }

    :is([data-theme="dark"], .dark, .editora-theme-dark) .rte-footnote-ref:focus,
    :is([data-theme="dark"], .dark, .editora-theme-dark) .rte-footnote-ref:focus-visible,
    :is([data-theme="dark"], .dark, .editora-theme-dark) .rte-footnote-ref.rte-footnote-selected {
      background: rgba(138, 180, 255, 0.16);
      box-shadow: 0 0 0 2px rgba(138, 180, 255, 0.3);
    }

    :is([data-theme="dark"], .dark, .editora-theme-dark) .rte-footnotes {
      border-top-color: #4b5563;
    }

    :is([data-theme="dark"], .dark, .editora-theme-dark) .rte-footnote-backref {
      color: #8ab4ff;
    }
  `, document.head.appendChild(t);
}
function L(t) {
  return t ? t.nodeType === Node.ELEMENT_NODE ? t : t.parentElement : null;
}
function q(t) {
  if (!t) return null;
  const e = t.querySelector('[contenteditable="true"]');
  return e instanceof HTMLElement ? e : null;
}
function H() {
  if (typeof window == "undefined") return null;
  const t = window[k];
  if (!(t instanceof HTMLElement)) return null;
  window[k] = null;
  const e = t.closest("[data-editora-editor], .rte-editor, .editora-editor, editora-editor") || (t.matches("[data-editora-editor], .rte-editor, .editora-editor, editora-editor") ? t : null);
  if (e) {
    const o = q(e);
    if (o) return o;
    if (e.getAttribute("contenteditable") === "true") return e;
  }
  if (t.getAttribute("contenteditable") === "true")
    return t;
  const n = t.closest('[contenteditable="true"]');
  return n instanceof HTMLElement ? n : null;
}
function B(t) {
  const e = t.closest('[contenteditable="true"]');
  if (!e) return null;
  let n = e, o = n.parentElement;
  for (; o; )
    o.getAttribute("contenteditable") === "true" && (n = o), o = o.parentElement;
  return n;
}
function u(t) {
  const e = L(t);
  if (!e) return null;
  const n = e.closest(C);
  return n || B(e);
}
function $() {
  const t = window.getSelection();
  return !t || t.rangeCount === 0 ? null : u(t.getRangeAt(0).startContainer);
}
function M() {
  const t = H();
  if (t && document.contains(t))
    return t;
  const e = $();
  if (e) return e;
  const n = document.activeElement, o = n ? u(n) : null;
  if (o) return o;
  const r = document.querySelector(C);
  return r || document.querySelector('[contenteditable="true"]');
}
function z() {
  const t = document.createElement("section");
  t.className = "rte-footnotes", t.setAttribute("data-type", "footnotes"), t.setAttribute("contenteditable", "false");
  const e = document.createElement("ol");
  return t.appendChild(e), t;
}
function g(t, e) {
  let n = t.querySelector(x);
  return !n && e && (n = z(), t.appendChild(n)), n ? (n.querySelector("ol") || n.appendChild(document.createElement("ol")), n) : null;
}
function D(t) {
  let e = t.querySelector("ol");
  return e || (e = document.createElement("ol"), t.appendChild(e)), e;
}
function P(t) {
  const e = document.createElement("sup");
  return e.className = "rte-footnote-ref", e.setAttribute("data-footnote-id", t), e.setAttribute("data-number", "0"), e.setAttribute("contenteditable", "false"), e.setAttribute("tabindex", "0"), e.setAttribute("role", "doc-noteref"), e.id = `ref-${t}`, e.textContent = "0", e;
}
function I(t, e) {
  const n = document.createElement("li");
  n.id = t, n.className = "rte-footnote-item", n.setAttribute("data-type", "footnote"), n.setAttribute("data-number", "0"), n.setAttribute("contenteditable", "false");
  const o = document.createElement("div");
  o.className = "rte-footnote-content", o.setAttribute("contenteditable", "true"), o.textContent = e;
  const r = document.createElement("a");
  return r.className = "rte-footnote-backref", r.href = `#ref-${t}`, r.setAttribute("aria-label", "Back to reference"), r.setAttribute("contenteditable", "false"), r.textContent = "↩", n.appendChild(o), n.appendChild(r), n;
}
function j(t) {
  let e = "";
  do
    N += 1, e = `fn-${Date.now().toString(36)}-${N.toString(36)}`;
  while (t.querySelector(`#${CSS.escape(e)}`));
  return e;
}
function v(t) {
  t.classList.remove("rte-footnote-highlighted"), t.classList.add("rte-footnote-highlighted"), window.setTimeout(() => {
    t.classList.remove("rte-footnote-highlighted");
  }, 1e3);
}
function A(t) {
  t && t.dispatchEvent(new Event("input", { bubbles: !0 }));
}
function S(t, e) {
  if (!t || e === t.innerHTML) return;
  const n = window.execEditorCommand || window.executeEditorCommand;
  if (typeof n == "function")
    try {
      n("recordDomTransaction", t, e, t.innerHTML);
    } catch (o) {
    }
}
function X(t, e) {
  return Array.from(t.querySelectorAll(p)).find((o) => o.getAttribute("data-footnote-id") === e) || null;
}
function V(t) {
  const e = window.getSelection();
  if (!e)
    throw new Error("Selection unavailable");
  let n = null;
  if (e.rangeCount > 0) {
    const r = e.getRangeAt(0);
    t.contains(r.commonAncestorContainer) && (n = r.cloneRange());
  }
  if (!n) {
    n = document.createRange();
    const r = g(t, !1);
    r ? (n.setStartBefore(r), n.collapse(!0)) : (n.selectNodeContents(t), n.collapse(!1));
  }
  const o = L(n.commonAncestorContainer);
  if (o != null && o.closest(x)) {
    const r = g(t, !0);
    r && (n.setStartBefore(r), n.collapse(!0));
  }
  return e.removeAllRanges(), e.addRange(n), n;
}
function R(t) {
  const e = t.parentNode;
  if (!e) return;
  const n = Array.from(e.childNodes).indexOf(t);
  if (n < 0) return;
  const o = window.getSelection();
  if (!o) return;
  const r = document.createRange();
  r.setStart(e, n), r.setEnd(e, n + 1), o.removeAllRanges(), o.addRange(r), t.focus({ preventScroll: !0 });
}
function O(t, e) {
  const n = window.getSelection();
  if (!n) return;
  const o = Math.max(0, Math.min(e, t.childNodes.length)), r = document.createRange();
  r.setStart(t, o), r.collapse(!0), n.removeAllRanges(), n.addRange(r);
}
function K(t) {
  if (t.collapsed || t.startContainer !== t.endContainer || t.endOffset !== t.startOffset + 1 || !(t.startContainer instanceof Element || t.startContainer instanceof DocumentFragment))
    return null;
  const e = t.startContainer.childNodes[t.startOffset];
  return !(e instanceof HTMLElement) || !e.matches(p) ? null : e;
}
function b(t, e, n) {
  const { startContainer: o, startOffset: r } = t;
  if (o.nodeType === Node.ELEMENT_NODE) {
    const c = o;
    if (n === "previous") {
      if (r > 0) return c.childNodes[r - 1] || null;
    } else if (r < c.childNodes.length)
      return c.childNodes[r] || null;
  }
  if (o.nodeType === Node.TEXT_NODE && (n === "previous" && r < o.data.length || n === "next" && r > 0))
    return null;
  let i = o;
  for (; i && i !== e; ) {
    const c = n === "previous" ? i.previousSibling : i.nextSibling;
    if (c) return c;
    i = i.parentNode;
  }
  return null;
}
function Y(t, e, n) {
  if (!t.collapsed) return null;
  const o = (c) => c instanceof HTMLElement && c.matches(p) ? c : null, { startContainer: r, startOffset: i } = t;
  if (r.nodeType === Node.ELEMENT_NODE) {
    const c = r;
    return n === "Backspace" && i > 0 ? o(c.childNodes[i - 1] || null) : n === "Delete" ? o(c.childNodes[i] || null) : null;
  }
  if (r.nodeType === Node.TEXT_NODE) {
    const c = r;
    if (n === "Backspace" && i === 0) {
      const a = o(c.previousSibling);
      return a || o(b(t, e, "previous"));
    }
    if (n === "Delete" && i === c.data.length) {
      const a = o(c.nextSibling);
      return a || o(b(t, e, "next"));
    }
  }
  return o(n === "Backspace" ? b(t, e, "previous") : b(t, e, "next"));
}
function m(t) {
  const e = Array.from(t.querySelectorAll(p)).filter((a) => !a.closest(x)), n = g(t, e.length > 0);
  if (!n) return;
  const o = D(n), r = Array.from(o.querySelectorAll(h)), i = /* @__PURE__ */ new Map();
  r.forEach((a) => i.set(a.id, a));
  const c = [];
  e.forEach((a, f) => {
    const l = a.getAttribute("data-footnote-id");
    if (!l) return;
    const s = f + 1;
    a.setAttribute("data-number", String(s)), a.id = `ref-${l}`, a.textContent = String(s);
    let d = i.get(l);
    d || (d = I(l, `Footnote ${s}`)), d.setAttribute("data-number", String(s));
    const E = d.querySelector(".rte-footnote-content");
    E && !(E.textContent || "").trim() && (E.textContent = `Footnote ${s}`);
    const y = d.querySelector(".rte-footnote-backref");
    y && (y.href = `#ref-${l}`, y.setAttribute("aria-label", `Back to reference ${s}`)), c.push(d);
  }), o.innerHTML = "", c.forEach((a) => o.appendChild(a)), c.length === 0 && n.remove();
}
function F(t, e) {
  const n = u(t), o = t.parentNode;
  if (!n || !o) return !1;
  const r = n.innerHTML, i = Array.from(o.childNodes).indexOf(t);
  if (i < 0) return !1;
  const c = t.getAttribute("data-footnote-id") || "";
  if (t.remove(), c) {
    const a = n.querySelector(
      `${h}#${CSS.escape(c)}`
    );
    a == null || a.remove();
  }
  return O(o, i), m(n), S(n, r), A(n), !0;
}
function G() {
  T || typeof document == "undefined" || (T = !0, document.addEventListener("click", (t) => {
    const e = t.target;
    if (!e) return;
    const n = e.closest(p);
    if (n) {
      const f = u(n);
      if (!f || !f.contains(n)) return;
      t.preventDefault(), t.stopPropagation(), R(n), n.classList.add("rte-footnote-selected"), window.setTimeout(() => n.classList.remove("rte-footnote-selected"), 1200);
      const l = n.getAttribute("data-footnote-id");
      if (!l) return;
      const s = f.querySelector(
        `${h}#${CSS.escape(l)}`
      );
      if (!s) return;
      s.scrollIntoView({ behavior: "smooth", block: "center" }), v(s);
      return;
    }
    const o = e.closest(".rte-footnote-backref");
    if (!o) return;
    const r = o.closest(h);
    if (!r) return;
    const i = u(r);
    if (!i || !i.contains(r)) return;
    t.preventDefault(), t.stopPropagation();
    const c = r.id;
    if (!c) return;
    const a = X(i, c);
    a && (a.scrollIntoView({ behavior: "smooth", block: "center" }), v(a), R(a));
  }), document.addEventListener("keydown", (t) => {
    if (t.key !== "Backspace" && t.key !== "Delete") return;
    const e = window.getSelection();
    if (!e || e.rangeCount === 0) return;
    const n = e.getRangeAt(0), o = u(n.commonAncestorContainer);
    if (!o || !o.contains(n.commonAncestorContainer)) return;
    const r = K(n);
    if (r) {
      t.preventDefault(), t.stopPropagation(), F(r, t.key);
      return;
    }
    const i = Y(
      n,
      o,
      t.key
    );
    i && (t.preventDefault(), t.stopPropagation(), F(i, t.key));
  }));
}
const J = (t = "") => {
  const e = M();
  if (!e) return !1;
  const n = e.innerHTML, o = window.getSelection();
  if (!o) return !1;
  let r;
  try {
    r = V(e);
  } catch (d) {
    return !1;
  }
  r.collapsed || (r.collapse(!1), o.removeAllRanges(), o.addRange(r));
  const i = j(e), c = P(i);
  try {
    r.insertNode(c);
  } catch (d) {
    return !1;
  }
  const a = document.createRange();
  a.setStartAfter(c), a.collapse(!0), o.removeAllRanges(), o.addRange(a);
  const f = g(e, !0);
  if (!f) return !1;
  const l = D(f), s = t.trim() || "Footnote";
  return l.appendChild(I(i, s)), m(e), S(e, n), A(e), !0;
}, Q = () => {
  const t = M();
  if (t) {
    m(t);
    return;
  }
  const e = Array.from(document.querySelectorAll(C));
  if (e.length > 0) {
    e.forEach((o) => m(o));
    return;
  }
  const n = document.querySelector('[contenteditable="true"]');
  n && m(n);
}, U = (t) => {
  if (!t) return;
  const e = Array.from(document.querySelectorAll(p)).find((i) => i.getAttribute("data-footnote-id") === t) || null, n = document.getElementById(t), o = u(e || n);
  if (!o) return;
  const r = o.innerHTML;
  e == null || e.remove(), n == null || n.remove(), m(o), S(o, r), A(o);
}, W = () => (_(), G(), {
  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><rect x="3" y="8" width="18" height="2" rx="1"></rect><rect x="3" y="12" width="16" height="2" rx="1"></rect><rect x="3" y="16" width="10" height="1.5" rx="0.75"></rect><text x="19" y="11" font-size="9" font-weight="600" fill="currentColor" font-family="system-ui, sans-serif">1</text></svg>'
    }
  ],
  commands: {
    insertFootnote: () => J()
  },
  keymap: {}
});
export {
  W as FootnotePlugin,
  U as deleteFootnote,
  J as insertFootnoteCommand,
  Q as renumberAllFootnotes
};
//# sourceMappingURL=FootnotePlugin.native-D730OGRm.mjs.map
