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{o as a,s as o}from"./log-DSV4Ht5l.js";import{r as s,t as c}from"./execFileNoThrow-azCkYhmF.js";import{b as l,x as u}from"./teammate-CbskW5G9.js";import{n as d,t as f}from"./sleep-C6koP5Ka.js";import{Fd as p,zd as m}from"./loadAgentsDir-C_wFhytF.js";import{c as h,n as g,o as _,r as v,s as y,t as b}from"./constants-BljlAJOX.js";import{l as x,n as S,o as C,r as w}from"./detection-95TCHkW8.js";function T(){return d(N)}function E(){let e,t=new Promise(t=>{e=t}),n=M;return M=t,n.then(()=>e)}function D(e){return{red:`red`,blue:`blue`,green:`green`,yellow:`yellow`,purple:`magenta`,orange:`colour208`,pink:`colour205`,cyan:`cyan`}[e]}function O(e){return c(_,e)}function k(e){return c(_,[`-L`,y(),...e])}var A,j,M,N,P;e((()=>{r(),s(),a(),u(),f(),h(),w(),p(),t(),A=!1,j=null,M=Promise.resolve(),N=200,P=class{constructor(){n(this,`type`,`tmux`),n(this,`displayName`,`tmux`),n(this,`supportsHideShow`,!0)}async isAvailable(){return x()}async isRunningInside(){return C()}async createTeammatePaneInSwarmView(e,t){let n=await E();try{return await this.isRunningInside()?await this.createTeammatePaneWithLeader(e,t):await this.createTeammatePaneExternal(e,t)}finally{n()}}async createTeammateWindowInSwarmView(e,t){let n=`teammate-${e.replace(/[^a-zA-Z0-9]/g,`-`).toLowerCase()}`,{windowTarget:r}=await this.createExternalSwarmSession(),i=await k([`new-window`,`-t`,g,`-n`,n,`-P`,`-F`,`#{pane_id}`]);if(i.code!==0)throw Error(`Failed to create tmux window: ${i.stderr||`Unknown error`}`);let a=i.stdout.trim();return await this.setPaneTitle(a,e,t,!0),await this.setPaneBorderColor(a,t,!0),{paneId:a,isFirstTeammate:!1,windowName:n}}async sendCommandToPane(e,t,n=!1){let r=await(n?k:O)([`send-keys`,`-t`,e,t,`Enter`]);if(r.code!==0)throw Error(`Failed to send command to pane ${e}: ${r.stderr}`)}async setPaneBorderColor(e,t,n=!1){let r=D(t),i=n?k:O;await i([`select-pane`,`-t`,e,`-P`,`bg=default,fg=${r}`]),await i([`set-option`,`-p`,`-t`,e,`pane-border-style`,`fg=${r}`]),await i([`set-option`,`-p`,`-t`,e,`pane-active-border-style`,`fg=${r}`])}async setPaneTitle(e,t,n,r=!1){let i=D(n),a=r?k:O;await a([`select-pane`,`-t`,e,`-T`,t]),await a([`set-option`,`-p`,`-t`,e,`pane-border-format`,`#[fg=${i},bold] #{pane_title} #[default]`])}async enablePaneBorderStatus(e,t=!1){let n=e||await this.getCurrentWindowTarget();n&&await(t?k:O)([`set-option`,`-w`,`-t`,n,`pane-border-status`,`top`])}async rebalancePanes(e,t){t?await this.rebalancePanesWithLeader(e):await this.rebalancePanesTiled(e)}async killPane(e,t=!1){return(await(t?k:O)([`kill-pane`,`-t`,e])).code===0}async hidePane(e,t=!1){let n=t?k:O;await n([`new-session`,`-d`,`-s`,b]);let r=await n([`break-pane`,`-d`,`-s`,e,`-t`,`${b}:`]);return r.code===0?i(`[TmuxBackend] Hidden pane ${e}`):i(`[TmuxBackend] Failed to hide pane ${e}: ${r.stderr}`),r.code===0}async showPane(e,t,n=!1){let r=n?k:O,a=await r([`join-pane`,`-h`,`-s`,e,`-t`,t]);if(a.code!==0)return i(`[TmuxBackend] Failed to show pane ${e}: ${a.stderr}`),!1;i(`[TmuxBackend] Showed pane ${e} in ${t}`),await r([`select-layout`,`-t`,t,`main-vertical`]);let o=(await r([`list-panes`,`-t`,t,`-F`,`#{pane_id}`])).stdout.trim().split(`
`).filter(Boolean);return o[0]&&await r([`resize-pane`,`-t`,o[0],`-x`,`30%`]),!0}async getCurrentPaneId(){let e=S();if(e)return e;let t=await c(_,[`display-message`,`-p`,`#{pane_id}`]);return t.code===0?t.stdout.trim():(i(`[TmuxBackend] Failed to get current pane ID (exit ${t.code}): ${t.stderr}`),null)}async getCurrentWindowTarget(){if(j)return j;let e=S(),t=[`display-message`];e&&t.push(`-t`,e),t.push(`-p`,`#{session_name}:#{window_index}`);let n=await c(_,t);return n.code===0?(j=n.stdout.trim(),j):(i(`[TmuxBackend] Failed to get current window target (exit ${n.code}): ${n.stderr}`),null)}async getCurrentWindowPaneCount(e,t=!1){let n=e||await this.getCurrentWindowTarget();if(!n)return null;let r=[`list-panes`,`-t`,n,`-F`,`#{pane_id}`],i=t?await k(r):await O(r);return i.code===0?l(i.stdout.trim().split(`
`),Boolean):(o(Error(`[TmuxBackend] Failed to get pane count for ${n} (exit ${i.code}): ${i.stderr}`)),null)}async hasSessionInSwarm(e){return(await k([`has-session`,`-t`,e])).code===0}async createExternalSwarmSession(){if(!await this.hasSessionInSwarm(`claude-swarm`)){let e=await k([`new-session`,`-d`,`-s`,g,`-n`,v,`-P`,`-F`,`#{pane_id}`]);if(e.code!==0)throw Error(`Failed to create swarm session: ${e.stderr||`Unknown error`}`);let t=e.stdout.trim(),n=`${g}:${v}`;return i(`[TmuxBackend] Created external swarm session with window ${n}, pane ${t}`),{windowTarget:n,paneId:t}}let e=(await k([`list-windows`,`-t`,g,`-F`,`#{window_name}`])).stdout.trim().split(`
`).filter(Boolean),t=`${g}:${v}`;if(e.includes(`swarm-view`))return{windowTarget:t,paneId:(await k([`list-panes`,`-t`,t,`-F`,`#{pane_id}`])).stdout.trim().split(`
`).filter(Boolean)[0]||``};let n=await k([`new-window`,`-t`,g,`-n`,v,`-P`,`-F`,`#{pane_id}`]);if(n.code!==0)throw Error(`Failed to create swarm-view window: ${n.stderr||`Unknown error`}`);return{windowTarget:t,paneId:n.stdout.trim()}}async createTeammatePaneWithLeader(e,t){let n=await this.getCurrentPaneId(),r=await this.getCurrentWindowTarget();if(!n||!r)throw Error(`Could not determine current tmux pane/window`);let a=await this.getCurrentWindowPaneCount(r);if(a===null)throw Error(`Could not determine pane count for current window`);let o=a===1,s;if(o)s=await c(_,[`split-window`,`-t`,n,`-h`,`-l`,`70%`,`-P`,`-F`,`#{pane_id}`]);else{let e=(await c(_,[`list-panes`,`-t`,r,`-F`,`#{pane_id}`])).stdout.trim().split(`
`).filter(Boolean).slice(1),t=e.length,n=t%2==1;s=await c(_,[`split-window`,`-t`,e[Math.floor((t-1)/2)]||e[e.length-1],n?`-v`:`-h`,`-P`,`-F`,`#{pane_id}`])}if(s.code!==0)throw Error(`Failed to create teammate pane: ${s.stderr}`);let l=s.stdout.trim();return i(`[TmuxBackend] Created teammate pane for ${e}: ${l}`),await this.setPaneBorderColor(l,t),await this.setPaneTitle(l,e,t),await this.rebalancePanesWithLeader(r),await T(),{paneId:l,isFirstTeammate:o}}async createTeammatePaneExternal(e,t){let{windowTarget:n,paneId:r}=await this.createExternalSwarmSession(),a=await this.getCurrentWindowPaneCount(n,!0);if(a===null)throw Error(`Could not determine pane count for swarm window`);let o=!A&&a===1,s;if(o)s=r,A=!0,i(`[TmuxBackend] Using initial pane for first teammate ${e}: ${s}`),await this.enablePaneBorderStatus(n,!0);else{let t=(await k([`list-panes`,`-t`,n,`-F`,`#{pane_id}`])).stdout.trim().split(`
`).filter(Boolean),r=t.length,a=r%2==1,o=await k([`split-window`,`-t`,t[Math.floor((r-1)/2)]||t[t.length-1],a?`-v`:`-h`,`-P`,`-F`,`#{pane_id}`]);if(o.code!==0)throw Error(`Failed to create teammate pane: ${o.stderr}`);s=o.stdout.trim(),i(`[TmuxBackend] Created teammate pane for ${e}: ${s}`)}return await this.setPaneBorderColor(s,t,!0),await this.setPaneTitle(s,e,t,!0),await this.rebalancePanesTiled(n),await T(),{paneId:s,isFirstTeammate:o}}async rebalancePanesWithLeader(e){let t=(await O([`list-panes`,`-t`,e,`-F`,`#{pane_id}`])).stdout.trim().split(`
`).filter(Boolean);if(t.length<=2)return;await O([`select-layout`,`-t`,e,`main-vertical`]);let n=t[0];await O([`resize-pane`,`-t`,n,`-x`,`30%`]),i(`[TmuxBackend] Rebalanced ${t.length-1} teammate panes with leader`)}async rebalancePanesTiled(e){let t=(await k([`list-panes`,`-t`,e,`-F`,`#{pane_id}`])).stdout.trim().split(`
`).filter(Boolean);t.length<=1||(await k([`select-layout`,`-t`,e,`tiled`]),i(`[TmuxBackend] Rebalanced ${t.length} teammate panes with tiled layout`))}},m(P)}))();export{};