import { __exports as L } from "./index-LexicalAutoLinkPlugin.prod2.js";
import { __require as E } from "./index-LexicalLink2.js";
import { __require as F } from "./index-LexicalComposerContext2.js";
import { __require as w } from "./index-LexicalUtils.js";
import { __require as B } from "./index-Lexical.js";
import M from "react";
var c;
function O() {
  if (c) return L;
  c = 1;
  var f = E(), $ = F(), v = w(), s = B(), C = M;
  function k(e) {
    let n = new URLSearchParams();
    n.append("code", e);
    for (let t = 1; t < arguments.length; t++) n.append("v", arguments[t]);
    throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${n} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`);
  }
  function h(e, n) {
    for (let t = 0; t < n.length; t++) {
      let r = n[t](e);
      if (r) return r;
    }
    return null;
  }
  let d = /[.,;\s]/;
  function T(e) {
    e = e.getPreviousSibling(), s.$isElementNode(e) && (e = e.getLastDescendant());
    var n;
    return !(n = e === null || s.$isLineBreakNode(e)) && (n = s.$isTextNode(e)) && (e = e.getTextContent(), n = d.test(e[e.length - 1])), n;
  }
  function N(e) {
    return e = e.getNextSibling(), s.$isElementNode(e) && (e = e.getFirstDescendant()), e === null || s.$isLineBreakNode(e) || s.$isTextNode(e) && d.test(e.getTextContent()[0]);
  }
  function _(e, n, t, r) {
    return (0 < e ? d.test(t[e - 1]) : T(r[0])) ? n < t.length ? d.test(t[n]) : N(r[r.length - 1]) : !1;
  }
  function R(e, n, t) {
    let r = [], l = [], i = [], o = 0, u = 0;
    for (e = [...e]; 0 < e.length; ) {
      let g = e[0], x = g.getTextContent().length, p = u;
      u + x <= n ? (r.push(g), o += x) : p >= t ? i.push(g) : l.push(g), u += x, e.shift();
    }
    return [o, r, l, i];
  }
  function q(e, n, t, r) {
    let l = f.$createAutoLinkNode(r.url, r.attributes);
    if (e.length === 1) {
      var i = e[0];
      n === 0 ? [u, i] = i.splitText(t) : [, u, i] = i.splitText(n, t);
      var o = s.$createTextNode(r.text);
      return o.setFormat(u.getFormat()), o.setDetail(u.getDetail()), l.append(o), u.replace(l), i;
    }
    if (1 < e.length) {
      r = e[0];
      var u = r.getTextContent().length;
      for (n === 0 ? i = r : [, i] = r.splitText(n), n = [], r = 1; r < e.length; r++) {
        let g = e[r], x = g.getTextContent().length, p = u, P = u + x;
        if (p < t) if (P <= t) n.push(g);
        else {
          let [U, D] = g.splitText(t - p);
          n.push(U), o = D;
        }
        u += x;
      }
      return e = (t = s.$getSelection()) ? t.getNodes().find(s.$isTextNode) : void 0, u = s.$createTextNode(i.getTextContent()), u.setFormat(i.getFormat()), u.setDetail(i.getDetail()), l.append(u, ...n), e && e === i && (s.$isRangeSelection(t) ? u.select(t.anchor.offset, t.focus.offset) : s.$isNodeSelection(t) && u.select(0, u.getTextContent().length)), i.replace(l), o;
    }
  }
  function S(e, n, t) {
    var r = [...e];
    e = r.map((o) => o.getTextContent()).join("");
    let l, i = 0;
    for (; (l = h(e, n)) && l !== null; ) {
      let o = l.index, u = o + l.length;
      if (_(i + o, i + u, e, r)) {
        let [g, , x, p] = R(r, i + o, i + u);
        r = (r = q(x, i + o - g, i + u - g, l)) ? [r, ...p] : p, t(l.url, null), i = 0;
      } else i += u;
      e = e.substring(u);
    }
  }
  function a(e, n, t) {
    var r = e.getChildren();
    let l = r.length;
    for (let i = 0; i < l; i++) {
      let o = r[i];
      if (!s.$isTextNode(o) || !o.isSimpleText()) {
        m(e), t(null, e.getURL());
        return;
      }
    }
    r = e.getTextContent(), n = h(r, n), n === null || n.text !== r ? (m(e), t(null, e.getURL())) : T(e) && N(e) ? (r = e.getURL(), r !== n.url && (e.setURL(n.url), t(n.url, r)), n.attributes && (r = e.getRel(), r !== n.attributes.rel && (e.setRel(n.attributes.rel || null), t(n.attributes.rel || null, r)), r = e.getTarget(), r !== n.attributes.target && (e.setTarget(n.attributes.target || null), t(n.attributes.target || null, r)))) : (m(e), t(null, e.getURL()));
  }
  function m(e) {
    let n = e.getChildren();
    var t = n.length;
    for (--t; 0 <= t; t--) e.insertAfter(n[t]);
    return e.remove(), n.map((r) => r.getLatest());
  }
  function A(e, n, t) {
    C.useEffect(() => {
      e.hasNodes([f.AutoLinkNode]) || k(77);
      let r = (l, i) => {
        t && t(l, i);
      };
      return v.mergeRegister(e.registerNodeTransform(s.TextNode, (l) => {
        var i = l.getParentOrThrow(), o = l.getPreviousSibling();
        if (f.$isAutoLinkNode(i)) a(i, n, r);
        else if (!f.$isLinkNode(i)) {
          if (l.isSimpleText() && (d.test(l.getTextContent()[0]) || !f.$isAutoLinkNode(o))) {
            for (i = [l], o = l.getNextSibling(); o !== null && s.$isTextNode(o) && o.isSimpleText() && (i.push(o), !/[\s]/.test(o.getTextContent())); )
              o = o.getNextSibling();
            S(
              i,
              n,
              r
            );
          }
          let u = l.getPreviousSibling();
          i = l.getNextSibling(), o = l.getTextContent(), f.$isAutoLinkNode(u) && !d.test(o[0]) && (u.append(l), a(u, n, r), l = u.getURL(), t && t(null, l)), f.$isAutoLinkNode(i) && !d.test(o[o.length - 1]) && (m(i), a(i, n, r), l = i.getURL(), t && t(null, l));
        }
      }));
    }, [e, n, t]);
  }
  return L.AutoLinkPlugin = function({ matchers: e, onChange: n }) {
    let [t] = $.useLexicalComposerContext();
    return A(t, e, n), null;
  }, L.createLinkMatcherWithRegExp = function(e, n = (t) => t) {
    return (t) => (t = e.exec(t), t === null ? null : { index: t.index, length: t[0].length, text: t[0], url: n(t[0]) });
  }, L;
}
export {
  O as __require
};
