import{gz as p,sz as o}from"./cli-0.5.4-k5fz4zd3.mjs";import"./cli-0.5.4-wbqf44ym.mjs";import"./cli-0.5.4-9ewg9gfs.mjs";import"./cli-0.5.4-we3mspgg.mjs";import"./cli-0.5.4-jzhazry0.mjs";import"./cli-0.5.4-82sw3hrw.mjs";import"./cli-0.5.4-31v143kt.mjs";import"./cli-0.5.4-1cy7wd6s.mjs";import"./cli-0.5.4-5e0pk96f.mjs";import"./cli-0.5.4-z154a7bp.mjs";import"./cli-0.5.4-ng5p0zjs.mjs";import"./cli-0.5.4-5hp7ks3t.mjs";import"./cli-0.5.4-8hdwp78f.mjs";import"./cli-0.5.4-mr9aznxx.mjs";import"./cli-0.5.4-jevfk5q7.mjs";import"./cli-0.5.4-27dzbzdy.mjs";import"./cli-0.5.4-a5bkdqqb.mjs";import"./cli-0.5.4-j2hnw539.mjs";import"./cli-0.5.4-9w59nnw4.mjs";import"./cli-0.5.4-z5d3q8kh.mjs";import"./cli-0.5.4-c0r12dqp.mjs";import"./cli-0.5.4-kqh172jk.mjs";import"./cli-0.5.4-vv4jbgkc.mjs";import"./cli-0.5.4-s7k1f0wc.mjs";import"./cli-0.5.4-0h0yz2k5.mjs";import"./cli-0.5.4-942dm5q1.mjs";import"./cli-0.5.4-rcmp5h4y.mjs";import"./cli-0.5.4-xdcww3x8.mjs";import"./cli-0.5.4-fq32ny1h.mjs";import{EY as l,xY as E,yY as O,zY as c}from"./cli-0.5.4-h1m67ngp.mjs";import{GY as y,HY as V,IY as v,JY as R,KY as M,MY as i}from"./cli-0.5.4-49erze3e.mjs";import"./cli-0.5.4-jytjks37.mjs";import"./cli-0.5.4-mn22gk7g.mjs";import"./cli-0.5.4-5t3jj85n.mjs";import"./cli-0.5.4-5shjmmkz.mjs";import"./cli-0.5.4-zb412kkm.mjs";import"./cli-0.5.4-prfn3sxd.mjs";import"./cli-0.5.4-6xmgebmk.mjs";import"./cli-0.5.4-vjgejcrz.mjs";import{v2 as _,w2 as d}from"./cli-0.5.4-jqt00ywx.mjs";import"./cli-0.5.4-9e7rkd8e.mjs";import"./cli-0.5.4-jp4wtw87.mjs";import"./cli-0.5.4-hambpq58.mjs";import"./cli-0.5.4-syvgmg9g.mjs";import"./cli-0.5.4-w8ttefr5.mjs";import"./cli-0.5.4-j7kkqxv6.mjs";import"./cli-0.5.4-qrwczx7j.mjs";import"./cli-0.5.4-v2z6p9td.mjs";import{B6 as A,J6 as T,R6 as H,T6 as w,r8 as W,t8 as g}from"./cli-0.5.4-cbq01zg3.mjs";import"./cli-0.5.4-37v5fhf1.mjs";import"./cli-0.5.4-7grzwf6q.mjs";import"./cli-0.5.4-9qmek5dq.mjs";import"./cli-0.5.4-9c6r1fjg.mjs";import"./cli-0.5.4-2dw33ms6.mjs";import"./cli-0.5.4-x1fzfxmm.mjs";import"./cli-0.5.4-e822fzaq.mjs";import"./cli-0.5.4-09dvrzcr.mjs";import{Pfa as Y,Sfa as I}from"./cli-0.5.4-end8wj9r.mjs";import"./cli-0.5.4-4x206j04.mjs";import"./cli-0.5.4-yd3rd9qt.mjs";import"./cli-0.5.4-sgcaw17t.mjs";import"./cli-0.5.4-jmc3bzbn.mjs";import"./cli-0.5.4-959vxhhb.mjs";import{Zma as S}from"./cli-0.5.4-aacw1src.mjs";function C(){return _(m)}function u(){let z,G=new Promise((K)=>{z=K}),J=D;return D=G,J.then(()=>z)}function P(z){return{red:"red",blue:"blue",green:"green",yellow:"yellow",purple:"magenta",orange:"colour208",pink:"colour205",cyan:"cyan"}[z]}function f(z){return H(v,z)}function $(z){return H(v,["-L",M(),...z])}class N{type="tmux";displayName="tmux";supportsHideShow=!0;async isAvailable(){return c()}async isRunningInside(){return E()}async createTeammatePaneInSwarmView(z,G){let J=await u();try{if(await this.isRunningInside())return await this.createTeammatePaneWithLeader(z,G);return await this.createTeammatePaneExternal(z,G)}finally{J()}}async sendCommandToPane(z,G,J=!1){let Q=await(J?$:f)(["send-keys","-t",z,G,"Enter"]);if(Q.code!==0)throw Error(`Failed to send command to pane ${z}: ${Q.stderr}`)}async setPaneBorderColor(z,G,J=!1){let K=P(G),Q=J?$:f;await Q(["select-pane","-t",z,"-P",`bg=default,fg=${K}`]),await Q(["set-option","-p","-t",z,"pane-border-style",`fg=${K}`]),await Q(["set-option","-p","-t",z,"pane-active-border-style",`fg=${K}`])}async setPaneTitle(z,G,J,K=!1){let Q=P(J),q=K?$:f;await q(["select-pane","-t",z,"-T",G]),await q(["set-option","-p","-t",z,"pane-border-format",`#[fg=${Q},bold] #{pane_title} #[default]`])}async enablePaneBorderStatus(z,G=!1){let J=z||await this.getCurrentWindowTarget();if(!J)return;await(G?$:f)(["set-option","-w","-t",J,"pane-border-status","top"])}async rebalancePanes(z,G){if(G)await this.rebalancePanesWithLeader(z);else await this.rebalancePanesTiled(z)}async killPane(z,G=!1){return(await(G?$:f)(["kill-pane","-t",z])).code===0}async hidePane(z,G=!1){let J=G?$:f;await J(["new-session","-d","-s",R]);let K=await J(["break-pane","-d","-s",z,"-t",`${R}:`]);if(K.code===0)Y(`[TmuxBackend] Hidden pane ${z}`);else Y(`[TmuxBackend] Failed to hide pane ${z}: ${K.stderr}`);return K.code===0}async showPane(z,G,J=!1){let K=J?$:f,Q=await K(["join-pane","-h","-s",z,"-t",G]);if(Q.code!==0)return Y(`[TmuxBackend] Failed to show pane ${z}: ${Q.stderr}`),!1;Y(`[TmuxBackend] Showed pane ${z} in ${G}`),await K(["select-layout","-t",G,"main-vertical"]);let Z=(await K(["list-panes","-t",G,"-F","#{pane_id}"])).stdout.trim().split(`
`).filter(Boolean);if(Z[0])await K(["resize-pane","-t",Z[0],"-x","30%"]);return!0}async getCurrentPaneId(){let z=O();if(z)return z;let G=await H(v,["display-message","-p","#{pane_id}"]);if(G.code!==0)return Y(`[TmuxBackend] Failed to get current pane ID (exit ${G.code}): ${G.stderr}`),null;return G.stdout.trim()}async getCurrentWindowTarget(){if(h)return h;let z=O(),G=["display-message"];if(z)G.push("-t",z);G.push("-p","#{session_name}:#{window_index}");let J=await H(v,G);if(J.code!==0)return Y(`[TmuxBackend] Failed to get current window target (exit ${J.code}): ${J.stderr}`),null;return h=J.stdout.trim(),h}async getCurrentWindowPaneCount(z,G=!1){let J=z||await this.getCurrentWindowTarget();if(!J)return null;let K=["list-panes","-t",J,"-F","#{pane_id}"],Q=G?await $(K):await f(K);if(Q.code!==0)return A(Error(`[TmuxBackend] Failed to get pane count for ${J} (exit ${Q.code}): ${Q.stderr}`)),null;return W(Q.stdout.trim().split(`
`),Boolean)}async hasSessionInSwarm(z){return(await $(["has-session","-t",z])).code===0}async createExternalSwarmSession(){if(!await this.hasSessionInSwarm(y)){let q=await $(["new-session","-d","-s",y,"-n",V,"-P","-F","#{pane_id}"]);if(q.code!==0)throw Error(`Failed to create swarm session: ${q.stderr||"Unknown error"}`);let Z=q.stdout.trim(),B=`${y}:${V}`;return Y(`[TmuxBackend] Created external swarm session with window ${B}, pane ${Z}`),{windowTarget:B,paneId:Z}}let J=(await $(["list-windows","-t",y,"-F","#{window_name}"])).stdout.trim().split(`
`).filter(Boolean),K=`${y}:${V}`;if(J.includes(V)){let Z=(await $(["list-panes","-t",K,"-F","#{pane_id}"])).stdout.trim().split(`
`).filter(Boolean);return{windowTarget:K,paneId:Z[0]||""}}let Q=await $(["new-window","-t",y,"-n",V,"-P","-F","#{pane_id}"]);if(Q.code!==0)throw Error(`Failed to create swarm-view window: ${Q.stderr||"Unknown error"}`);return{windowTarget:K,paneId:Q.stdout.trim()}}async createTeammatePaneWithLeader(z,G){let J=await this.getCurrentPaneId(),K=await this.getCurrentWindowTarget();if(!J||!K)throw Error("Could not determine current tmux pane/window");let Q=await this.getCurrentWindowPaneCount(K);if(Q===null)throw Error("Could not determine pane count for current window");let q=Q===1,Z;if(q)Z=await H(v,["split-window","-t",J,"-h","-l","70%","-P","-F","#{pane_id}"]);else{let j=(await H(v,["list-panes","-t",K,"-F","#{pane_id}"])).stdout.trim().split(`
`).filter(Boolean).slice(1),b=j.length,U=b%2===1,k=Math.floor((b-1)/2),x=j[k]||j[j.length-1];Z=await H(v,["split-window","-t",x,U?"-v":"-h","-P","-F","#{pane_id}"])}if(Z.code!==0)throw Error(`Failed to create teammate pane: ${Z.stderr}`);let B=Z.stdout.trim();return Y(`[TmuxBackend] Created teammate pane for ${z}: ${B}`),await this.setPaneBorderColor(B,G),await this.setPaneTitle(B,z,G),await this.rebalancePanesWithLeader(K),await C(),{paneId:B,isFirstTeammate:q}}async createTeammatePaneExternal(z,G){let{windowTarget:J,paneId:K}=await this.createExternalSwarmSession(),Q=await this.getCurrentWindowPaneCount(J,!0);if(Q===null)throw Error("Could not determine pane count for swarm window");let q=!F&&Q===1,Z;if(q)Z=K,F=!0,Y(`[TmuxBackend] Using initial pane for first teammate ${z}: ${Z}`),await this.enablePaneBorderStatus(J,!0);else{let X=(await $(["list-panes","-t",J,"-F","#{pane_id}"])).stdout.trim().split(`
`).filter(Boolean),L=X.length,j=L%2===1,b=Math.floor((L-1)/2),U=X[b]||X[X.length-1],k=await $(["split-window","-t",U,j?"-v":"-h","-P","-F","#{pane_id}"]);if(k.code!==0)throw Error(`Failed to create teammate pane: ${k.stderr}`);Z=k.stdout.trim(),Y(`[TmuxBackend] Created teammate pane for ${z}: ${Z}`)}return await this.setPaneBorderColor(Z,G,!0),await this.setPaneTitle(Z,z,G,!0),await this.rebalancePanesTiled(J),await C(),{paneId:Z,isFirstTeammate:q}}async rebalancePanesWithLeader(z){let J=(await f(["list-panes","-t",z,"-F","#{pane_id}"])).stdout.trim().split(`
`).filter(Boolean);if(J.length<=2)return;await f(["select-layout","-t",z,"main-vertical"]);let K=J[0];await f(["resize-pane","-t",K,"-x","30%"]),Y(`[TmuxBackend] Rebalanced ${J.length-1} teammate panes with leader`)}async rebalancePanesTiled(z){let J=(await $(["list-panes","-t",z,"-F","#{pane_id}"])).stdout.trim().split(`
`).filter(Boolean);if(J.length<=1)return;await $(["select-layout","-t",z,"tiled"]),Y(`[TmuxBackend] Rebalanced ${J.length} teammate panes with tiled layout`)}}var F=!1,h=null,D,m=200;var s=S(()=>{I();w();T();g();d();i();l();o();D=Promise.resolve();p(N)});s();export{N as TmuxBackend};
