/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
'use strict';var h=require("@lexical/link"),l=require("@lexical/react/LexicalComposerContext"),m=require("@lexical/utils"),q=require("lexical"),y=require("react");function z(b,f){for(let c=0;c<f.length;c++){let a=f[c](b);if(a)return a}return null}let A=/[.,;\s]/;function C(b){b=b.getPreviousSibling();q.$isElementNode(b)&&(b=b.getLastDescendant());var f;!(f=null===b||q.$isLineBreakNode(b))&&(f=q.$isTextNode(b))&&(b=b.getTextContent(),f=A.test(b[b.length-1]));return f}
function D(b){b=b.getNextSibling();q.$isElementNode(b)&&(b=b.getFirstDescendant());return null===b||q.$isLineBreakNode(b)||q.$isTextNode(b)&&A.test(b.getTextContent()[0])}function E(b){let f=b.getChildren();var c=f.length;for(--c;0<=c;c--)b.insertAfter(f[c]);b.remove();return f.map(a=>a.getLatest())}
function F(b,f,c){y.useEffect(()=>{if(!b.hasNodes([h.AutoLinkNode]))throw Error("Minified Lexical error #77; visit https://lexical.dev/docs/error?code=77 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");return m.mergeRegister(b.registerNodeTransform(q.TextNode,a=>{var e=a.getParentOrThrow();if(h.$isAutoLinkNode(e))a:{a=e.getChildren();var d=a.length;for(var g=0;g<d;g++){var n=a[g];if(!q.$isTextNode(n)||!n.isSimpleText()){E(e);a=e.getURL();
c&&c(null,a);break a}}d=e.getTextContent();a=z(d,f);null===a||a.text!==d?(E(e),a=e.getURL(),c&&c(null,a)):C(e)&&D(e)?(d=e.getURL(),d!==a.url&&(e.setURL(a.url),c&&c(a.url,d)),a.attributes&&(d=e.getRel(),d!==a.attributes.rel&&(e.setRel(a.attributes.rel||null),c&&c(a.attributes.rel||null,d)),d=e.getTarget(),d!==a.attributes.target&&(e.setTarget(a.attributes.target||null),c&&c(a.attributes.target||null,d)))):(E(e),a=e.getURL(),c&&c(null,a))}else if(!h.$isLinkNode(e)){if(a.isSimpleText()){n=e=a.getTextContent();
let k=0,u=a;for(;(d=z(n,f))&&null!==d;){let r=d.index,v=d.length,w=r+v;var t=k+r,p=k+w,x=e,B=a;(0<t?A.test(x[t-1]):C(B))&&(p<x.length?A.test(x[p]):D(B))?(0===k+r?[g,u]=u.splitText(k+v):[,g,u]=u.splitText(k+r,k+r+v),t=h.$createAutoLinkNode(d.url,d.attributes),p=q.$createTextNode(d.text),p.setFormat(g.getFormat()),p.setDetail(g.getDetail()),t.append(p),g.replace(t),c&&c(d.url,null),k=0):k+=w;n=n.substring(w)}}g=a.getPreviousSibling();d=a.getNextSibling();a=a.getTextContent();h.$isAutoLinkNode(g)&&!A.test(a[0])&&
(E(g),g=g.getURL(),c&&c(null,g));h.$isAutoLinkNode(d)&&!A.test(a[a.length-1])&&(E(d),a=d.getURL(),c&&c(null,a))}}))},[b,f,c])}exports.AutoLinkPlugin=function({matchers:b,onChange:f}){let [c]=l.useLexicalComposerContext();F(c,b,f);return null}
