const S = ".rte-content, .editora-content", pt = "rte-citations-styles", b = "rte-citations-panel", ot = ".rte-citation-ref[data-citation-id]", W = '.rte-citation-bibliography[data-type="citation-bibliography"]', G = '.rte-citation-footnotes[data-type="citation-footnotes"]', x = ':is([data-theme="dark"], .dark, .editora-theme-dark, .rte-theme-dark)', L = ["apa", "mla", "chicago"], Ot = {
  panelTitle: "Citations",
  panelAriaLabel: "Citations panel",
  styleLabel: "Citation style",
  authorLabel: "Author",
  yearLabel: "Year",
  titleLabel: "Title",
  sourceLabel: "Source",
  urlLabel: "URL",
  noteLabel: "Footnote note",
  insertText: "Insert Citation",
  refreshText: "Refresh Bibliography",
  closeText: "Close",
  bibliographyTitle: "References",
  footnotesTitle: "Citation Notes",
  noCitationsText: "No citations inserted yet.",
  styleButtonPrefix: "Style",
  recentHeading: "Recent citations",
  deleteRecentText: "x",
  summaryPrefix: "Citations",
  invalidMessage: "Author and title are required."
}, f = /* @__PURE__ */ new WeakMap(), T = /* @__PURE__ */ new WeakMap(), ht = /* @__PURE__ */ new WeakMap(), D = /* @__PURE__ */ new WeakMap(), v = /* @__PURE__ */ new Map(), V = /* @__PURE__ */ new WeakMap(), Q = /* @__PURE__ */ new WeakMap(), q = /* @__PURE__ */ new Set();
let I = null, B = null, O = null, k = null, Y = 0, Pt = 0, at = 0, M = null, p = null;
function g(t) {
  return t.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
}
function Dt(t) {
  return t.replace(/\u00A0/g, " ").replace(/\s+/g, " ").trim();
}
function xt(t, e) {
  const n = e(t);
  if (!n) return "";
  const i = n.match(/\d{4}/);
  return i ? i[0] : n;
}
function yt(t, e) {
  const n = e(t);
  return n ? /^https?:\/\//i.test(n) ? n : `https://${n}` : "";
}
function F(t) {
  return t.toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 80);
}
function Ct(t, e) {
  return {
    id: F(e.normalizeText(t.id || "")),
    author: e.normalizeText(t.author || ""),
    year: xt(t.year || "", e.normalizeText) || void 0,
    title: e.normalizeText(t.title || ""),
    source: e.normalizeText(t.source || "") || void 0,
    url: yt(t.url || "", e.normalizeText) || void 0,
    note: e.normalizeText(t.note || "") || void 0
  };
}
function Z(t = {}) {
  const e = t.defaultStyle && L.includes(t.defaultStyle) ? t.defaultStyle : "apa", n = {
    ...Ot,
    ...t.labels || {}
  };
  return {
    defaultStyle: e,
    enableFootnoteSync: t.enableFootnoteSync !== !1,
    debounceMs: Math.max(80, Number(t.debounceMs ?? 220)),
    maxRecentCitations: Math.max(3, Math.min(30, Number(t.maxRecentCitations ?? 8))),
    labels: n,
    normalizeText: t.normalizeText || Dt,
    generateCitationId: typeof t.generateCitationId == "function" ? t.generateCitationId : void 0
  };
}
function wt(t) {
  return t ? t.nodeType === Node.ELEMENT_NODE ? t : t.parentElement : null;
}
function lt(t) {
  return t.closest("[data-editora-editor], .rte-editor, .editora-editor, editora-editor") || t;
}
function X(t) {
  return t ? (t.getAttribute("data-theme") || t.getAttribute("theme") || "").toLowerCase() === "dark" ? !0 : t.classList.contains("dark") || t.classList.contains("editora-theme-dark") || t.classList.contains("rte-theme-dark") : !1;
}
function Ht(t) {
  const e = lt(t);
  if (X(e)) return !0;
  const n = e.closest("[data-theme], [theme], .dark, .editora-theme-dark, .rte-theme-dark");
  return X(n) ? !0 : X(document.documentElement) || X(document.body);
}
function tt(t, e) {
  t.classList.remove("rte-citations-theme-dark"), Ht(e) && t.classList.add("rte-citations-theme-dark");
}
function h(t, e = !0) {
  if (t?.contentElement instanceof HTMLElement) return t.contentElement;
  if (t?.editorElement instanceof HTMLElement) {
    const o = t.editorElement;
    if (o.matches(S)) return o;
    const r = o.querySelector(S);
    if (r instanceof HTMLElement) return r;
  }
  const n = window.getSelection();
  if (n && n.rangeCount > 0) {
    const o = n.getRangeAt(0).startContainer, a = wt(o)?.closest(S);
    if (a) return a;
  }
  const i = document.activeElement;
  if (i) {
    if (i.matches(S)) return i;
    const o = i.closest(S);
    if (o) return o;
  }
  return p && p.isConnected ? p : (p && !p.isConnected && (p = null), e ? document.querySelector(S) : null);
}
function $(t) {
  return t.getAttribute("contenteditable") === "false" || t.getAttribute("data-readonly") === "true";
}
function K(t) {
  const e = wt(t);
  return e ? !!(e.closest(W) || e.closest(G)) : !1;
}
function Ft(t, e) {
  if (at += 1, e.generateCitationId) {
    const n = e.generateCitationId({ editor: t, index: at }), i = F(e.normalizeText(n || ""));
    if (i) return i;
  }
  return `cite-${Date.now().toString(36)}-${at.toString(36)}`;
}
function st(t) {
  return t.map((e) => (e || "").trim()).filter(Boolean).join(" ").trim();
}
function _t(t, e) {
  const n = t.author || "Unknown", i = t.year || "n.d.";
  return e === "mla" ? `(${n} ${i})` : e === "chicago" ? `(${n} ${i})` : `(${n}, ${i})`;
}
function ut(t, e) {
  const n = t.author || "Unknown", i = t.year || "n.d.", o = t.title || "Untitled", r = t.source || "", a = t.url || "";
  return st(e === "mla" ? [
    `${n}.`,
    `"${o}."`,
    r ? `${r},` : "",
    `${i}.`,
    a
  ] : e === "chicago" ? [
    `${n}.`,
    `${o}.`,
    r ? `${r}.` : "",
    `(${i}).`,
    a
  ] : [
    `${n}.`,
    `(${i}).`,
    `${o}.`,
    r ? `${r}.` : "",
    a
  ]);
}
function R(t) {
  return Array.from(t.querySelectorAll(ot)).filter(
    (e) => !e.closest(W) && !e.closest(G)
  );
}
function vt(t, e) {
  const n = F(e.normalizeText(t.getAttribute("data-citation-id") || ""));
  if (!n) return null;
  const i = e.normalizeText(t.getAttribute("data-citation-author") || ""), o = e.normalizeText(t.getAttribute("data-citation-title") || "");
  return {
    id: n,
    author: i || "Unknown",
    year: xt(t.getAttribute("data-citation-year") || "", e.normalizeText) || void 0,
    title: o || "Untitled",
    source: e.normalizeText(t.getAttribute("data-citation-source") || "") || void 0,
    url: yt(t.getAttribute("data-citation-url") || "", e.normalizeText) || void 0,
    note: e.normalizeText(t.getAttribute("data-citation-note") || "") || void 0
  };
}
function Et(t, e, n) {
  t.classList.add("rte-citation-ref"), t.setAttribute("data-citation-id", e.id), t.setAttribute("data-citation-author", e.author || ""), t.setAttribute("data-citation-year", e.year || ""), t.setAttribute("data-citation-title", e.title || ""), t.setAttribute("data-citation-source", e.source || ""), t.setAttribute("data-citation-url", e.url || ""), t.setAttribute("data-citation-note", e.note || ""), t.setAttribute("contenteditable", "false"), t.setAttribute("tabindex", "0"), t.setAttribute("role", "doc-biblioref"), t.setAttribute("data-style", n), t.textContent = _t(e, n);
}
function it(t, e, n) {
  const i = lt(t);
  Array.from(
    i.querySelectorAll(
      `.rte-toolbar-button[data-command="${e}"], .editora-toolbar-button[data-command="${e}"]`
    )
  ).forEach((r) => {
    r.classList.toggle("active", n), r.setAttribute("data-active", n ? "true" : "false"), r.setAttribute("aria-pressed", n ? "true" : "false");
  });
}
function N(t, e) {
  const n = T.get(t);
  return n && L.includes(n) ? n : e?.defaultStyle || "apa";
}
function j(t, e) {
  const n = R(t), i = /* @__PURE__ */ new Map();
  return n.forEach((o) => {
    const r = vt(o, e);
    if (!r) return;
    if (!i.has(r.id)) {
      i.set(r.id, r);
      return;
    }
    const a = i.get(r.id);
    i.set(r.id, {
      ...a,
      author: a.author || r.author,
      title: a.title || r.title,
      year: a.year || r.year,
      source: a.source || r.source,
      url: a.url || r.url,
      note: a.note || r.note
    });
  }), Array.from(i.values());
}
function qt(t, e, n) {
  const i = Ct(e, n);
  if (!i.id || !i.author || !i.title) return;
  const r = (D.get(t) || []).filter((a) => a.id !== i.id);
  D.set(t, [i, ...r].slice(0, n.maxRecentCitations));
}
function rt(t, e, n) {
  const i = D.get(t) || [], o = /* @__PURE__ */ new Map();
  e.slice(Math.max(0, e.length - n.maxRecentCitations)).reverse().forEach((a) => {
    a.id && o.set(a.id, a);
  }), i.forEach((a) => {
    !a.id || o.has(a.id) || o.set(a.id, a);
  });
  const r = Array.from(o.values()).slice(0, n.maxRecentCitations);
  return D.set(t, r), r;
}
function St(t, e, n, i) {
  const o = F(i.normalizeText(e || ""));
  return o && rt(t, n, i).find((a) => a.id === o) || null;
}
function Kt(t, e, n) {
  const i = F(n.normalizeText(e || ""));
  if (!i) return !1;
  const o = D.get(t) || [], r = o.filter((a) => a.id !== i);
  return r.length === o.length ? !1 : (D.set(t, r), !0);
}
function $t(t, e, n) {
  const i = document.createElement("section");
  i.className = t, i.setAttribute("data-type", e), i.setAttribute("contenteditable", "false"), i.setAttribute("aria-label", n), e === "citation-bibliography" ? i.setAttribute("role", "doc-bibliography") : e === "citation-footnotes" && i.setAttribute("role", "doc-endnotes");
  const o = document.createElement("h3");
  o.className = "rte-citation-section-title", o.textContent = n;
  const r = document.createElement("ol");
  return r.className = "rte-citation-list", r.setAttribute("role", "list"), i.appendChild(o), i.appendChild(r), i;
}
function jt(t, e) {
  let n = t.querySelector(W);
  n || (n = $t("rte-citation-bibliography", "citation-bibliography", e.labels.bibliographyTitle), t.appendChild(n));
  const i = n.querySelector(".rte-citation-section-title");
  return i && (i.textContent = e.labels.bibliographyTitle), n.setAttribute("aria-label", e.labels.bibliographyTitle), n;
}
function Ut(t, e) {
  let n = t.querySelector(G);
  n || (n = $t("rte-citation-footnotes", "citation-footnotes", e.labels.footnotesTitle), t.appendChild(n));
  const i = n.querySelector(".rte-citation-section-title");
  return i && (i.textContent = e.labels.footnotesTitle), n.setAttribute("aria-label", e.labels.footnotesTitle), n;
}
function At(t, e) {
  t.querySelector(e)?.remove();
}
function Wt(t, e, n, i) {
  if (e.length === 0) {
    At(t, W);
    return;
  }
  const r = jt(t, n).querySelector(".rte-citation-list");
  if (!r) return;
  const a = document.createDocumentFragment();
  e.forEach((s, u) => {
    const c = document.createElement("li");
    c.className = "rte-citation-item", c.id = `rte-citation-entry-${s.id}`, c.setAttribute("data-citation-id", s.id), c.setAttribute("data-citation-number", String(u + 1)), c.textContent = ut(s, i), a.appendChild(c);
  }), r.innerHTML = "", r.appendChild(a);
}
function Gt(t, e, n, i) {
  if (!n.enableFootnoteSync || e.length === 0) {
    At(t, G), R(t).forEach((l) => {
      l.removeAttribute("data-footnote-number"), l.removeAttribute("data-footnote-target");
    });
    return;
  }
  const r = Ut(t, n).querySelector(".rte-citation-list");
  if (!r) return;
  const a = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map();
  e.forEach((l, d) => {
    s.set(l.id, d + 1);
  });
  const u = /* @__PURE__ */ new Map();
  R(t).forEach((l) => {
    const d = l.getAttribute("data-citation-id") || "";
    if (!d || !s.has(d)) return;
    const m = (u.get(d) || 0) + 1;
    u.set(d, m);
    const w = `rte-citation-ref-${d}-${m}`;
    l.id = w;
    const A = s.get(d);
    l.setAttribute("data-footnote-number", String(A)), l.setAttribute("data-footnote-target", `rte-citation-note-${d}`), a.has(d) || a.set(d, w);
  });
  const c = document.createDocumentFragment();
  e.forEach((l, d) => {
    const m = document.createElement("li");
    m.className = "rte-citation-item rte-citation-footnote-item", m.id = `rte-citation-note-${l.id}`, m.setAttribute("data-citation-id", l.id);
    const w = document.createElement("span");
    w.className = "rte-citation-footnote-number", w.textContent = `${d + 1}. `;
    const A = document.createElement("span");
    A.className = "rte-citation-footnote-text";
    const Bt = l.note ? `${l.note}. ` : "";
    A.textContent = `${Bt}${ut(l, i)}`, m.appendChild(w), m.appendChild(A);
    const mt = a.get(l.id);
    if (mt) {
      const _ = document.createElement("a");
      _.className = "rte-citation-backref", _.href = `#${mt}`, _.setAttribute("aria-label", `Back to citation ${d + 1}`), _.textContent = "Back", m.appendChild(_);
    }
    c.appendChild(m);
  }), r.innerHTML = "", r.appendChild(c);
}
function Vt(t, e, n, i) {
  const o = ut(e, n);
  t.setAttribute("data-citation-number", String(i)), t.setAttribute("aria-label", `Citation ${i}: ${o}`);
}
function Yt(t, e, n) {
  const i = R(t);
  let o = `${e}:${n ? "1" : "0"}:${i.length}`;
  return i.forEach((r) => {
    o += `|${r.getAttribute("data-citation-id") || ""}`, o += `|${r.getAttribute("data-citation-author") || ""}`, o += `|${r.getAttribute("data-citation-year") || ""}`, o += `|${r.getAttribute("data-citation-title") || ""}`, o += `|${r.getAttribute("data-citation-source") || ""}`, o += `|${r.getAttribute("data-citation-url") || ""}`, o += `|${r.getAttribute("data-citation-note") || ""}`;
  }), o;
}
function H(t) {
  const e = v.get(t);
  if (!e) return;
  const n = f.get(t) || M;
  if (!n) return;
  const i = N(t, n), o = j(t, n), r = rt(t, o, n), a = e.querySelector(".rte-citations-status"), s = e.querySelector('[data-action="cycle-style"]'), u = e.querySelector(".rte-citations-recent-list");
  if (a) {
    const c = o.length;
    a.textContent = `${n.labels.summaryPrefix}: ${c} | Style: ${i.toUpperCase()} | Footnotes: ${n.enableFootnoteSync ? "On" : "Off"}`;
  }
  if (s && (s.textContent = `${n.labels.styleButtonPrefix}: ${i.toUpperCase()}`, s.setAttribute("aria-label", `${n.labels.styleButtonPrefix}: ${i.toUpperCase()}`)), u) {
    if (r.length === 0) {
      u.innerHTML = `<li class="rte-citations-empty">${g(n.labels.noCitationsText)}</li>`;
      return;
    }
    u.innerHTML = r.map(
      (c) => `
          <li class="rte-citations-recent-item">
            <div class="rte-citations-recent-row">
              <button
                type="button"
                class="rte-citations-recent-btn"
                data-action="insert-from-recent"
                data-citation-id="${g(c.id)}"
                aria-label="Insert citation: ${g(c.title)}"
              >
                <span class="rte-citations-recent-title">${g(c.title)}</span>
                <span class="rte-citations-recent-meta">${g(c.author)}${c.year ? ` (${g(c.year)})` : ""}</span>
              </button>
              <button
                type="button"
                class="rte-citations-recent-delete"
                data-action="delete-by-id"
                data-citation-id="${g(c.id)}"
                aria-label="Delete citation: ${g(c.title)}"
              >${g(n.labels.deleteRecentText)}</button>
            </div>
          </li>
        `
    ).join("");
  }
}
function y(t, e, n = !1) {
  const i = N(t, e), o = Yt(t, i, e.enableFootnoteSync);
  if (!n && ht.get(t) === o)
    return j(t, e);
  const r = R(t), a = /* @__PURE__ */ new Map();
  r.forEach((c) => {
    const l = vt(c, e);
    l && (a.has(l.id) || a.set(l.id, l));
  });
  const s = Array.from(a.values());
  rt(t, s, e);
  const u = /* @__PURE__ */ new Map();
  return s.forEach((c, l) => {
    u.set(c.id, l + 1);
  }), r.forEach((c) => {
    const l = c.getAttribute("data-citation-id") || "", d = a.get(l);
    if (!d) return;
    Et(c, d, i);
    const m = u.get(d.id) || 1;
    Vt(c, d, i, Math.max(1, m));
  }), Wt(t, s, e, i), Gt(t, s, e, i), ht.set(t, o), H(t), t.dispatchEvent(
    new CustomEvent("editora:citations-refreshed", {
      bubbles: !0,
      detail: {
        citations: s,
        style: i,
        footnoteSync: e.enableFootnoteSync
      }
    })
  ), s;
}
function kt(t) {
  const e = Q.get(t);
  typeof e == "number" && (window.clearTimeout(e), q.delete(e), Q.delete(t));
}
function Tt(t) {
  const e = f.get(t) || M;
  if (!e) return;
  kt(t);
  const n = window.setTimeout(() => {
    q.delete(n), Q.delete(t), y(t, e, !1);
  }, e.debounceMs);
  q.add(n), Q.set(t, n);
}
function Xt(t) {
  const e = window.getSelection();
  if (!e) throw new Error("Selection unavailable");
  if (e.rangeCount > 0) {
    const a = e.getRangeAt(0);
    if (t.contains(a.commonAncestorContainer) && !K(a.commonAncestorContainer))
      return a.cloneRange();
  }
  const n = document.createRange(), i = t.querySelector(W), o = t.querySelector(G), r = i || o;
  return r ? (n.setStartBefore(r), n.collapse(!0), n) : (n.selectNodeContents(t), n.collapse(!1), n);
}
function dt(t) {
  t.dispatchEvent(new Event("input", { bubbles: !0 }));
}
function ft(t, e) {
  if (e === t.innerHTML) return;
  const n = window.execEditorCommand || window.executeEditorCommand;
  if (typeof n == "function")
    try {
      n("recordDomTransaction", t, e, t.innerHTML);
    } catch {
    }
}
function Mt(t, e) {
  const n = window.getSelection();
  if (!n) return;
  const i = document.createRange();
  if (t.nodeType === Node.TEXT_NODE) {
    const o = t, r = Math.max(0, Math.min(e, o.length));
    i.setStart(o, r);
  } else {
    const o = t.childNodes.length, r = Math.max(0, Math.min(e, o));
    i.setStart(t, r);
  }
  i.collapse(!0), n.removeAllRanges(), n.addRange(i);
}
function Lt(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(ot) ? null : e;
}
function J(t, e, n) {
  const { startContainer: i, startOffset: o } = t;
  if (i.nodeType === Node.ELEMENT_NODE) {
    const a = i;
    if (n === "previous") {
      if (o > 0) return a.childNodes[o - 1] || null;
    } else if (o < a.childNodes.length)
      return a.childNodes[o] || null;
  }
  if (i.nodeType === Node.TEXT_NODE && (n === "previous" && o < i.data.length || n === "next" && o > 0))
    return null;
  let r = i;
  for (; r && r !== e; ) {
    const a = n === "previous" ? r.previousSibling : r.nextSibling;
    if (a) return a;
    r = r.parentNode;
  }
  return null;
}
function Rt(t, e, n) {
  if (!t.collapsed) return null;
  const i = (a) => a instanceof HTMLElement && a.matches(ot) ? a : null, { startContainer: o, startOffset: r } = t;
  if (o.nodeType === Node.ELEMENT_NODE) {
    const a = o;
    return n === "Backspace" && r > 0 ? i(a.childNodes[r - 1] || null) : n === "Delete" ? i(a.childNodes[r] || null) : null;
  }
  if (o.nodeType === Node.TEXT_NODE) {
    const a = o;
    if (n === "Backspace" && r === 0) {
      const s = i(a.previousSibling);
      return s || i(J(t, e, "previous"));
    }
    if (n === "Delete" && r === a.data.length) {
      const s = i(a.nextSibling);
      return s || i(J(t, e, "next"));
    }
  }
  return i(n === "Backspace" ? J(t, e, "previous") : J(t, e, "next"));
}
function P(t, e, n) {
  const i = t.closest(S);
  if (!i || K(t)) return !1;
  const o = t.parentNode;
  if (!o) return !1;
  const r = i.innerHTML, a = Array.from(o.childNodes).indexOf(t);
  if (a < 0) return !1;
  const s = t.nextSibling;
  return s instanceof Text && (s.data === " " ? s.remove() : s.data.startsWith(" ") && (s.data = s.data.slice(1))), t.remove(), Mt(o, a), y(i, n, !0), ft(i, r), dt(i), e === "Delete" && i.focus({ preventScroll: !0 }), !0;
}
function Jt(t, e, n) {
  if (t.key !== "Backspace" && t.key !== "Delete") return !1;
  const i = t.key, o = t.target;
  if (o?.matches(ot) && e.contains(o) && !K(o))
    return t.preventDefault(), t.stopPropagation(), P(o, i, n);
  const r = window.getSelection();
  if (!r || r.rangeCount === 0) return !1;
  const a = r.getRangeAt(0);
  if (!e.contains(a.commonAncestorContainer) || K(a.commonAncestorContainer)) return !1;
  const s = Lt(a);
  if (s)
    return t.preventDefault(), t.stopPropagation(), P(s, i, n);
  const u = Rt(a, e, i);
  return u ? (t.preventDefault(), t.stopPropagation(), P(u, i, n)) : !1;
}
function Nt(t, e, n) {
  const i = F(n.normalizeText(e || ""));
  if (!i) return !1;
  const o = R(t).filter((a) => a.getAttribute("data-citation-id") === i);
  if (o.length === 0) return !1;
  if (o.length === 1)
    return P(o[0], "Delete", n);
  const r = t.innerHTML;
  return o.forEach((a) => {
    const s = a.nextSibling;
    s instanceof Text && (s.data === " " ? s.remove() : s.data.startsWith(" ") && (s.data = s.data.slice(1))), a.remove();
  }), Mt(t, t.childNodes.length), y(t, n, !0), ft(t, r), dt(t), t.focus({ preventScroll: !0 }), !0;
}
function Zt(t, e) {
  const n = window.getSelection();
  if (!n || n.rangeCount === 0) return !1;
  const i = n.getRangeAt(0);
  if (!t.contains(i.commonAncestorContainer) || K(i.commonAncestorContainer)) return !1;
  const o = Lt(i);
  if (o)
    return P(o, "Delete", e);
  const r = Rt(i, t, "Backspace");
  return r ? P(r, "Backspace", e) : !1;
}
function et(t, e, n) {
  const i = Ct(e, n);
  if (!i.author || !i.title) return !1;
  i.id || (i.id = Ft(t, n));
  const o = t.innerHTML;
  let r;
  try {
    r = Xt(t);
  } catch {
    return !1;
  }
  const a = window.getSelection();
  if (!a) return !1;
  r.collapsed || r.deleteContents();
  const s = document.createElement("span");
  Et(s, i, N(t, n));
  try {
    r.insertNode(s);
  } catch {
    return !1;
  }
  const u = document.createTextNode(" ");
  s.nextSibling ? s.parentNode?.insertBefore(u, s.nextSibling) : s.parentNode?.appendChild(u);
  const c = document.createRange();
  if (u.parentNode) {
    const l = Array.from(u.parentNode.childNodes).indexOf(u) + 1;
    c.setStart(u.parentNode, Math.max(0, l));
  } else
    c.setStartAfter(s);
  return c.collapse(!0), a.removeAllRanges(), a.addRange(c), qt(t, i, n), y(t, n, !0), ft(t, o), dt(t), !0;
}
function Qt(t, e) {
  if (!e) return !1;
  const n = R(t).find((r) => r.getAttribute("data-citation-id") === e) || null;
  if (!n) return !1;
  n.scrollIntoView({ behavior: "smooth", block: "center", inline: "nearest" }), n.focus({ preventScroll: !0 });
  const i = window.getSelection();
  if (!i) return !0;
  const o = document.createRange();
  return o.selectNode(n), i.removeAllRanges(), i.addRange(o), !0;
}
function nt(t) {
  return V.get(t) === !0;
}
function U(t, e = !1) {
  const n = v.get(t);
  n && (n.classList.remove("show"), V.set(t, !1), it(t, "toggleCitationsPanel", !1), e && t.focus({ preventScroll: !0 }));
}
function te(t) {
  v.forEach((e, n) => {
    n !== t && U(n, !1);
  });
}
function ct(t, e) {
  if (!e.classList.contains("show")) return;
  const i = lt(t).getBoundingClientRect(), o = Math.min(window.innerWidth - 20, 380), r = Math.max(10, window.innerWidth - o - 10), a = Math.min(Math.max(10, i.right - o), r), s = Math.max(10, Math.min(window.innerHeight - 10 - 260, i.top + 10));
  e.style.width = `${o}px`, e.style.left = `${a}px`, e.style.top = `${s}px`, e.style.maxHeight = `${Math.max(260, window.innerHeight - 24)}px`;
}
function C(t, e) {
  return t.querySelector(`[data-field="${e}"]`);
}
function ee(t) {
  const e = C(t, "author")?.value || "", n = C(t, "year")?.value || "", i = C(t, "title")?.value || "", o = C(t, "source")?.value || "", r = C(t, "url")?.value || "", a = C(t, "note")?.value || "";
  return {
    author: e,
    year: n,
    title: i,
    source: o,
    url: r,
    note: a
  };
}
function E(t, e) {
  const n = t.querySelector(".rte-citations-live");
  n && (n.textContent = e);
}
function ne(t, e, n) {
  const i = C(e, "style");
  i && (i.value = N(t, n));
}
function zt(t, e, n) {
  const i = L.includes(e) ? e : n.defaultStyle;
  return T.set(t, i), y(t, n, !0), i;
}
function bt(t, e) {
  const n = N(t, e), i = L.indexOf(n), o = L[(i + 1) % L.length];
  return T.set(t, o), y(t, e, !0), o;
}
function oe(t) {
  const e = v.get(t);
  if (e) return e;
  const n = f.get(t) || M || Z(), i = `rte-citations-panel-${Pt++}`, o = document.createElement("section");
  o.className = b, o.id = i, o.setAttribute("role", "dialog"), o.setAttribute("aria-modal", "false"), o.setAttribute("aria-label", n.labels.panelAriaLabel), o.setAttribute("tabindex", "-1"), o.innerHTML = `
    <header class="rte-citations-header">
      <h2 class="rte-citations-title">${g(n.labels.panelTitle)}</h2>
      <button type="button" class="rte-citations-icon-btn" data-action="close" aria-label="${g(n.labels.closeText)}">✕</button>
    </header>
    <div class="rte-citations-body">
      <p class="rte-citations-status" aria-live="polite"></p>

      <div class="rte-citations-grid">
        <label class="rte-citations-label">
          ${g(n.labels.styleLabel)}
          <select data-field="style" class="rte-citations-field">
            <option value="apa">APA</option>
            <option value="mla">MLA</option>
            <option value="chicago">Chicago</option>
          </select>
        </label>
        <label class="rte-citations-label">
          ${g(n.labels.authorLabel)}
          <input type="text" data-field="author" class="rte-citations-field" autocomplete="off" />
        </label>
        <label class="rte-citations-label">
          ${g(n.labels.yearLabel)}
          <input type="text" data-field="year" class="rte-citations-field" inputmode="numeric" autocomplete="off" />
        </label>
        <label class="rte-citations-label">
          ${g(n.labels.titleLabel)}
          <input type="text" data-field="title" class="rte-citations-field" autocomplete="off" />
        </label>
        <label class="rte-citations-label">
          ${g(n.labels.sourceLabel)}
          <input type="text" data-field="source" class="rte-citations-field" autocomplete="off" />
        </label>
        <label class="rte-citations-label">
          ${g(n.labels.urlLabel)}
          <input type="url" data-field="url" class="rte-citations-field" autocomplete="off" />
        </label>
        <label class="rte-citations-label rte-citations-label-note">
          ${g(n.labels.noteLabel)}
          <textarea data-field="note" class="rte-citations-field" rows="2"></textarea>
        </label>
      </div>

      <div class="rte-citations-controls" role="toolbar" aria-label="Citation actions">
        <button type="button" class="rte-citations-btn rte-citations-btn-primary" data-action="insert">${g(n.labels.insertText)}</button>
        <button type="button" class="rte-citations-btn" data-action="refresh">${g(n.labels.refreshText)}</button>
        <button type="button" class="rte-citations-btn" data-action="cycle-style"></button>
      </div>

      <section class="rte-citations-recent" aria-label="${g(n.labels.recentHeading)}">
        <h3 class="rte-citations-recent-heading">${g(n.labels.recentHeading)}</h3>
        <ul class="rte-citations-recent-list" role="list"></ul>
      </section>

      <p class="rte-citations-shortcut">Shortcut: Ctrl/Cmd + Alt + Shift + C</p>
      <span class="rte-citations-live" aria-live="polite"></span>
    </div>
  `, o.addEventListener("click", (a) => {
    const s = a.target;
    if (!s) return;
    const u = s.closest("[data-action]");
    if (!u) return;
    const c = u.getAttribute("data-action") || "", l = f.get(t) || M || n;
    if (f.set(t, l), c === "close") {
      U(t, !0);
      return;
    }
    if (c === "insert") {
      if ($(t)) return;
      const d = ee(o);
      if (!l.normalizeText(d.author) || !l.normalizeText(d.title)) {
        E(o, l.labels.invalidMessage);
        return;
      }
      if (!et(t, d, l)) {
        E(o, l.labels.invalidMessage);
        return;
      }
      E(o, "Citation inserted.");
      const w = C(o, "title"), A = C(o, "note");
      w && (w.value = ""), A && (A.value = "");
      return;
    }
    if (c === "refresh") {
      const d = y(t, l, !0);
      E(o, `Refreshed ${d.length} citation${d.length === 1 ? "" : "s"}.`);
      return;
    }
    if (c === "cycle-style") {
      const d = bt(t, l);
      ne(t, o, l), E(o, `Style changed to ${d.toUpperCase()}.`);
      return;
    }
    if (c === "insert-from-recent") {
      if ($(t)) return;
      const d = u.getAttribute("data-citation-id") || "", m = St(t, d, j(t, l), l);
      if (!m) return;
      et(t, m, l), E(o, `Inserted citation: ${m.title}.`);
      return;
    }
    if (c === "delete-by-id") {
      if ($(t)) return;
      const d = u.getAttribute("data-citation-id") || "";
      if (Nt(t, d, l)) {
        E(o, "Citation deleted.");
        return;
      }
      Kt(t, d, l) && (H(t), E(o, "Removed from recent citations."));
    }
  }), o.addEventListener("keydown", (a) => {
    if (a.key === "Escape") {
      a.preventDefault(), U(t, !0);
      return;
    }
    const s = a.target;
    if (!s || !s.matches(".rte-citations-recent-btn") || a.key !== "ArrowDown" && a.key !== "ArrowUp") return;
    const u = Array.from(o.querySelectorAll(".rte-citations-recent-btn"));
    if (u.length === 0) return;
    const c = u.indexOf(s);
    if (c < 0) return;
    a.preventDefault();
    const l = a.key === "ArrowDown" ? 1 : -1, d = (c + l + u.length) % u.length;
    u[d].focus();
  });
  const r = C(o, "style");
  return r?.addEventListener("change", () => {
    const a = f.get(t) || M || n, s = r.value;
    zt(t, s, a), E(o, `Style changed to ${s.toUpperCase()}.`);
  }), tt(o, t), document.body.appendChild(o), v.set(t, o), V.set(t, !1), H(t), o;
}
function z(t) {
  const e = oe(t);
  te(t), e.classList.add("show"), V.set(t, !0), it(t, "toggleCitationsPanel", !0), tt(e, t), ct(t, e), H(t), C(e, "author")?.focus();
}
function It(t, e) {
  const n = nt(t);
  return (typeof e == "boolean" ? e : !n) ? z(t) : U(t, !1), !0;
}
function gt(t) {
  return typeof t.key == "string" ? t.key.toLowerCase() : "";
}
function ie(t) {
  const e = gt(t);
  return (t.metaKey || t.ctrlKey) && t.altKey && t.shiftKey && e === "c";
}
function re(t) {
  const e = gt(t);
  return (t.metaKey || t.ctrlKey) && t.altKey && t.shiftKey && e === "b";
}
function ae(t) {
  const e = gt(t);
  return (t.metaKey || t.ctrlKey) && t.altKey && t.shiftKey && e === "j";
}
function se(t) {
  M = t, I || (I = (e) => {
    const i = e.target?.closest(S);
    if (!i) return;
    p = i, f.has(i) || f.set(i, t), T.has(i) || T.set(i, t.defaultStyle);
    const o = v.get(i);
    o && (tt(o, i), ct(i, o)), it(i, "toggleCitationsPanel", nt(i));
  }, document.addEventListener("focusin", I, !0)), B || (B = (e) => {
    const i = e.target?.closest(S);
    i && (p = i, Tt(i));
  }, document.addEventListener("input", B, !0)), O || (O = (e) => {
    if (e.defaultPrevented) return;
    const i = !!e.target?.closest(`.${b} input, .${b} textarea, .${b} select`), o = h(void 0, !1);
    if (!o || $(o)) return;
    const r = f.get(o) || M || t;
    if (f.set(o, r), p = o, e.key === "Escape" && nt(o)) {
      e.preventDefault(), U(o, !0);
      return;
    }
    if (!i && !Jt(e, o, r)) {
      if (ie(e)) {
        e.preventDefault(), e.stopPropagation(), It(o);
        return;
      }
      if (re(e)) {
        e.preventDefault(), e.stopPropagation(), y(o, r, !0), z(o);
        return;
      }
      ae(e) && (e.preventDefault(), e.stopPropagation(), bt(o, r));
    }
  }, document.addEventListener("keydown", O, !0)), k || (k = () => {
    v.forEach((e, n) => {
      if (!n.isConnected || !e.isConnected) {
        kt(n), e.remove(), v.delete(n), V.delete(n);
        return;
      }
      tt(e, n), ct(n, e);
    });
  }, window.addEventListener("scroll", k, !0), window.addEventListener("resize", k));
}
function ce() {
  I && (document.removeEventListener("focusin", I, !0), I = null), B && (document.removeEventListener("input", B, !0), B = null), O && (document.removeEventListener("keydown", O, !0), O = null), k && (window.removeEventListener("scroll", k, !0), window.removeEventListener("resize", k), k = null), v.forEach((t) => t.remove()), v.clear(), M = null, p = null;
}
function le() {
  if (typeof document > "u" || document.getElementById(pt)) return;
  const t = document.createElement("style");
  t.id = pt, t.textContent = `
    .rte-toolbar-group-items.citations,
    .editora-toolbar-group-items.citations {
      display: flex;
      border: 1px solid #ccc;
      border-radius: 3px;
      background: #fff;
    }

    .rte-toolbar-group-items.citations .rte-toolbar-button,
    .editora-toolbar-group-items.citations .editora-toolbar-button {
      border: none;
      border-radius: 0;
      border-right: 1px solid #ccc;
    }

    .rte-toolbar-group-items.citations .rte-toolbar-item:last-child .rte-toolbar-button,
    .editora-toolbar-group-items.citations .editora-toolbar-item:last-child .editora-toolbar-button {
      border-right: none;
    }

    .rte-toolbar-button[data-command="toggleCitationsPanel"].active,
    .editora-toolbar-button[data-command="toggleCitationsPanel"].active {
      background: #ccc;
    }

    ${x} .rte-toolbar-group-items.citations,
    ${x} .editora-toolbar-group-items.citations,
    .${b}.rte-citations-theme-dark {
      border-color: #566275;
    }

    ${x} .rte-toolbar-group-items.citations .rte-toolbar-button[data-command="refreshCitations"] svg,
    ${x} .editora-toolbar-group-items.citations .editora-toolbar-button[data-command="refreshCitations"] svg
    {
      fill: none;
    }
    ${x} .rte-toolbar-group-items.citations .rte-toolbar-button,
    ${x} .editora-toolbar-group-items.citations .editora-toolbar-button
    {
      border-color: #566275;
    }
    ${x} .rte-toolbar-button[data-command="toggleCitationsPanel"].active,
    ${x} .editora-toolbar-button[data-command="toggleCitationsPanel"].active {
      background: linear-gradient(180deg, #5eaaf6 0%, #4a95de 100%);
    }
    .${b} {
      position: fixed;
      z-index: 1500;
      right: 16px;
      top: 16px;
      width: min(380px, calc(100vw - 20px));
      max-height: calc(100vh - 24px);
      display: none;
      flex-direction: column;
      border-radius: 12px;
      border: 1px solid #d1d5db;
      background: #ffffff;
      color: #0f172a;
      box-shadow: 0 18px 38px rgba(15, 23, 42, 0.16);
      overflow: hidden;
    }

    .${b}.show {
      display: flex;
    }

    .${b}.rte-citations-theme-dark {
      background: #0f172a;
      color: #e2e8f0;
      border-color: #334155;
      box-shadow: 0 20px 40px rgba(2, 6, 23, 0.5);
    }

    .rte-citations-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 10px 12px;
      border-bottom: 1px solid #e2e8f0;
      background: #f8fafc;
    }

    .${b}.rte-citations-theme-dark .rte-citations-header {
      border-bottom-color: #334155;
      background: #111827;
    }

    .rte-citations-title {
      margin: 0;
      font-size: 14px;
      font-weight: 700;
    }

    .rte-citations-icon-btn {
      border: 1px solid #cbd5e1;
      background: #ffffff;
      color: #0f172a;
      border-radius: 6px;
      cursor: pointer;
      min-width: 34px;
      min-height: 34px;
      width: 34px;
      height: 34px;
      padding: 0;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font-size: 16px;
      line-height: 1;
      font-weight: 600;
    }

    .rte-citations-icon-btn:hover,
    .rte-citations-icon-btn:focus-visible {
      outline: none;
      border-color: #3b82f6;
      box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
    }

    .${b}.rte-citations-theme-dark .rte-citations-icon-btn {
      background: #0f172a;
      border-color: #475569;
      color: #e2e8f0;
    }

    .${b}.rte-citations-theme-dark .rte-citations-icon-btn:hover,
    .${b}.rte-citations-theme-dark .rte-citations-icon-btn:focus-visible {
      border-color: #60a5fa;
      box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.24);
    }

    .rte-citations-body {
      display: flex;
      flex-direction: column;
      gap: 10px;
      padding: 12px;
      overflow: auto;
    }

    .rte-citations-status {
      margin: 0;
      font-size: 12px;
      color: #475569;
    }

    .${b}.rte-citations-theme-dark .rte-citations-status {
      color: #94a3b8;
    }

    .rte-citations-grid {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 8px;
    }

    .rte-citations-label {
      display: flex;
      flex-direction: column;
      gap: 4px;
      font-size: 12px;
      font-weight: 600;
      color: inherit;
    }

    .rte-citations-label-note {
      grid-column: 1 / -1;
    }

    .rte-citations-field {
      width: 100%;
      box-sizing: border-box;
      min-height: 30px;
      border-radius: 8px;
      border: 1px solid #cbd5e1;
      background: #ffffff;
      color: inherit;
      font-size: 13px;
      padding: 6px 8px;
    }

    .rte-citations-field:focus-visible {
      outline: none;
      border-color: #1d4ed8;
      box-shadow: 0 0 0 2px rgba(29, 78, 216, 0.2);
    }

    .${b}.rte-citations-theme-dark .rte-citations-field {
      border-color: #334155;
      background: #0b1220;
      color: #e2e8f0;
    }

    .rte-citations-controls {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
    }

    .rte-citations-btn {
      border: 1px solid #cbd5e1;
      background: #f8fafc;
      color: inherit;
      border-radius: 8px;
      padding: 6px 10px;
      font-size: 12px;
      font-weight: 600;
      cursor: pointer;
    }

    .rte-citations-btn:hover,
    .rte-citations-btn:focus-visible {
      outline: none;
      border-color: #1d4ed8;
      box-shadow: 0 0 0 2px rgba(29, 78, 216, 0.2);
    }

    .rte-citations-btn-primary {
      background: #1d4ed8;
      border-color: #1d4ed8;
      color: #ffffff;
    }

    .rte-citations-btn-primary:hover,
    .rte-citations-btn-primary:focus-visible {
      background: #1e40af;
      border-color: #1e40af;
    }

    .${b}.rte-citations-theme-dark .rte-citations-btn {
      border-color: #334155;
      background: #0b1220;
      color: #e2e8f0;
    }

    .${b}.rte-citations-theme-dark .rte-citations-btn-primary {
      border-color: #2563eb;
      background: #2563eb;
      color: #ffffff;
    }

    .rte-citations-recent {
      border: 1px solid #e2e8f0;
      border-radius: 10px;
      padding: 8px;
    }

    .${b}.rte-citations-theme-dark .rte-citations-recent {
      border-color: #334155;
    }

    .rte-citations-recent-heading {
      margin: 0 0 8px;
      font-size: 12px;
      font-weight: 700;
    }

    .rte-citations-recent-list {
      margin: 0;
      padding: 0;
      list-style: none;
      display: flex;
      flex-direction: column;
      gap: 6px;
      max-height: 170px;
      overflow: auto;
    }

    .rte-citations-recent-btn {
      width: 100%;
      text-align: left;
      border: 1px solid #cbd5e1;
      background: #ffffff;
      color: inherit;
      border-radius: 8px;
      padding: 7px;
      cursor: pointer;
      display: flex;
      flex-direction: column;
      gap: 2px;
    }

    .rte-citations-recent-row {
      display: flex;
      gap: 6px;
      align-items: stretch;
    }

    .rte-citations-recent-delete {
      flex: 0 0 auto;
      border: 1px solid #fecaca;
      background: #fff1f2;
      color: #b91c1c;
      border-radius: 8px;
      padding: 0 8px;
      font-size: 11px;
      font-weight: 700;
      cursor: pointer;
      min-height: 34px;
    }

    .rte-citations-recent-delete:hover,
    .rte-citations-recent-delete:focus-visible {
      outline: none;
      border-color: #f87171;
      box-shadow: 0 0 0 2px rgba(248, 113, 113, 0.2);
    }

    .rte-citations-recent-btn:focus-visible,
    .rte-citations-recent-btn:hover {
      outline: none;
      border-color: #1d4ed8;
      box-shadow: 0 0 0 2px rgba(29, 78, 216, 0.18);
    }

    .${b}.rte-citations-theme-dark .rte-citations-recent-btn {
      border-color: #334155;
      background: #0b1220;
      color: #e2e8f0;
    }

    .${b}.rte-citations-theme-dark .rte-citations-recent-delete {
      border-color: #7f1d1d;
      background: #2b1218;
      color: #fca5a5;
    }

    .rte-citations-recent-title {
      font-size: 12px;
      font-weight: 700;
      line-height: 1.3;
    }

    .rte-citations-recent-meta {
      font-size: 11px;
      color: #64748b;
      line-height: 1.3;
    }

    .${b}.rte-citations-theme-dark .rte-citations-recent-meta {
      color: #94a3b8;
    }

    .rte-citations-empty {
      border: 1px dashed #cbd5e1;
      border-radius: 8px;
      padding: 8px;
      font-size: 12px;
      color: #64748b;
    }

    .${b}.rte-citations-theme-dark .rte-citations-empty {
      border-color: #334155;
      color: #94a3b8;
    }

    .rte-citations-shortcut {
      margin: 0;
      font-size: 11px;
      color: #64748b;
    }

    .${b}.rte-citations-theme-dark .rte-citations-shortcut {
      color: #94a3b8;
    }

    .rte-citations-live {
      position: absolute;
      width: 1px;
      height: 1px;
      margin: -1px;
      padding: 0;
      overflow: hidden;
      clip: rect(0 0 0 0);
      border: 0;
    }

    .rte-citation-ref {
      display: inline-flex;
      align-items: center;
      border-radius: 6px;
      border: 1px solid rgba(29, 78, 216, 0.24);
      background: rgba(29, 78, 216, 0.08);
      color: #1e3a8a;
      padding: 0 4px;
      margin: 0 1px;
      font-size: 0.92em;
      line-height: 1.35;
      white-space: nowrap;
      cursor: pointer;
      user-select: all;
    }

    .rte-citation-ref:focus,
    .rte-citation-ref:focus-visible {
      outline: none;
      border-color: #1d4ed8;
      box-shadow: 0 0 0 2px rgba(29, 78, 216, 0.22);
    }

    .${x} .rte-citation-ref {
      border-color: rgba(96, 165, 250, 0.45);
      background: rgba(37, 99, 235, 0.22);
      color: #bfdbfe;
    }

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

    .${x} .rte-citation-bibliography,
    .${x} .rte-citation-footnotes {
      border-top-color: #475569;
    }

    .rte-citation-section-title {
      margin: 0 0 8px;
      font-size: 1em;
      font-weight: 700;
    }

    .rte-citation-list {
      margin: 0;
      padding-left: 22px;
    }

    .rte-citation-item {
      margin: 0 0 8px;
      line-height: 1.45;
    }

    .rte-citation-backref {
      margin-left: 8px;
      color: #1d4ed8;
      text-decoration: none;
      font-size: 0.9em;
    }

    .rte-citation-backref:hover,
    .rte-citation-backref:focus-visible {
      text-decoration: underline;
      outline: none;
    }

    .${x} .rte-citation-backref {
      color: #93c5fd;
    }

    @media (max-width: 768px) {
      .${b} {
        left: 10px !important;
        right: 10px;
        top: 10px !important;
        width: auto !important;
        max-height: calc(100vh - 20px);
      }

      .rte-citations-grid {
        grid-template-columns: 1fr;
      }

      .rte-citations-recent-list {
        max-height: 34vh;
      }
    }
  `, document.head.appendChild(t);
}
const ue = (t = {}) => {
  const e = Z(t);
  return le(), {
    name: "citations",
    toolbar: [
      {
        id: "citationsGroup",
        label: "Citations",
        type: "group",
        command: "citations",
        items: [
          {
            id: "citations",
            label: "Citations",
            command: "toggleCitationsPanel",
            shortcut: "Mod-Alt-Shift-c",
            icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" focusable="false" aria-hidden="true"><path d="M6 5h12M6 9h12M6 13h8M6 17h10" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/><path d="M17 14.5a2.5 2.5 0 0 1 2.5 2.5v2H15v-2a2 2 0 0 1 2-2Z" stroke="currentColor" stroke-width="1.5"/></svg>'
          },
          {
            id: "citationsRefresh",
            label: "Refresh Citations",
            command: "refreshCitations",
            shortcut: "Mod-Alt-Shift-b",
            icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" focusable="false" aria-hidden="true"><path d="M20 12a8 8 0 1 1-2.34-5.66" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/><path d="M20 4v6h-6" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"/></svg>'
          },
          {
            id: "citationsStyle",
            label: "Cycle Citation Style",
            command: "cycleCitationStyle",
            shortcut: "Mod-Alt-Shift-j",
            icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" focusable="false" aria-hidden="true"><path d="M5 6h14M5 10h8M5 14h14M5 18h10" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/><circle cx="18" cy="10" r="2" stroke="currentColor" stroke-width="1.6"/></svg>'
          }
        ]
      }
    ],
    commands: {
      citations: (n, i) => {
        const o = h(i);
        if (!o || $(o)) return !1;
        const r = f.get(o) || e;
        return f.set(o, r), T.set(o, N(o, r)), p = o, z(o), y(o, r, !1), !0;
      },
      toggleCitationsPanel: (n, i) => {
        const o = h(i);
        if (!o || $(o)) return !1;
        const r = f.get(o) || e;
        f.set(o, r), p = o;
        const a = It(o, typeof n == "boolean" ? n : void 0);
        return nt(o) && y(o, r, !1), a;
      },
      insertCitation: (n, i) => {
        const o = h(i);
        if (!o || $(o) || !n || typeof n != "object") return !1;
        const r = f.get(o) || e;
        f.set(o, r), p = o;
        const a = et(o, n, r);
        return a && z(o), a;
      },
      refreshCitations: (n, i) => {
        const o = h(i);
        if (!o) return !1;
        const r = f.get(o) || e;
        return f.set(o, r), p = o, y(o, r, !0), z(o), !0;
      },
      setCitationStyle: (n, i) => {
        const o = h(i);
        if (!o || !n) return !1;
        const r = f.get(o) || e;
        return f.set(o, r), p = o, zt(o, n, r), !0;
      },
      cycleCitationStyle: (n, i) => {
        const o = h(i);
        if (!o) return !1;
        const r = f.get(o) || e;
        return f.set(o, r), p = o, bt(o, r), H(o), !0;
      },
      getCitationRecords: (n, i) => {
        const o = h(i);
        if (!o) return !1;
        const r = f.get(o) || e, a = j(o, r);
        if (typeof n == "function")
          try {
            n(a);
          } catch {
          }
        return o.__citationRecords = a, o.dispatchEvent(
          new CustomEvent("editora:citations-data", {
            bubbles: !0,
            detail: {
              records: a,
              style: N(o, r)
            }
          })
        ), !0;
      },
      setCitationsOptions: (n, i) => {
        const o = h(i);
        if (!o || !n || typeof n != "object") return !1;
        const r = f.get(o) || e, a = Z({
          ...r,
          ...n,
          labels: {
            ...r.labels,
            ...n.labels || {}
          },
          normalizeText: n.normalizeText || r.normalizeText,
          generateCitationId: n.generateCitationId || r.generateCitationId
        });
        return f.set(o, a), n.defaultStyle && L.includes(n.defaultStyle) && T.set(o, n.defaultStyle), y(o, a, !0), H(o), !0;
      },
      locateCitation: (n, i) => {
        const o = h(i);
        return !o || typeof n != "string" ? !1 : Qt(o, n);
      },
      deleteCitation: (n, i) => {
        const o = h(i);
        if (!o || $(o)) return !1;
        const r = f.get(o) || e;
        return f.set(o, r), typeof n == "string" && n.trim() ? Nt(o, n, r) : Zt(o, r);
      },
      insertRecentCitation: (n, i) => {
        const o = h(i);
        if (!o || $(o)) return !1;
        const r = f.get(o) || e;
        f.set(o, r);
        const a = j(o, r), s = rt(o, a, r);
        if (s.length === 0) return !1;
        const u = typeof n == "string" && n.trim() ? St(o, n, a, r) : s[0];
        if (!u) return !1;
        const c = et(o, u, r);
        return c && z(o), c;
      }
    },
    keymap: {
      "Mod-Alt-Shift-c": "toggleCitationsPanel",
      "Mod-Alt-Shift-C": "toggleCitationsPanel",
      "Mod-Alt-Shift-b": "refreshCitations",
      "Mod-Alt-Shift-B": "refreshCitations",
      "Mod-Alt-Shift-j": "cycleCitationStyle",
      "Mod-Alt-Shift-J": "cycleCitationStyle"
    },
    init: function(i) {
      Y += 1;
      const o = this && typeof this.__pluginConfig == "object" ? Z({ ...e, ...this.__pluginConfig }) : e;
      se(o);
      const r = h(
        i && i.editorElement ? { editorElement: i.editorElement } : void 0,
        !1
      );
      r && (p = r, f.set(r, o), T.set(r, o.defaultStyle), it(r, "toggleCitationsPanel", !1), Tt(r));
    },
    destroy: () => {
      Y = Math.max(0, Y - 1), !(Y > 0) && (q.forEach((n) => {
        window.clearTimeout(n);
      }), q.clear(), ce());
    }
  };
};
export {
  ue as CitationsPlugin
};
