import { f as A, g as S } from "./editorContainerHelpers-C7kdWnS0.mjs";
const b = /* @__PURE__ */ new Set();
function D() {
  if (typeof window == "undefined" || window.__anchorObserverInitialized) return;
  window.__anchorObserverInitialized = !0, new MutationObserver((l) => {
    l.forEach((s) => {
      s.removedNodes.forEach((i) => {
        var a, d;
        if (i.nodeType === Node.ELEMENT_NODE) {
          const o = i;
          if ((a = o.classList) != null && a.contains("rte-anchor")) {
            const u = o.id;
            u && b.delete(u);
          }
          const p = (d = o.querySelectorAll) == null ? void 0 : d.call(o, ".rte-anchor");
          p == null || p.forEach((u) => {
            const c = u.id;
            c && b.delete(c);
          });
        }
      });
    });
  }).observe(document.body, {
    childList: !0,
    subtree: !0
  });
}
function E(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 I() {
  const t = A();
  if (!t) return;
  const l = S(t);
  if (!l) return;
  const s = l.querySelectorAll(".rte-anchor"), i = /* @__PURE__ */ new Set();
  s.forEach((a) => {
    const d = a.id;
    d && i.add(d);
  }), b.clear(), i.forEach((a) => b.add(a));
}
function R(t, l, s, i) {
  I();
  const a = document.createElement("div");
  a.className = "rte-anchor-dialog-overlay", a.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: 10000;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
  `;
  const d = document.createElement("div");
  if (d.className = "rte-anchor-dialog", d.style.cssText = `
    background: white;
    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 r = document.createElement("style");
    r.id = "rte-anchor-dialog-styles", r.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(r);
  }
  let o = "";
  const p = document.createElement("div");
  p.style.cssText = `
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid #e0e0e0;
    background: #f9f9f9;
  `;
  const u = document.createElement("h3");
  u.style.cssText = "margin: 0; font-size: 18px; font-weight: 600; color: #333;", u.textContent = "Add Anchor";
  const c = document.createElement("button");
  c.textContent = "✕", c.style.cssText = `
    background: none;
    border: none;
    font-size: 24px;
    color: #999;
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.2s ease;
  `, c.onmouseover = () => {
    c.style.background = "#e0e0e0", c.style.color = "#333";
  }, c.onmouseout = () => {
    c.style.background = "none", c.style.color = "#999";
  }, p.appendChild(u), p.appendChild(c);
  const w = document.createElement("div");
  w.style.cssText = "padding: 20px;";
  const y = document.createElement("div");
  y.style.cssText = "margin-bottom: 0;";
  const m = document.createElement("label");
  m.textContent = "Anchor ID", m.style.cssText = "display: block; font-size: 14px; font-weight: 500; color: #333; margin-bottom: 8px;", m.setAttribute("for", "anchor-id-input");
  const e = document.createElement("input");
  e.id = "anchor-id-input", e.type = "text", e.placeholder = "e.g., section-introduction", e.value = "", e.style.cssText = `
    width: 100%;
    padding: 10px 12px;
    font-size: 14px;
    border: 1px solid #d0d0d0;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    transition: all 0.2s ease;
    box-sizing: border-box;
  `;
  const f = document.createElement("div");
  f.style.cssText = `
    color: #d32f2f;
    font-size: 12px;
    margin-top: 6px;
    display: none;
  `;
  const v = document.createElement("div");
  v.textContent = "URL-safe ID (letters, numbers, hyphens, underscores). Must start with letter or underscore.", v.style.cssText = "color: #999; font-size: 12px; margin-top: 8px; line-height: 1.4;", y.appendChild(m), y.appendChild(e), y.appendChild(f), y.appendChild(v), w.appendChild(y);
  const x = document.createElement("div");
  x.style.cssText = `
    display: flex;
    gap: 12px;
    padding: 16px 20px;
    border-top: 1px solid #e0e0e0;
    background: #f9f9f9;
    justify-content: flex-end;
  `;
  const h = document.createElement("button");
  h.textContent = "Cancel", h.style.cssText = `
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 500;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
    background: #f0f0f0;
    color: #333;
  `, h.onmouseover = () => h.style.background = "#e0e0e0", h.onmouseout = () => h.style.background = "#f0f0f0";
  const n = document.createElement("button");
  n.textContent = "Add Anchor", n.style.cssText = `
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 500;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
    background: #0066cc;
    color: white;
  `, n.disabled = !e.value.trim();
  const T = () => {
    e.value.trim() ? (n.disabled = !1, n.style.background = "#0066cc", n.style.color = "white", n.style.cursor = "pointer") : (n.disabled = !0, n.style.background = "#d0d0d0", n.style.color = "#999", n.style.cursor = "not-allowed");
  };
  n.onmouseover = () => {
    n.disabled || (n.style.background = "#0052a3", n.style.boxShadow = "0 2px 8px rgba(0, 102, 204, 0.3)");
  }, n.onmouseout = () => {
    n.disabled || (n.style.background = "#0066cc", n.style.boxShadow = "none");
  }, x.appendChild(h), x.appendChild(n), e.oninput = () => {
    const r = e.value;
    if (T(), r.trim()) {
      const C = E(r);
      C.valid ? b.has(r) ? (o = `Anchor ID already exists: ${r}`, f.textContent = "⚠ " + o, f.style.display = "block", e.style.borderColor = "#d32f2f", e.style.background = "#ffebee") : (o = "", f.style.display = "none", e.style.borderColor = "#d0d0d0", e.style.background = "white") : (o = C.error, f.textContent = "⚠ " + o, f.style.display = "block", e.style.borderColor = "#d32f2f", e.style.background = "#ffebee");
    } else
      f.style.display = "none", e.style.borderColor = "#d0d0d0", e.style.background = "white";
  }, e.onfocus = () => {
    e.style.borderColor = o ? "#d32f2f" : "#0066cc", e.style.boxShadow = o ? "0 0 0 3px rgba(211, 47, 47, 0.1)" : "0 0 0 3px rgba(0, 102, 204, 0.1)", e.style.background = o ? "#ffebee" : "#f9f9ff";
  }, e.onblur = () => {
    e.style.boxShadow = "none", o || (e.style.background = "white");
  };
  const k = () => {
    const r = e.value.trim();
    !r || !E(r).valid || b.has(r) || (s && s(r), a.remove());
  }, g = () => {
    a.remove();
  };
  n.onclick = k, h.onclick = g, c.onclick = g, e.onkeydown = (r) => {
    r.key === "Enter" ? (r.preventDefault(), k()) : r.key === "Escape" && (r.preventDefault(), g());
  }, a.onclick = (r) => {
    r.target === a && g();
  }, d.appendChild(p), d.appendChild(w), d.appendChild(x), a.appendChild(d), document.body.appendChild(a), setTimeout(() => e.focus(), 100);
}
function N(t, l) {
  let s;
  if (l)
    s = l;
  else {
    const o = window.getSelection();
    if (!o || o.rangeCount === 0) return;
    s = o.getRangeAt(0);
  }
  const i = document.createElement("span");
  i.id = t, i.className = "rte-anchor", i.setAttribute("data-type", "anchor"), i.setAttribute("data-anchor-id", t), i.setAttribute("title", `Anchor: ${t}`), i.style.cssText = `
    display: inline;
    position: relative;
    cursor: pointer;
  `, s.insertNode(i), b.add(t), s.setStart(i.nextSibling || i.parentNode, 0), s.collapse(!0);
  const a = window.getSelection();
  a && (a.removeAllRanges(), a.addRange(s));
  const d = A();
  if (d) {
    const o = S(d);
    o && o.dispatchEvent(new Event("input", { bubbles: !0 }));
  }
  z();
}
function z() {
  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 _ = () => (typeof window != "undefined" && (D(), z()), {
  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 l = t.getRangeAt(0).cloneRange();
        return R("add", "", (s) => {
          N(s, l);
        }, l), !0;
      } catch (t) {
        return console.error("Failed to insert anchor:", t), !1;
      }
    }
  },
  keymap: {
    "Mod-Shift-k": "insertAnchor"
  }
});
export {
  _ as AnchorPlugin
};
//# sourceMappingURL=AnchorPlugin.native-7es9PVZ9.mjs.map
