import { __exports as m } from "./index-LexicalAutoLinkPlugin.dev2.js";
import { __require as H } from "./index-LexicalLink2.js";
import { __require as J } from "./index-LexicalComposerContext2.js";
import { __require as K } from "./index-LexicalUtils.js";
import { __require as Q } from "./index-Lexical.js";
import X from "react";
var C;
function it() {
  if (C) return m;
  C = 1;
  var x = H(), E = J(), P = K(), a = Q(), q = X;
  function M(t, e = (n) => n) {
    return (n) => {
      const i = t.exec(n);
      return i === null ? null : {
        index: i.index,
        length: i[0].length,
        text: i[0],
        url: e(i[0])
      };
    };
  }
  function b(t, e) {
    for (let n = 0; n < e.length; n++) {
      const i = e[n](t);
      if (i)
        return i;
    }
    return null;
  }
  const U = /[.,;\s]/;
  function L(t) {
    return U.test(t);
  }
  function $(t) {
    return L(t[t.length - 1]);
  }
  function A(t) {
    return L(t[0]);
  }
  function _(t) {
    let e = t.getPreviousSibling();
    return a.$isElementNode(e) && (e = e.getLastDescendant()), e === null || a.$isLineBreakNode(e) || a.$isTextNode(e) && $(e.getTextContent());
  }
  function v(t) {
    let e = t.getNextSibling();
    return a.$isElementNode(e) && (e = e.getFirstDescendant()), e === null || a.$isLineBreakNode(e) || a.$isTextNode(e) && A(e.getTextContent());
  }
  function V(t, e, n, i) {
    return (t > 0 ? L(n[t - 1]) : _(i[0])) ? e < n.length ? L(n[e]) : v(i[i.length - 1]) : !1;
  }
  function B(t, e, n) {
    const i = [], s = [], o = [];
    let r = 0, l = 0;
    const c = [...t];
    for (; c.length > 0; ) {
      const u = c[0], g = u.getTextContent().length, f = l;
      l + g <= e ? (i.push(u), r += g) : f >= n ? o.push(u) : s.push(u), l += g, c.shift();
    }
    return [r, i, s, o];
  }
  function D(t, e, n, i) {
    const s = x.$createAutoLinkNode(i.url, i.attributes);
    if (t.length === 1) {
      let o = t[0], r;
      e === 0 ? [r, o] = o.splitText(n) : [, r, o] = o.splitText(e, n);
      const l = a.$createTextNode(i.text);
      return l.setFormat(r.getFormat()), l.setDetail(r.getDetail()), s.append(l), r.replace(s), o;
    } else if (t.length > 1) {
      const o = t[0];
      let r = o.getTextContent().length, l;
      e === 0 ? l = o : [, l] = o.splitText(e);
      const c = [];
      let u;
      for (let h = 1; h < t.length; h++) {
        const N = t[h], T = N.getTextContent().length, k = r, y = r + T;
        if (k < n)
          if (y <= n)
            c.push(N);
          else {
            const [z, G] = N.splitText(n - k);
            c.push(z), u = G;
          }
        r += T;
      }
      const d = a.$getSelection(), g = d ? d.getNodes().find(a.$isTextNode) : void 0, f = a.$createTextNode(l.getTextContent());
      return f.setFormat(l.getFormat()), f.setDetail(l.getDetail()), s.append(f, ...c), g && g === l && (a.$isRangeSelection(d) ? f.select(d.anchor.offset, d.focus.offset) : a.$isNodeSelection(d) && f.select(0, f.getTextContent().length)), l.replace(s), u;
    }
  }
  function F(t, e, n) {
    let i = [...t], s = i.map((l) => l.getTextContent()).join(""), o, r = 0;
    for (; (o = b(s, e)) && o !== null; ) {
      const l = o.index, c = o.length, u = l + c;
      if (V(r + l, r + u, s, i)) {
        const [g, , f, h] = B(i, r + l, r + u), N = r + l - g, R = r + u - g, T = D(f, N, R, o);
        i = T ? [T, ...h] : h, n(o.url, null), r = 0;
      } else
        r += u;
      s = s.substring(u);
    }
  }
  function S(t, e, n) {
    const i = t.getChildren(), s = i.length;
    for (let c = 0; c < s; c++) {
      const u = i[c];
      if (!a.$isTextNode(u) || !u.isSimpleText()) {
        p(t), n(null, t.getURL());
        return;
      }
    }
    const o = t.getTextContent(), r = b(o, e);
    if (r === null || r.text !== o) {
      p(t), n(null, t.getURL());
      return;
    }
    if (!_(t) || !v(t)) {
      p(t), n(null, t.getURL());
      return;
    }
    const l = t.getURL();
    if (l !== r.url && (t.setURL(r.url), n(r.url, l)), r.attributes) {
      const c = t.getRel();
      c !== r.attributes.rel && (t.setRel(r.attributes.rel || null), n(r.attributes.rel || null, c));
      const u = t.getTarget();
      u !== r.attributes.target && (t.setTarget(r.attributes.target || null), n(r.attributes.target || null, u));
    }
  }
  function O(t, e, n) {
    const i = t.getPreviousSibling(), s = t.getNextSibling(), o = t.getTextContent();
    x.$isAutoLinkNode(i) && !A(o) && (i.append(t), S(i, e, n), n(null, i.getURL())), x.$isAutoLinkNode(s) && !$(o) && (p(s), S(s, e, n), n(null, s.getURL()));
  }
  function p(t) {
    const e = t.getChildren(), n = e.length;
    for (let i = n - 1; i >= 0; i--)
      t.insertAfter(e[i]);
    return t.remove(), e.map((i) => i.getLatest());
  }
  function W(t) {
    const e = [t];
    let n = t.getNextSibling();
    for (; n !== null && a.$isTextNode(n) && n.isSimpleText() && (e.push(n), !/[\s]/.test(n.getTextContent())); )
      n = n.getNextSibling();
    return e;
  }
  function w(t, e, n) {
    q.useEffect(() => {
      if (!t.hasNodes([x.AutoLinkNode]))
        throw Error("LexicalAutoLinkPlugin: AutoLinkNode not registered on editor");
      const i = (s, o) => {
        n && n(s, o);
      };
      return P.mergeRegister(t.registerNodeTransform(a.TextNode, (s) => {
        const o = s.getParentOrThrow(), r = s.getPreviousSibling();
        if (x.$isAutoLinkNode(o))
          S(o, e, i);
        else if (!x.$isLinkNode(o)) {
          if (s.isSimpleText() && (A(s.getTextContent()) || !x.$isAutoLinkNode(r))) {
            const l = W(s);
            F(l, e, i);
          }
          O(s, e, i);
        }
      }));
    }, [t, e, n]);
  }
  function j({
    matchers: t,
    onChange: e
  }) {
    const [n] = E.useLexicalComposerContext();
    return w(n, t, e), null;
  }
  return m.AutoLinkPlugin = j, m.createLinkMatcherWithRegExp = M, m;
}
export {
  it as __require
};
