import { __exports as _ } from "./index-LexicalLink.prod2.js";
import { __require as A } from "./index-LexicalUtils.js";
import { __require as O } from "./index-Lexical.js";
var L;
function y() {
  if (L) return _;
  L = 1;
  var m = A(), g = O();
  let N = /* @__PURE__ */ new Set(["http:", "https:", "mailto:", "sms:", "tel:"]);
  class a extends g.ElementNode {
    static getType() {
      return "link";
    }
    static clone(t) {
      return new a(t.__url, { rel: t.__rel, target: t.__target, title: t.__title }, t.__key);
    }
    constructor(t, e = {}, u) {
      super(u);
      let { target: o = null, rel: n = null, title: i = null } = e;
      this.__url = t, this.__target = o, this.__rel = n, this.__title = i;
    }
    createDOM(t) {
      let e = document.createElement("a");
      return e.href = this.sanitizeUrl(this.__url), this.__target !== null && (e.target = this.__target), this.__rel !== null && (e.rel = this.__rel), this.__title !== null && (e.title = this.__title), m.addClassNamesToElement(
        e,
        t.theme.link
      ), e;
    }
    updateDOM(t, e) {
      let u = this.__url, o = this.__target, n = this.__rel, i = this.__title;
      return u !== t.__url && (e.href = u), o !== t.__target && (o ? e.target = o : e.removeAttribute("target")), n !== t.__rel && (n ? e.rel = n : e.removeAttribute("rel")), i !== t.__title && (i ? e.title = i : e.removeAttribute("title")), !1;
    }
    static importDOM() {
      return { a: () => ({ conversion: k, priority: 1 }) };
    }
    static importJSON(t) {
      let e = f(t.url, { rel: t.rel, target: t.target, title: t.title });
      return e.setFormat(t.format), e.setIndent(t.indent), e.setDirection(t.direction), e;
    }
    sanitizeUrl(t) {
      try {
        let e = new URL(t);
        if (!N.has(e.protocol)) return "about:blank";
      } catch {
      }
      return t;
    }
    exportJSON() {
      return { ...super.exportJSON(), rel: this.getRel(), target: this.getTarget(), title: this.getTitle(), type: "link", url: this.getURL(), version: 1 };
    }
    getURL() {
      return this.getLatest().__url;
    }
    setURL(t) {
      this.getWritable().__url = t;
    }
    getTarget() {
      return this.getLatest().__target;
    }
    setTarget(t) {
      this.getWritable().__target = t;
    }
    getRel() {
      return this.getLatest().__rel;
    }
    setRel(t) {
      this.getWritable().__rel = t;
    }
    getTitle() {
      return this.getLatest().__title;
    }
    setTitle(t) {
      this.getWritable().__title = t;
    }
    insertNewAfter(t, e = !0) {
      return t = f(this.__url, { rel: this.__rel, target: this.__target, title: this.__title }), this.insertAfter(t, e), t;
    }
    canInsertTextBefore() {
      return !1;
    }
    canInsertTextAfter() {
      return !1;
    }
    canBeEmpty() {
      return !1;
    }
    isInline() {
      return !0;
    }
    extractWithChild(t, e) {
      if (!g.$isRangeSelection(e)) return !1;
      t = e.anchor.getNode();
      let u = e.focus.getNode();
      return this.isParentOf(t) && this.isParentOf(u) && 0 < e.getTextContent().length;
    }
  }
  function k(r) {
    let t = null;
    if (m.isHTMLAnchorElement(r)) {
      let e = r.textContent;
      (e !== null && e !== "" || 0 < r.children.length) && (t = f(r.getAttribute("href") || "", { rel: r.getAttribute("rel"), target: r.getAttribute("target"), title: r.getAttribute("title") }));
    }
    return { node: t };
  }
  function f(r, t) {
    return g.$applyNodeReplacement(new a(r, t));
  }
  function h(r) {
    return r instanceof a;
  }
  class c extends a {
    static getType() {
      return "autolink";
    }
    static clone(t) {
      return new c(t.__url, { rel: t.__rel, target: t.__target, title: t.__title }, t.__key);
    }
    static importJSON(t) {
      let e = d(t.url, { rel: t.rel, target: t.target, title: t.title });
      return e.setFormat(t.format), e.setIndent(t.indent), e.setDirection(t.direction), e;
    }
    static importDOM() {
      return null;
    }
    exportJSON() {
      return { ...super.exportJSON(), type: "autolink", version: 1 };
    }
    insertNewAfter(t, e = !0) {
      return t = this.getParentOrThrow().insertNewAfter(t, e), g.$isElementNode(t) ? (e = d(this.__url, { rel: this.__rel, target: this.__target, title: this.__title }), t.append(e), e) : null;
    }
  }
  function d(r, t) {
    return g.$applyNodeReplacement(new c(r, t));
  }
  let T = g.createCommand("TOGGLE_LINK_COMMAND");
  function x(r, t) {
    for (; r !== null && r.getParent() !== null && !t(r); ) r = r.getParentOrThrow();
    return t(r) ? r : null;
  }
  return _.$createAutoLinkNode = d, _.$createLinkNode = f, _.$isAutoLinkNode = function(r) {
    return r instanceof c;
  }, _.$isLinkNode = h, _.AutoLinkNode = c, _.LinkNode = a, _.TOGGLE_LINK_COMMAND = T, _.toggleLink = function(r, t = {}) {
    let { target: e, title: u } = t, o = t.rel === void 0 ? "noreferrer" : t.rel;
    if (t = g.$getSelection(), g.$isRangeSelection(t)) if (t = t.extract(), r === null) t.forEach((n) => {
      if (n = n.getParent(), h(n)) {
        let i = n.getChildren();
        for (let l = 0; l < i.length; l++) n.insertBefore(i[l]);
        n.remove();
      }
    });
    else {
      if (t.length === 1) {
        let l = x(t[0], h);
        if (l !== null) {
          l.setURL(r), e !== void 0 && l.setTarget(e), o !== null && l.setRel(o), u !== void 0 && l.setTitle(u);
          return;
        }
      }
      let n = null, i = null;
      t.forEach((l) => {
        var s = l.getParent();
        if (s !== i && s !== null && (!g.$isElementNode(l) || l.isInline())) if (h(s)) i = s, s.setURL(r), e !== void 0 && s.setTarget(e), o !== null && i.setRel(o), u !== void 0 && i.setTitle(u);
        else if (s.is(n) || (n = s, i = f(r, { rel: o, target: e, title: u }), h(s) ? l.getPreviousSibling() === null ? s.insertBefore(i) : s.insertAfter(i) : l.insertBefore(i)), h(l)) {
          if (!l.is(i)) {
            if (i !== null) {
              s = l.getChildren();
              for (let p = 0; p < s.length; p++) i.append(s[p]);
            }
            l.remove();
          }
        } else i !== null && i.append(l);
      });
    }
  }, _;
}
export {
  y as __require
};
