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