import{n as e}from"./chunk-DR8-3Aex.js";import{n as t,t as n}from"./memoize-DNnuA2aU.js";import{l as r,s as i}from"./envUtils-BWeoiL4Y.js";import{l as a,p as o}from"./debug-otxXY3j-.js";import{n as s,r as c}from"./analytics-DqMQntaB.js";import{n as l,t as u}from"./bundledMode-kSQejuY4.js";import{n as d,t as f}from"./findExecutable-BHQl4NaS.js";import{o as p,s as m}from"./log-DSV4Ht5l.js";import{i as h,o as g}from"./platform-mh9ypxS8.js";import{r as _,t as v}from"./execFileNoThrow-azCkYhmF.js";import{o as y,r as b}from"./stringUtils-KZoO4GUd.js";import{n as x,t as S}from"./distRoot-BNg_iO5-.js";import{homedir as C}from"os";import*as w from"path";import{existsSync as T}from"fs";import{execFile as E,spawn as D}from"child_process";function O(e,t,n){let r=n??process.platform;return T(e)?{mode:`builtin`,command:e,args:[]}:(t===void 0?f(`rg`,[]).cmd:t??`rg`)===`rg`?{mode:`builtin`,command:e,args:[],note:`no ripgrep available on ${r}; install ripgrep via apt/pkg/brew`}:{mode:`system`,command:`rg`,args:[],note:`fallback: builtin rg unavailable on ${r}, using system rg`}}function k(){let e=L();return{rgPath:e.command,rgArgs:e.args,argv0:e.argv0}}function A(e){return e.includes(`os error 11`)||e.includes(`Resource temporarily unavailable`)}function j(e,t,n,r,i=!1){let{rgPath:a,rgArgs:o,argv0:s}=k(),c=i?[`-j`,`1`]:[],l=[...o,...c,...e,t],u=h()===`wsl`?6e4:2e4,d=parseInt(process.env.CLAUDE_CODE_GLOB_TIMEOUT_SECONDS||``,10)||0,f=d>0?d*1e3:u;if(s){let e=D(a,l,{argv0:s,signal:n,windowsHide:!0}),t=``,i=``,o=!1,c=!1;e.stdout?.on(`data`,e=>{o||(t+=e.toString(),t.length>R&&(t=t.slice(0,R),o=!0))}),e.stderr?.on(`data`,e=>{c||(i+=e.toString(),i.length>R&&(i=i.slice(0,R),c=!0))});let u,d=setTimeout(()=>{process.platform===`win32`?e.kill():(e.kill(`SIGTERM`),u=setTimeout(e=>e.kill(`SIGKILL`),5e3,e))},f),p=!1;return e.on(`close`,(e,n)=>{if(!p)if(p=!0,clearTimeout(d),clearTimeout(u),e===0||e===1)r(null,t,i);else{let a=Error(`ripgrep exited with code ${e}`);a.code=e??void 0,a.signal=n??void 0,r(a,t,i)}}),e.on(`error`,e=>{p||(p=!0,clearTimeout(d),clearTimeout(u),r(e,t,i))}),e}return E(a,l,{maxBuffer:R,signal:n,timeout:f,killSignal:process.platform===`win32`?void 0:`SIGKILL`},r)}async function M(e,t,n){await F();let{rgPath:r,rgArgs:i,argv0:a}=k();return new Promise((o,s)=>{let c=D(r,[...i,...e,t],{argv0:a,signal:n,windowsHide:!0,stdio:[`ignore`,`pipe`,`ignore`]}),l=0;c.stdout?.on(`data`,e=>{l+=b(e,`
`)});let u=!1;c.on(`close`,e=>{u||(u=!0,e===0||e===1?o(l):s(Error(`rg --files exited ${e}`)))}),c.on(`error`,e=>{u||(u=!0,s(e))})})}async function N(e,t,n){return await F(),H().catch(e=>{m(e)}),new Promise((r,i)=>{let a=(s,l,u,d)=>{if(!s){r(l.trim().split(`
`).map(e=>e.replace(/\r$/,``)).filter(Boolean));return}if(s.code===1){r([]);return}if([`ENOENT`,`EACCES`,`EPERM`].includes(s.code)){i(s);return}if(!d&&A(u)){o(`rg EAGAIN error detected, retrying with single-threaded mode (-j 1)`),c(`tengu_ripgrep_eagain_retry`,{}),j(e,t,n,(e,t,n)=>{a(e,t,n,!0)},!0);return}let f=l&&l.trim().length>0,p=s.signal===`SIGTERM`||s.signal===`SIGKILL`||s.code===`ABORT_ERR`,g=s.code===`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`,_=[];if(f&&(_=l.trim().split(`
`).map(e=>e.replace(/\r$/,``)).filter(Boolean),_.length>0&&(p||g)&&(_=_.slice(0,-1))),o(`rg error (signal=${s.signal}, code=${s.code}, stderr: ${u}), ${_.length} results`),s.code!==2&&s.code!==`ABORT_ERR`&&m(s),p&&_.length===0){i(new z(`Ripgrep search timed out after ${h()===`wsl`?60:20} seconds. The search may have matched files but did not complete in time. Try searching a more specific path or pattern.`,_));return}r(_)};j(e,t,n,(e,t,n)=>{a(e,t,n,!1)})})}function P(){let e=L();return{mode:e.mode,path:e.command,working:V?.working??null,note:V?.note??e.note}}async function F(){if(process.platform!==`darwin`||U)return;U=!0;let e=L();if(e.mode!==`builtin`)return;let t=e.command;if((await v(`codesign`,[`-vv`,`-d`,t],{preserveOutputOnError:!1})).stdout.split(`
`).find(e=>e.includes(`linker-signed`)))try{let e=await v(`codesign`,[`--sign`,`-`,`--force`,`--preserve-metadata=entitlements,requirements,flags,runtime`,t]);e.code!==0&&m(Error(`Failed to sign ripgrep: ${e.stdout} ${e.stderr}`));let n=await v(`xattr`,[`-d`,`com.apple.quarantine`,t]);n.code!==0&&m(Error(`Failed to remove quarantine: ${n.stdout} ${n.stderr}`))}catch(e){m(e)}}var I,L,R,z,B,V,H,U,W=e((()=>{n(),s(),u(),a(),x(),i(),_(),d(),p(),g(),y(),I=S,L=t(()=>{if(r(process.env.USE_BUILTIN_RIPGREP)){let{cmd:e}=f(`rg`,[]);if(e!==`rg`)return{mode:`system`,command:`rg`,args:[]}}if(l())return{mode:`embedded`,command:process.execPath,args:[`--no-config`],argv0:`rg`};let e=w.resolve(I,`vendor`,`ripgrep`);return O(process.platform===`win32`?w.resolve(e,`${process.arch}-win32`,`rg.exe`):w.resolve(e,`${process.arch}-${process.platform}`,`rg`))}),R=2e7,z=class extends Error{constructor(e,t){super(e),this.partialResults=t,this.name=`RipgrepTimeoutError`}},B=t(async(e,t,n=[])=>{if(w.resolve(e)!==w.resolve(C()))try{let r=[`--files`,`--hidden`];n.forEach(e=>{r.push(`--glob`,`!${e}`)});let i=await M(r,e,t);if(i===0)return 0;let a=10**Math.floor(Math.log10(i));return Math.round(i/a)*a}catch(e){e?.name!==`AbortError`&&m(e)}},(e,t,n=[])=>`${e}|${n.join(`,`)}`),V=null,H=t(async()=>{if(V!==null)return;let e=L();try{let t;if(e.argv0){let n=Bun.spawn([e.command,`--version`],{argv0:e.argv0,stderr:`ignore`,stdout:`pipe`}),[r,i]=await Promise.all([n.stdout.text(),n.exited]);t={code:i,stdout:r}}else t=await v(e.command,[...e.args,`--version`],{timeout:5e3});let n=t.code===0&&!!t.stdout&&t.stdout.startsWith(`ripgrep `);V={working:n,lastTested:Date.now(),config:e,note:e.note},o(`Ripgrep first use test: ${n?`PASSED`:`FAILED`} (mode=${e.mode}, path=${e.command})`),c(`tengu_ripgrep_availability`,{working:+!!n,using_system:+(e.mode===`system`)})}catch(t){V={working:!1,lastTested:Date.now(),config:e,note:e.note},m(t)}}),U=!1}));export{W as a,k as c,P as i,B as n,O as o,L as r,N as s,z as t};