"use client";
import { addClassNamesToElement as g } from "../utils/LexicalUtils.prod.js";
import { ElementNode as p, isHTMLAnchorElement as h, $isRangeSelection as f, $applyNodeReplacement as d, $isElementNode as m } from "../../lexical/Lexical.prod.js";
const k = /* @__PURE__ */ new Set(["http:", "https:", "mailto:", "sms:", "tel:"]);
class u extends p {
  static getType() {
    return "link";
  }
  static clone(t) {
    return new u(t.__url, { rel: t.__rel, target: t.__target, title: t.__title }, t.__key);
  }
  constructor(t = "", e = {}, n) {
    super(n);
    const { target: i = null, rel: s = null, title: l = null } = e;
    this.__url = t, this.__target = i, this.__rel = s, this.__title = l;
  }
  createDOM(t) {
    const e = document.createElement("a");
    return this.updateLinkDOM(null, e, t), g(e, t.theme.link), e;
  }
  updateLinkDOM(t, e, n) {
    if (h(e)) {
      t && t.__url === this.__url || (e.href = this.sanitizeUrl(this.__url));
      for (const i of ["target", "rel", "title"]) {
        const s = `__${i}`, l = this[s];
        t && t[s] === l || (l ? e[i] = l : e.removeAttribute(i));
      }
    }
  }
  updateDOM(t, e, n) {
    return this.updateLinkDOM(t, e, n), !1;
  }
  static importDOM() {
    return { a: (t) => ({ conversion: U, priority: 1 }) };
  }
  static importJSON(t) {
    return o().updateFromJSON(t);
  }
  updateFromJSON(t) {
    return super.updateFromJSON(t).setURL(t.url).setRel(t.rel || null).setTarget(t.target || null).setTitle(t.title || null);
  }
  sanitizeUrl(t) {
    t = c(t);
    try {
      const e = new URL(c(t));
      if (!k.has(e.protocol)) return "about:blank";
    } catch {
      return t;
    }
    return t;
  }
  exportJSON() {
    return { ...super.exportJSON(), rel: this.getRel(), target: this.getTarget(), title: this.getTitle(), url: this.getURL() };
  }
  getURL() {
    return this.getLatest().__url;
  }
  setURL(t) {
    const e = this.getWritable();
    return e.__url = t, e;
  }
  getTarget() {
    return this.getLatest().__target;
  }
  setTarget(t) {
    const e = this.getWritable();
    return e.__target = t, e;
  }
  getRel() {
    return this.getLatest().__rel;
  }
  setRel(t) {
    const e = this.getWritable();
    return e.__rel = t, e;
  }
  getTitle() {
    return this.getLatest().__title;
  }
  setTitle(t) {
    const e = this.getWritable();
    return e.__title = t, e;
  }
  insertNewAfter(t, e = !0) {
    const n = o(this.__url, { rel: this.__rel, target: this.__target, title: this.__title });
    return this.insertAfter(n, e), n;
  }
  canInsertTextBefore() {
    return !1;
  }
  canInsertTextAfter() {
    return !1;
  }
  canBeEmpty() {
    return !1;
  }
  isInline() {
    return !0;
  }
  extractWithChild(t, e, n) {
    if (!f(e)) return !1;
    const i = e.anchor.getNode(), s = e.focus.getNode();
    return this.isParentOf(i) && this.isParentOf(s) && e.getTextContent().length > 0;
  }
  isEmailURI() {
    return this.__url.startsWith("mailto:");
  }
  isWebSiteURI() {
    return this.__url.startsWith("https://") || this.__url.startsWith("http://");
  }
}
function U(r) {
  let t = null;
  if (h(r)) {
    const e = r.textContent;
    (e !== null && e !== "" || r.children.length > 0) && (t = o(r.getAttribute("href") || "", { rel: r.getAttribute("rel"), target: r.getAttribute("target"), title: r.getAttribute("title") }));
  }
  return { node: t };
}
function o(r = "", t) {
  return d(new u(r, t));
}
function S(r) {
  return r instanceof u;
}
class a extends u {
  constructor(t = "", e = {}, n) {
    super(t, e, n), this.__isUnlinked = e.isUnlinked !== void 0 && e.isUnlinked !== null && e.isUnlinked;
  }
  static getType() {
    return "autolink";
  }
  static clone(t) {
    return new a(t.__url, { isUnlinked: t.__isUnlinked, rel: t.__rel, target: t.__target, title: t.__title }, t.__key);
  }
  getIsUnlinked() {
    return this.__isUnlinked;
  }
  setIsUnlinked(t) {
    const e = this.getWritable();
    return e.__isUnlinked = t, e;
  }
  createDOM(t) {
    return this.__isUnlinked ? document.createElement("span") : super.createDOM(t);
  }
  updateDOM(t, e, n) {
    return super.updateDOM(t, e, n) || t.__isUnlinked !== this.__isUnlinked;
  }
  static importJSON(t) {
    return _().updateFromJSON(t);
  }
  updateFromJSON(t) {
    return super.updateFromJSON(t).setIsUnlinked(t.isUnlinked || !1);
  }
  static importDOM() {
    return null;
  }
  exportJSON() {
    return { ...super.exportJSON(), isUnlinked: this.__isUnlinked };
  }
  insertNewAfter(t, e = !0) {
    const n = this.getParentOrThrow().insertNewAfter(t, e);
    if (m(n)) {
      const i = _(this.__url, { isUnlinked: this.__isUnlinked, rel: this.__rel, target: this.__target, title: this.__title });
      return n.append(i), i;
    }
    return null;
  }
}
function _(r = "", t) {
  return d(new a(r, t));
}
function T(r) {
  return r instanceof a;
}
const N = /^\+?[0-9\s()-]{5,}$/;
function c(r) {
  return r.match(/^[a-z][a-z0-9+.-]*:/i) || r.match(/^[/#.]/) ? r : r.includes("@") ? `mailto:${r}` : N.test(r) ? `tel:${r}` : `https://${r}`;
}
export {
  _ as $createAutoLinkNode,
  o as $createLinkNode,
  T as $isAutoLinkNode,
  S as $isLinkNode,
  a as AutoLinkNode,
  u as LinkNode,
  c as formatUrl
};
