import { f as D, g as S } from "./editorContainerHelpers-C7kdWnS0.mjs";
const b = /* @__PURE__ */ new Set(), x = '[data-theme="dark"], .dark, .editora-theme-dark';
function z(t, c) {
  if (c === t.innerHTML) return;
  const i = window.execEditorCommand || window.executeEditorCommand;
  if (typeof i == "function")
    try {
      i("recordDomTransaction", t, c, t.innerHTML);
    } catch {
    }
}
function R() {
  if (typeof window > "u" || window.__anchorObserverInitialized) return;
  window.__anchorObserverInitialized = !0, new MutationObserver((c) => {
    c.forEach((i) => {
      i.removedNodes.forEach((n) => {
        if (n.nodeType === Node.ELEMENT_NODE) {
          const d = n;
          if (d.classList?.contains("rte-anchor")) {
            const r = d.id;
            r && b.delete(r);
          }
          d.querySelectorAll?.(".rte-anchor")?.forEach((r) => {
            const u = r.id;
            u && b.delete(u);
          });
        }
      });
    });
  }).observe(document.body, {
    childList: !0,
    subtree: !0
  });
}
function A(t) {
  return !t || t.trim().length === 0 ? { valid: !1, error: "Anchor ID cannot be empty" } : t.length > 256 ? { valid: !1, error: "Anchor ID must be less than 256 characters" } : /^[a-z_]/.test(t) ? /^[a-z0-9\-_]+$/.test(t) ? { valid: !0, error: "" } : { valid: !1, error: "Anchor ID can only contain letters, numbers, hyphens, and underscores" } : { valid: !1, error: "Anchor ID must start with a letter or underscore" };
}
function M() {
  const t = D();
  if (!t) return;
  const c = S(t);
  if (!c) return;
  const i = c.querySelectorAll(".rte-anchor"), n = /* @__PURE__ */ new Set();
  i.forEach((d) => {
    const e = d.id;
    e && n.add(e);
  }), b.clear(), n.forEach((d) => b.add(d));
}
function H(t) {
  if (t) {
    const n = t.startContainer;
    if ((n.nodeType === Node.ELEMENT_NODE ? n : n.parentElement)?.closest(x)) return !0;
  }
  const c = window.getSelection();
  if (c && c.rangeCount > 0) {
    const n = c.getRangeAt(0).startContainer;
    if ((n.nodeType === Node.ELEMENT_NODE ? n : n.parentElement)?.closest(x)) return !0;
  }
  return document.activeElement?.closest(x) ? !0 : document.body.matches(x) || document.documentElement.matches(x);
}
function _(t, c, i, n) {
  M();
  const d = H(n), e = d ? {
    overlay: "rgba(0, 0, 0, 0.62)",
    dialogBg: "#1f2937",
    panelBg: "#222d3a",
    border: "#3b4657",
    text: "#e2e8f0",
    muted: "#94a3b8",
    closeHoverBg: "#334155",
    fieldBg: "#111827",
    fieldFocusBg: "#111827",
    fieldBorder: "#4b5563",
    fieldText: "#e2e8f0",
    fieldErrorBg: "#3f2124",
    fieldErrorBorder: "#ef4444",
    cancelBg: "#334155",
    cancelHover: "#475569",
    cancelText: "#e2e8f0",
    saveBg: "#3b82f6",
    saveHover: "#2563eb",
    saveDisabledBg: "#374151",
    saveDisabledText: "#7f8ca1",
    help: "#9fb0c6",
    focusRing: "rgba(88, 166, 255, 0.25)",
    errorRing: "rgba(239, 68, 68, 0.25)"
  } : {
    overlay: "rgba(0, 0, 0, 0.5)",
    dialogBg: "#ffffff",
    panelBg: "#f9f9f9",
    border: "#e0e0e0",
    text: "#333333",
    muted: "#999999",
    closeHoverBg: "#e0e0e0",
    fieldBg: "#ffffff",
    fieldFocusBg: "#f9f9ff",
    fieldBorder: "#d0d0d0",
    fieldText: "#333333",
    fieldErrorBg: "#ffebee",
    fieldErrorBorder: "#d32f2f",
    cancelBg: "#f0f0f0",
    cancelHover: "#e0e0e0",
    cancelText: "#333333",
    saveBg: "#0066cc",
    saveHover: "#0052a3",
    saveDisabledBg: "#d0d0d0",
    saveDisabledText: "#999999",
    help: "#999999",
    focusRing: "rgba(0, 102, 204, 0.1)",
    errorRing: "rgba(211, 47, 47, 0.1)"
  }, r = document.createElement("div");
  r.className = "rte-anchor-dialog-overlay", r.style.cssText = `
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: ${e.overlay};
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
  `;
  const u = document.createElement("div");
  if (u.className = "rte-anchor-dialog", u.style.cssText = `
    background: ${e.dialogBg};
    border: 1px solid ${e.border};
    border-radius: 8px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    width: 90%;
    max-width: 450px;
    overflow: hidden;
    animation: rte-anchor-dialog-appear 0.2s ease;
  `, !document.getElementById("rte-anchor-dialog-styles")) {
    const s = document.createElement("style");
    s.id = "rte-anchor-dialog-styles", s.textContent = `
      @keyframes rte-anchor-dialog-appear {
        from { opacity: 0; transform: scale(0.95); }
        to { opacity: 1; transform: scale(1); }
      }
      .rte-anchor-dialog input:focus {
        outline: none !important;
      }
    `, document.head.appendChild(s);
  }
  let l = "";
  const h = document.createElement("div");
  h.style.cssText = `
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid ${e.border};
    background: ${e.panelBg};
  `;
  const w = document.createElement("h3");
  w.style.cssText = `margin: 0; font-size: 18px; font-weight: 600; color: ${e.text};`, w.textContent = "Add Anchor";
  const f = document.createElement("button");
  f.textContent = "✕", f.style.cssText = `
    background: none;
    border: none;
    font-size: 24px;
    color: ${e.muted};
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.2s ease;
  `, f.onmouseover = () => {
    f.style.background = e.closeHoverBg, f.style.color = "#f8fafc";
  }, f.onmouseout = () => {
    f.style.background = "none", f.style.color = e.muted;
  }, h.appendChild(w), h.appendChild(f);
  const C = document.createElement("div");
  C.style.cssText = "padding: 20px;";
  const m = document.createElement("div");
  m.style.cssText = "margin-bottom: 0;";
  const y = document.createElement("label");
  y.textContent = "Anchor ID", y.style.cssText = `display: block; font-size: 14px; font-weight: 500; color: ${e.text}; margin-bottom: 8px;`, y.setAttribute("for", "anchor-id-input");
  const o = document.createElement("input");
  o.id = "anchor-id-input", o.type = "text", o.placeholder = "e.g., section-introduction", o.value = "", o.style.cssText = `
    width: 100%;
    padding: 10px 12px;
    font-size: 14px;
    border: 1px solid ${e.fieldBorder};
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    color: ${e.fieldText};
    background: ${e.fieldBg};
    transition: all 0.2s ease;
    box-sizing: border-box;
  `, o.style.setProperty("caret-color", e.fieldText);
  const p = document.createElement("div");
  p.style.cssText = `
    color: #d32f2f;
    font-size: 12px;
    margin-top: 6px;
    display: none;
  `;
  const B = document.createElement("div");
  B.textContent = "URL-safe ID (letters, numbers, hyphens, underscores). Must start with letter or underscore.", B.style.cssText = `color: ${e.help}; font-size: 12px; margin-top: 8px; line-height: 1.4;`, m.appendChild(y), m.appendChild(o), m.appendChild(p), m.appendChild(B), C.appendChild(m);
  const v = document.createElement("div");
  v.style.cssText = `
    display: flex;
    gap: 12px;
    padding: 16px 20px;
    border-top: 1px solid ${e.border};
    background: ${e.panelBg};
    justify-content: flex-end;
  `;
  const g = document.createElement("button");
  g.textContent = "Cancel", g.style.cssText = `
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 500;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
    background: ${e.cancelBg};
    color: ${e.cancelText};
  `, g.onmouseover = () => g.style.background = e.cancelHover, g.onmouseout = () => g.style.background = e.cancelBg;
  const a = document.createElement("button");
  a.textContent = "Add Anchor", a.style.cssText = `
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 500;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
    background: ${e.saveBg};
    color: white;
  `, a.disabled = !o.value.trim();
  const $ = () => {
    o.value.trim() ? (a.disabled = !1, a.style.background = e.saveBg, a.style.color = "white", a.style.cursor = "pointer") : (a.disabled = !0, a.style.background = e.saveDisabledBg, a.style.color = e.saveDisabledText, a.style.cursor = "not-allowed");
  };
  a.onmouseover = () => {
    a.disabled || (a.style.background = e.saveHover, a.style.boxShadow = d ? "0 2px 8px rgba(59, 130, 246, 0.35)" : "0 2px 8px rgba(0, 102, 204, 0.3)");
  }, a.onmouseout = () => {
    a.disabled || (a.style.background = e.saveBg, a.style.boxShadow = "none");
  }, v.appendChild(g), v.appendChild(a), o.oninput = () => {
    const s = o.value;
    if ($(), s.trim()) {
      const k = A(s);
      k.valid ? b.has(s) ? (l = `Anchor ID already exists: ${s}`, p.textContent = "⚠ " + l, p.style.display = "block", o.style.borderColor = e.fieldErrorBorder, o.style.background = e.fieldErrorBg) : (l = "", p.style.display = "none", o.style.borderColor = e.fieldBorder, o.style.background = e.fieldBg) : (l = k.error, p.textContent = "⚠ " + l, p.style.display = "block", o.style.borderColor = e.fieldErrorBorder, o.style.background = e.fieldErrorBg);
    } else
      p.style.display = "none", o.style.borderColor = e.fieldBorder, o.style.background = e.fieldBg;
  }, o.onfocus = () => {
    o.style.borderColor = l ? e.fieldErrorBorder : e.saveBg, o.style.boxShadow = l ? `0 0 0 3px ${e.errorRing}` : `0 0 0 3px ${e.focusRing}`, o.style.background = l ? e.fieldErrorBg : e.fieldFocusBg;
  }, o.onblur = () => {
    o.style.boxShadow = "none", l || (o.style.background = e.fieldBg);
  };
  const T = () => {
    const s = o.value.trim();
    !s || !A(s).valid || b.has(s) || (i && i(s), r.remove());
  }, E = () => {
    r.remove();
  };
  a.onclick = T, g.onclick = E, f.onclick = E, o.onkeydown = (s) => {
    s.key === "Enter" ? (s.preventDefault(), T()) : s.key === "Escape" && (s.preventDefault(), E());
  }, r.onclick = (s) => {
    s.target === r && E();
  }, u.appendChild(h), u.appendChild(C), u.appendChild(v), r.appendChild(u), document.body.appendChild(r), setTimeout(() => o.focus(), 100);
}
function I(t, c) {
  let i;
  if (c)
    i = c;
  else {
    const l = window.getSelection();
    if (!l || l.rangeCount === 0) return;
    i = l.getRangeAt(0);
  }
  let n = null, d = i.startContainer;
  for (; d && d !== document.body; ) {
    if (d.nodeType === Node.ELEMENT_NODE) {
      const l = d;
      if (l.getAttribute("contenteditable") === "true") {
        n = l;
        break;
      }
    }
    d = d.parentNode;
  }
  const e = n?.innerHTML ?? "", r = document.createElement("span");
  r.id = t, r.className = "rte-anchor", r.setAttribute("data-type", "anchor"), r.setAttribute("data-anchor-id", t), r.setAttribute("title", `Anchor: ${t}`), r.style.cssText = `
    display: inline;
    position: relative;
    cursor: pointer;
  `, i.insertNode(r), b.add(t), i.setStart(r.nextSibling || r.parentNode, 0), i.collapse(!0);
  const u = window.getSelection();
  if (u && (u.removeAllRanges(), u.addRange(i)), n)
    z(n, e), n.dispatchEvent(new Event("input", { bubbles: !0 }));
  else {
    const l = D();
    if (l) {
      const h = S(l);
      h && h.dispatchEvent(new Event("input", { bubbles: !0 }));
    }
  }
  N();
}
function N() {
  if (document.getElementById("rte-anchor-styles")) return;
  const t = document.createElement("style");
  t.id = "rte-anchor-styles", t.textContent = `
    .rte-anchor {
      display: inline;
      position: relative;
      cursor: pointer;
      transition: all 0.2s ease;
    }
    
    .rte-anchor:hover::before {
      content: '⚓';
      position: absolute;
      top: -1.2em;
      left: 0;
      background: #333;
      color: #fff;
      padding: 2px 6px;
      border-radius: 3px;
      font-size: 0.8em;
      white-space: nowrap;
      z-index: 100;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    }
    
    .rte-anchor:hover::after {
      content: attr(data-anchor-id);
      position: absolute;
      top: -1.2em;
      left: 1.4em;
      background: #333;
      color: #fff;
      padding: 2px 6px;
      border-radius: 3px;
      font-size: 0.75em;
      font-family: 'Courier New', monospace;
      white-space: nowrap;
      z-index: 100;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    }
    
    [contenteditable='true'] .rte-anchor::before {
      content: '';
      position: absolute;
      width: 8px;
      height: 8px;
      background: #0066cc;
      border-radius: 50%;
      top: -3px;
      left: 0;
      opacity: 0.5;
      transition: opacity 0.2s ease;
    }
    
    [contenteditable='true'] .rte-anchor:hover::before {
      opacity: 1;
      width: auto;
      height: auto;
      background: #333;
      border-radius: 3px;
      top: -1.2em;
      padding: 2px 6px;
      font-size: 0.8em;
      content: '⚓';
    }
    
    @media print {
      .rte-anchor::before,
      .rte-anchor::after {
        display: none;
      }
      .rte-anchor {
        cursor: auto;
      }
    }
    
    .rte-anchor:focus {
      outline: 2px solid #0066cc;
      outline-offset: 2px;
    }
  `, document.head.appendChild(t);
}
const O = () => (typeof window < "u" && (R(), N()), {
  name: "anchor",
  toolbar: [
    {
      label: "Anchor",
      command: "insertAnchor",
      icon: '<svg width="24px" height="24px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M12 8.4C13.4912 8.4 14.7 7.19117 14.7 5.7C14.7 4.20883 13.4912 3 12 3C10.5088 3 9.3 4.20883 9.3 5.7C9.3 7.19117 10.5088 8.4 12 8.4ZM12 8.4V20.9999M12 20.9999C9.61305 20.9999 7.32387 20.0518 5.63604 18.364C3.94821 16.6761 3 14.3869 3 12H5M12 20.9999C14.3869 20.9999 16.6761 20.0518 18.364 18.364C20.0518 16.6761 21 14.3869 21 12H19" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>',
      shortcut: "Mod-Shift-k"
    }
  ],
  commands: {
    insertAnchor: () => {
      try {
        const t = window.getSelection();
        if (!t || t.rangeCount === 0)
          return alert("Please place your cursor where you want to insert the anchor."), !1;
        const c = t.getRangeAt(0).cloneRange();
        return _("add", "", (i) => {
          I(i, c);
        }, c), !0;
      } catch (t) {
        return console.error("Failed to insert anchor:", t), !1;
      }
    }
  },
  keymap: {
    "Mod-Shift-k": "insertAnchor"
  }
});
export {
  O as AnchorPlugin
};
