// @bun
import{PG as U}from"./chunk-xyhgvt88.js";import{dlopen as S,FFIType as x,ptr as M}from"bun:ffi";import{Buffer as G}from"buffer";class O{listeners=[];event=(z)=>{return this.listeners.push(z),{dispose:()=>{let H=this.listeners.indexOf(z);if(H!==-1)this.listeners.splice(H,1)}}};fire(z){for(let H of this.listeners)H(z)}}import{join as Y,dirname as R,basename as L}from"path";import{existsSync as j}from"fs";var k=80,N=24,_="sh",B="xterm";function P(z){if(z.length===0)return"''";return`'${z.replace(/'/g,"'\\''")}'`}function v(){let z=process.env.BUN_PTY_LIB;if(z&&j(z))return z;try{let q=U(`../rust-pty/target/release/${process.platform==="win32"?"rust_pty.dll":process.platform==="darwin"?process.arch==="arm64"?"librust_pty_arm64.dylib":"librust_pty.dylib":process.arch==="arm64"?"librust_pty_arm64.so":"librust_pty.so"}`);if(q)return q}catch{}let{platform:H,arch:J}=process,X=H==="darwin"?J==="arm64"?["librust_pty_arm64.dylib","librust_pty.dylib"]:["librust_pty.dylib"]:H==="win32"?["rust_pty.dll"]:J==="arm64"?["librust_pty_arm64.so","librust_pty.so"]:["librust_pty.so"],V=Bun.fileURLToPath(import.meta.url),$=R(V),W=L($),K=W==="src"||W==="dist"?R($):$,w=[Y(K,"rust-pty","target","release"),Y(K,"..","bun-pty","rust-pty","target","release"),Y(process.cwd(),"node_modules","bun-pty","rust-pty","target","release")],C=[];for(let q of w)for(let A of X)C.push(Y(q,A));for(let q of C)if(j(q))return q;throw Error(`librust_pty shared library not found.
Checked:
  - BUN_PTY_LIB=${z??"<unset>"}
  - ${C.join(`
  - `)}

Set BUN_PTY_LIB or ensure one of these paths contains the file.`)}var g=v(),Z;try{Z=S(g,{bun_pty_spawn:{args:[x.cstring,x.cstring,x.cstring,x.i32,x.i32],returns:x.i32},bun_pty_write:{args:[x.i32,x.pointer,x.i32],returns:x.i32},bun_pty_read:{args:[x.i32,x.pointer,x.i32],returns:x.i32},bun_pty_resize:{args:[x.i32,x.i32,x.i32],returns:x.i32},bun_pty_kill:{args:[x.i32],returns:x.i32},bun_pty_get_pid:{args:[x.i32],returns:x.i32},bun_pty_get_exit_code:{args:[x.i32],returns:x.i32},bun_pty_close:{args:[x.i32],returns:x.void}})}catch(z){console.error("Failed to load lib",z)}class Q{handle=-1;_pid=-1;_cols=k;_rows=N;_name=B;_readLoop=!1;_closing=!1;_decoder=new TextDecoder("utf-8");_onData=new O;_onExit=new O;constructor(z=_,H=[],J={name:B}){this._cols=J.cols??k,this._rows=J.rows??N;let X=J.cwd??process.cwd(),V=[P(z),...H.map(P)].join(" "),$="";if(J.env)$=Object.entries(J.env).map(([K,w])=>`${K}=${w}`).join("\x00")+"\x00";if(this.handle=Z.symbols.bun_pty_spawn(G.from(`${V}\x00`,"utf8"),G.from(`${X}\x00`,"utf8"),G.from(`${$}\x00`,"utf8"),this._cols,this._rows),this.handle<0)throw Error("PTY spawn failed");this._pid=Z.symbols.bun_pty_get_pid(this.handle),this._startReadLoop()}get pid(){return this._pid}get cols(){return this._cols}get rows(){return this._rows}get process(){return"shell"}get onData(){return this._onData.event}get onExit(){return this._onExit.event}write(z){if(this._closing)return;let H=G.from(z,"utf8");Z.symbols.bun_pty_write(this.handle,M(H),H.length)}resize(z,H){if(this._closing)return;this._cols=z,this._rows=H,Z.symbols.bun_pty_resize(this.handle,z,H)}kill(z="SIGTERM"){if(this._closing)return;this._closing=!0,Z.symbols.bun_pty_kill(this.handle),Z.symbols.bun_pty_close(this.handle),this._onExit.fire({exitCode:0,signal:z})}async _startReadLoop(){if(this._readLoop)return;this._readLoop=!0;let z=G.allocUnsafe(4096);while(this._readLoop&&!this._closing){let H=Z.symbols.bun_pty_read(this.handle,M(z),z.length);if(H>0){let J=this._decoder.decode(z.subarray(0,H),{stream:!0});if(J)this._onData.fire(J)}else if(H===-2){let J=this._decoder.decode();if(J)this._onData.fire(J);let X=Z.symbols.bun_pty_get_exit_code(this.handle);this._onExit.fire({exitCode:X});break}else if(H<0){let J=this._decoder.decode();if(J)this._onData.fire(J);break}else await new Promise((J)=>setTimeout(J,8))}}}function D(z,H,J){return new Q(z,H,J)}function f(z,H,J){let X=D(z,H,J);return{pid:X.pid,onData(V){return X.onData(V)},onExit(V){return X.onExit(V)},write(V){X.write(V)},resize(V,$){X.resize(V,$)},kill(V){X.kill(V)}}}export{f as spawn};
