let a = null, u = !1, l = null;
const p = '[data-theme="dark"], .dark, .editora-theme-dark', w = (e) => {
  if (!e) return null;
  let t = e;
  for (; t; ) {
    if (t.hasAttribute("contenteditable") && t.getAttribute("contenteditable") === "true" || t.hasAttribute("data-editora-content"))
      return t;
    t = t.parentElement;
  }
  return null;
}, C = (e) => {
  if (e) {
    const n = e.startContainer;
    if ((n.nodeType === Node.ELEMENT_NODE ? n : n.parentElement)?.closest(p)) return !0;
  }
  return document.activeElement?.closest(p) ? !0 : document.body.matches(p) || document.documentElement.matches(p);
}, T = () => {
  if (document.getElementById("rte-link-dialog-theme-styles")) return;
  const e = document.createElement("style");
  e.id = "rte-link-dialog-theme-styles", e.textContent = `
    .link-dialog-overlay.rte-theme-dark .link-dialog {
      background: #1f2937 !important;
      border: 1px solid #4b5563 !important;
      color: #e2e8f0 !important;
      box-shadow: 0 18px 45px rgba(0, 0, 0, 0.6) !important;
    }

    .link-dialog-overlay.rte-theme-dark .link-dialog-header {
      border-bottom-color: #3b4657 !important;
      background: #222d3a !important;
    }

    .link-dialog-overlay.rte-theme-dark .link-dialog-header h3,
    .link-dialog-overlay.rte-theme-dark label {
      color: #e2e8f0 !important;
    }

    .link-dialog-overlay.rte-theme-dark .link-dialog-close {
      color: #94a3b8 !important;
    }

    .link-dialog-overlay.rte-theme-dark .link-dialog-close:hover {
      background: #334155 !important;
      color: #f8fafc !important;
      border-radius: 4px;
    }

    .link-dialog-overlay.rte-theme-dark .link-dialog-footer {
      border-top-color: #3b4657 !important;
      background: #222d3a !important;
    }

    .link-dialog-overlay.rte-theme-dark input[type='text'],
    .link-dialog-overlay.rte-theme-dark input[type='url'] {
      background: #111827 !important;
      border-color: #4b5563 !important;
      color: #e2e8f0 !important;
    }

    .link-dialog-overlay.rte-theme-dark input[type='text']::placeholder,
    .link-dialog-overlay.rte-theme-dark input[type='url']::placeholder {
      color: #94a3b8 !important;
    }

    .link-dialog-overlay.rte-theme-dark .btn-cancel {
      background: #334155 !important;
      border-color: #4b5563 !important;
      color: #e2e8f0 !important;
    }

    .link-dialog-overlay.rte-theme-dark .btn-cancel:hover {
      background: #475569 !important;
      border-color: #64748b !important;
    }

    .link-dialog-overlay.rte-theme-dark .btn-submit {
      background: #3b82f6 !important;
    }

    .link-dialog-overlay.rte-theme-dark .btn-submit:hover {
      background: #2563eb !important;
    }
  `, document.head.appendChild(e);
}, A = (e) => {
  if (!a) {
    console.warn("No selection range stored");
    return;
  }
  const t = a.startContainer, n = t.nodeType === Node.TEXT_NODE ? t.parentElement : t, o = w(n);
  if (o) {
    if (u && l) {
      l.href = e.url, l.textContent = e.text, l.target = e.target, e.target === "_blank" ? l.setAttribute("rel", "noopener noreferrer") : l.removeAttribute("rel"), e.title ? l.title = e.title : l.removeAttribute("title");
      const r = document.createRange();
      r.selectNodeContents(l);
      const i = window.getSelection();
      i && (i.removeAllRanges(), i.addRange(r));
    } else {
      const r = document.createElement("a");
      r.href = e.url, r.textContent = e.text, r.target = e.target, e.target === "_blank" && r.setAttribute("rel", "noopener noreferrer"), e.title && (r.title = e.title), a.deleteContents(), a.insertNode(r), a.setStartAfter(r), a.setEndAfter(r);
      const i = window.getSelection();
      i && (i.removeAllRanges(), i.addRange(a));
    }
    o.focus(), a = null, u = !1, l = null;
  }
}, k = (e, t) => {
  T();
  const n = document.createElement("div");
  n.className = "link-dialog-overlay", t && n.classList.add("rte-theme-dark"), n.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;
  `;
  const o = document.createElement("div");
  o.className = "link-dialog", o.style.cssText = `
    background: white;
    border-radius: 8px;
    width: 500px;
    max-width: 90%;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  `, o.innerHTML = `
    <div class="link-dialog-header" style="padding: 16px 20px; border-bottom: 1px solid #ddd; display: flex; justify-content: space-between; align-items: center;">
      <h3 style="margin: 0; font-size: 18px;">${e.isEditing ? "Edit Link" : "Insert Link"}</h3>
      <button class="link-dialog-close" style="background: none; border: none; font-size: 24px; cursor: pointer; padding: 0; width: 30px; height: 30px;">×</button>
    </div>
    <form id="link-form">
      <div class="link-dialog-body" style="padding: 20px;">
        <div class="form-group" style="margin-bottom: 16px;">
          <label for="link-text" style="display: block; margin-bottom: 6px; font-weight: 500;">Link Text:</label>
          <input
            id="link-text"
            type="text"
            value="${e.text || ""}"
            placeholder="Enter link text"
            style="width: 100%; padding: 10px 12px; border: 1px solid #ccc; border-radius: 6px; font-size: 14px; line-height: 1.45; box-sizing: border-box;"
          />
        </div>
        <div class="form-group" style="margin-bottom: 16px;">
          <label for="link-url" style="display: block; margin-bottom: 6px; font-weight: 500;">URL:</label>
          <input
            id="link-url"
            type="url"
            value="${e.url || ""}"
            placeholder="https://example.com"
            required
            style="width: 100%; padding: 10px 12px; border: 1px solid #ccc; border-radius: 6px; font-size: 14px; line-height: 1.45; box-sizing: border-box;"
          />
        </div>
        <div class="form-group" style="margin-bottom: 16px;">
          <label for="link-title" style="display: block; margin-bottom: 6px; font-weight: 500;">Title (optional):</label>
          <input
            id="link-title"
            type="text"
            value="${e.title || ""}"
            placeholder="Link tooltip text"
            style="width: 100%; padding: 10px 12px; border: 1px solid #ccc; border-radius: 6px; font-size: 14px; line-height: 1.45; box-sizing: border-box;"
          />
        </div>
        <div class="form-group" style="margin-bottom: 0;">
          <label style="display: flex; align-items: center; cursor: pointer;">
            <input
              id="link-target"
              type="checkbox"
              ${e.target === "_blank" ? "checked" : ""}
              style="margin-right: 8px;"
            />
            Open in new window/tab
          </label>
        </div>
      </div>
      <div class="link-dialog-footer" style="padding: 12px 20px; border-top: 1px solid #ddd; display: flex; justify-content: flex-end; gap: 10px;">
        <button type="button" class="btn-cancel" style="padding: 8px 16px; border: 1px solid #ccc; background: white; border-radius: 4px; cursor: pointer;">Cancel</button>
        <button type="submit" class="btn-submit" style="padding: 8px 16px; border: none; background: #007bff; color: white; border-radius: 4px; cursor: pointer;">
          ${e.isEditing ? "Update Link" : "Insert Link"}
        </button>
      </div>
    </form>
  `, n.appendChild(o), document.body.appendChild(n);
  const r = o.querySelector("#link-form"), i = o.querySelector("#link-text"), d = o.querySelector("#link-url"), y = o.querySelector("#link-title"), v = o.querySelector("#link-target"), E = o.querySelector(".link-dialog-close"), L = o.querySelector(".btn-cancel"), b = (s) => {
    s.key === "Escape" && (s.preventDefault(), s.stopPropagation(), c());
  }, c = () => {
    document.removeEventListener("keydown", b, !0), n.remove();
  };
  E.addEventListener("click", c), L.addEventListener("click", c), n.addEventListener("click", (s) => {
    s.target === n && c();
  }), document.addEventListener("keydown", b, !0), r.addEventListener("submit", (s) => {
    s.preventDefault();
    const g = d.value.trim();
    g && (A({
      text: i.value.trim() || g,
      url: g,
      target: v.checked ? "_blank" : "_self",
      title: y.value.trim() || void 0
    }), c());
  }), setTimeout(() => i.focus(), 100);
}, x = () => {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return !1;
  const t = e.getRangeAt(0).cloneRange();
  a = t;
  const n = C(t), o = e.toString() || "", r = t.startContainer, d = (r.nodeType === Node.TEXT_NODE ? r.parentElement : r)?.closest("a");
  return d ? (u = !0, l = d, k({
    text: d.textContent || "",
    url: d.href,
    target: d.target || "_self",
    title: d.title || "",
    isEditing: !0
  }, n)) : (u = !1, l = null, k({
    text: o,
    url: "",
    target: "_self",
    isEditing: !1
  }, n)), !0;
}, h = () => (document.execCommand("unlink", !1), !0), m = (e, t) => {
  typeof window < "u" && window.registerEditorCommand?.(e, t);
}, f = () => {
  m("openLinkDialog", x), m("removeLink", h), m("createLink", (e) => {
    e && document.execCommand("createLink", !1, e);
  });
};
typeof window < "u" && (document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", f) : f());
const S = () => ({
  name: "link",
  marks: {
    link: {
      attrs: {
        href: {},
        title: { default: null },
        target: { default: null }
      },
      parseDOM: [
        {
          tag: "a[href]",
          getAttrs: (e) => ({
            href: e.getAttribute("href"),
            title: e.getAttribute("title"),
            target: e.getAttribute("target")
          })
        }
      ],
      toDOM: (e) => [
        "a",
        {
          href: e.attrs.href,
          title: e.attrs.title,
          target: e.attrs.target,
          rel: e.attrs.target === "_blank" ? "noopener noreferrer" : null
        },
        0
      ]
    }
  },
  toolbar: [
    {
      label: "Link",
      command: "openLinkDialog",
      type: "button",
      icon: '<svg width="24" height="24" focusable="false"><path d="M6.2 12.3a1 1 0 0 1 1.4 1.4l-2 2a2 2 0 1 0 2.6 2.8l4.8-4.8a1 1 0 0 0 0-1.4 1 1 0 1 1 1.4-1.3 2.9 2.9 0 0 1 0 4L9.6 20a3.9 3.9 0 0 1-5.5-5.5l2-2Zm11.6-.6a1 1 0 0 1-1.4-1.4l2-2a2 2 0 1 0-2.6-2.8L11 10.3a1 1 0 0 0 0 1.4A1 1 0 1 1 9.6 13a2.9 2.9 0 0 1 0-4L14.4 4a3.9 3.9 0 0 1 5.5 5.5l-2 2Z" fill-rule="nonzero"></path></svg>',
      shortcut: "Mod-k"
    }
  ],
  commands: {
    openLinkDialog: x,
    removeLink: h
  },
  keymap: {
    "Mod-k": "openLinkDialog"
  }
});
export {
  S as LinkPlugin
};
