import{n as e}from"./chunk-DR8-3Aex.js";import{n as t,t as n}from"./defineProperty-80gRi2ZW.js";import{l as r,p as i}from"./debug-otxXY3j-.js";import{i as a,o}from"./platform-mh9ypxS8.js";import{r as s,t as c}from"./execFileNoThrow-azCkYhmF.js";import{Bd as l,Fd as u}from"./loadAgentsDir-C_wFhytF.js";import{a as d,r as f}from"./detection-95TCHkW8.js";import{tmpdir as p}from"os";import{join as m}from"path";import{randomUUID as h}from"crypto";import{readFile as g,unlink as _}from"fs/promises";function v(e){return`'${e.replace(/'/g,`''`)}'`}function y(e,t){let n=v(t);return[`$ErrorActionPreference = 'Stop'`,`Set-Content -LiteralPath ${n} -Value $PID`,[`try { ${e}; if ($LASTEXITCODE -is [int]) { exit $LASTEXITCODE } }`,`catch { Write-Error $_; exit 1 }`,`finally { Remove-Item -LiteralPath ${n} -Force -ErrorAction SilentlyContinue }`].join(`
`)].join(`; `)}function b(){let e=process.env.CLAUDE_WT_PANE_TIMEOUT_MS;if(!e)return S;let t=Number.parseInt(e,10);return Number.isFinite(t)&&t>0?t:S}async function x(e,t){let n=Date.now()+t,r;for(;Date.now()<n;){try{let t=(await g(e,`utf-8`)).trim();if(!/^\d+$/.test(t))r=Error(`pidFile content not a valid pid: ${JSON.stringify(t)}`);else{let e=Number.parseInt(t,10);if(Number.isFinite(e)&&e>0)return e;r=Error(`pidFile content parsed to invalid pid: ${e}`)}}catch(e){r=e}await new Promise(e=>setTimeout(e,C))}throw r??Error(`pidFile never appeared`)}var S,C,w;e((()=>{r(),s(),o(),f(),u(),t(),S=8e3,C=200,w=class{constructor(e,t){n(this,`type`,`windows-terminal`),n(this,`displayName`,`Windows Terminal`),n(this,`supportsHideShow`,!1),n(this,`panes`,new Map),n(this,`runCommand`,void 0),n(this,`getPlatformValue`,void 0),n(this,`pidFileDir`,void 0),typeof e==`function`||e===void 0?(this.runCommand=e??c,this.getPlatformValue=t??a,this.pidFileDir=p()):(this.runCommand=e.runCommand??c,this.getPlatformValue=e.getPlatform??a,this.pidFileDir=e.pidFileDir??p())}makePidFile(e){return m(this.pidFileDir,`${e.replace(/[^a-zA-Z0-9_-]/g,`-`)}.pid`)}async isAvailable(){return this.getPlatformValue()===`windows`?process.env.WT_SESSION?!0:(await this.runCommand(`where.exe`,[`wt.exe`])).code===0:!1}async isRunningInside(){return this.getPlatformValue()===`windows`&&d()}async createTeammatePaneInSwarmView(e,t){let n=`wt-${h()}`,r=this.panes.size===0;return this.panes.set(n,{title:e,mode:`pane`,pidFile:this.makePidFile(n),status:`registered`}),{paneId:n,isFirstTeammate:r}}async createTeammateWindowInSwarmView(e,t){let n=`wt-${h()}`,r=`teammate-${e.replace(/[^a-zA-Z0-9]/g,`-`).toLowerCase()}`;return this.panes.set(n,{title:e,mode:`window`,pidFile:this.makePidFile(n),status:`registered`}),{paneId:n,isFirstTeammate:!1,windowName:r}}async sendCommandToPane(e,t,n){let r=this.panes.get(e);if(!r)throw Error(`Unknown Windows Terminal pane id: ${e}`);if(r.status===`ready`||r.status===`killing`)throw Error(`Pane ${e} already spawned (status=${r.status}); create a new pane to re-launch`);if(r.status===`spawning`)throw Error(`Pane ${e} is currently spawning; wait for the in-flight launch to complete`);if(r.status===`dead`)throw Error(`Pane ${e} is dead; create a new pane`);let i,a,o=new Promise((e,t)=>{i=e,a=t});o.catch(()=>{}),r.status=`spawning`,r.spawnPromise=o;try{let n=y(t,r.pidFile),a=Buffer.from(n,`utf16le`).toString(`base64`),o=r.mode===`window`?[`-w`,`-1`,`new-tab`,`--title`,r.title]:[`-w`,`0`,`split-pane`,`--vertical`,`--title`,r.title];await _(r.pidFile).catch(()=>{});let s=await this.runCommand(`wt.exe`,[...o,`powershell.exe`,`-NoLogo`,`-NoProfile`,`-ExecutionPolicy`,`Bypass`,`-EncodedCommand`,a]);if(s.code!==0)throw Error(`Failed to launch Windows Terminal teammate ${e}: ${s.stderr}`);let c=b(),l;try{l=await x(r.pidFile,c)}catch(t){throw Error(`Windows Terminal pane failed to launch within ${c}ms\n  paneId: ${e}\n  pidFile: ${r.pidFile}\n  wt.exe stdout: ${s.stdout||`(empty)`}\n  wt.exe stderr: ${s.stderr||`(empty)`}\n  underlying: ${t instanceof Error?t.message:String(t)}\n  override timeout via env CLAUDE_WT_PANE_TIMEOUT_MS`)}r.pid=l,r.status=`ready`,i()}catch(e){throw r.status=`dead`,r.pid=void 0,a(e),e}finally{r.spawnPromise=void 0}}async setPaneBorderColor(e,t,n){}async setPaneTitle(e,t,n,r){}async enablePaneBorderStatus(e,t){}async rebalancePanes(e,t){}async killPane(e,t){let n=this.panes.get(e);if(!n)return!1;if(n.status===`spawning`&&n.spawnPromise&&await n.spawnPromise.catch(()=>{}),n.status===`dead`)return this.panes.delete(e),!1;if(n.status!==`ready`)return!1;n.status=`killing`;let r=n.pid;if(r===void 0){let t=null;for(let r=0;r<3;r++)try{t=(await g(n.pidFile,`utf-8`)).trim();break}catch{if(r===2)return n.status=`dead`,this.panes.delete(e),!1;await new Promise(e=>setTimeout(e,500))}if(!t||!/^\d+$/.test(t))return n.status=`dead`,this.panes.delete(e),!1;let i=Number.parseInt(t,10);if(!Number.isFinite(i)||i<=0)return n.status=`dead`,this.panes.delete(e),!1;r=i}let a=await this.runCommand(`powershell.exe`,[`-NoLogo`,`-NoProfile`,`-Command`,`Stop-Process -Id ${r} -Force -ErrorAction Stop`]);return n.pid=void 0,n.status=`dead`,this.panes.delete(e),i(`[WindowsTerminalBackend] killPane ${e} pid=${r} code=${a.code}`),a.code===0}async hidePane(e,t){return!1}async showPane(e,t,n){return!1}},l(w)}))();export{};