import{h as x,C as R,g as H,f as W,r as _}from"./vendor-regex-utilities-2BQyy0xI.js";const d=String.raw,j=d`\\g<(?<gRNameOrNum>[^>&]+)&R=(?<gRDepth>[^>]+)>`,U=d`\(\?R=(?<rDepth>[^\)]+)\)|${j}`,T=d`\(\?<(?![=!])(?<captureName>[^>]+)>`,G=d`${T}|(?<unnamed>\()(?!\?)`,C=new RegExp(d`${T}|${U}|\(\?|\\?.`,"gsu"),L="Cannot use multiple overlapping recursions";function J(e,n){const{hiddenCaptures:t,mode:r}={hiddenCaptures:[],mode:"plugin",...n};let h=n?.captureTransfers??new Map;if(!new RegExp(U,"su").test(e))return{pattern:e,captureTransfers:h,hiddenCaptures:t};if(r==="plugin"&&x(e,d`\(\?\(DEFINE\)`,R.DEFAULT))throw new Error("DEFINE groups cannot be used with recursion");const a=[],o=x(e,d`\\[1-9]`,R.DEFAULT),f=new Map,l=[];let w=!1,i=0,u=0,m;for(C.lastIndex=0;m=C.exec(e);){const{0:g,groups:{captureName:p,rDepth:c,gRNameOrNum:s,gRDepth:D}}=m;if(g==="[")i++;else if(i)g==="]"&&i--;else if(c){if(v(c),w)throw new Error(L);if(o)throw new Error(`${r==="external"?"Backrefs":"Numbered backrefs"} cannot be used with global recursion`);const $=e.slice(0,m.index),E=e.slice(C.lastIndex);if(x(E,U,R.DEFAULT))throw new Error(L);const N=+c-1;e=B($,E,N,!1,t,a,u),h=S(h,$,N,a.length,0,u);break}else if(s){v(D);let $=!1;for(const b of l)if(b.name===s||b.num===+s){if($=!0,b.hasRecursedWithin)throw new Error(L);break}if(!$)throw new Error(d`Recursive \g cannot be used outside the referenced group "${r==="external"?s:d`\g<${s}&R=${D}>`}"`);const E=f.get(s),N=H(e,E);if(o&&x(N,d`${T}|\((?!\?)`,R.DEFAULT))throw new Error(`${r==="external"?"Backrefs":"Numbered backrefs"} cannot be used with recursion of capturing groups`);const k=e.slice(E,m.index),F=N.slice(k.length+g.length),I=a.length,A=+D-1,M=B(k,F,A,!0,t,a,u);h=S(h,k,A,a.length-I,I,u);const O=e.slice(0,E),z=e.slice(E+N.length);e=`${O}${M}${z}`,C.lastIndex+=M.length-g.length-k.length-F.length,l.forEach(b=>b.hasRecursedWithin=!0),w=!0}else if(p)u++,f.set(String(u),C.lastIndex),f.set(p,C.lastIndex),l.push({num:u,name:p});else if(g[0]==="("){const $=g==="(";$&&(u++,f.set(String(u),C.lastIndex)),l.push($?{num:u}:{})}else g===")"&&l.pop()}return t.push(...a),{pattern:e,captureTransfers:h,hiddenCaptures:t}}function v(e){const n=`Max depth must be integer between 2 and 100; used ${e}`;if(!/^[1-9]\d*$/.test(e))throw new Error(n);if(e=+e,e<2||e>100)throw new Error(n)}function B(e,n,t,r,h,a,o){const f=new Set;r&&W(e+n,T,({groups:{captureName:w}})=>{f.add(w)},R.DEFAULT);const l=[t,r?f:null,h,a,o];return`${e}${P(`(?:${e}`,"forward",...l)}(?:)${P(`${n})`,"backward",...l)}${n}`}function P(e,n,t,r,h,a,o){const l=i=>n==="forward"?i+2:t-i+2-1;let w="";for(let i=0;i<t;i++){const u=l(i);w+=_(e,d`${G}|\\k<(?<backref>[^>]+)>`,({0:m,groups:{captureName:g,unnamed:p,backref:c}})=>{if(c&&r&&!r.has(c))return m;const s=`_$${u}`;if(p||g){const D=o+a.length+1;return a.push(D),q(h,D),p?m:`(?<${g}${s}>`}return d`\k<${c}${s}>`},R.DEFAULT)}return w}function q(e,n){for(let t=0;t<e.length;t++)e[t]>=n&&e[t]++}function S(e,n,t,r,h,a){if(e.size&&r){let o=0;W(n,G,()=>o++,R.DEFAULT);const f=a-o+h,l=new Map;return e.forEach((w,i)=>{const u=(r-o*t)/t,m=o*t,g=i>f+o?i+r:i,p=[];for(const c of w)if(c<=f)p.push(c);else if(c>f+o+u)p.push(c+r);else if(c<=f+o)for(let s=0;s<=t;s++)p.push(c+o*s);else for(let s=0;s<=t;s++)p.push(c+m+u*s);l.set(g,p)}),l}return e}export{J as r};
