import{iz as p,uz as o}from"./cli-0.5.12-zc0qwdy2.mjs";import"./cli-0.5.12-whfz3xsx.mjs";import"./cli-0.5.12-z100r543.mjs";import"./cli-0.5.12-bzab36pn.mjs";import"./cli-0.5.12-pp1549k7.mjs";import"./cli-0.5.12-9mq24qzr.mjs";import"./cli-0.5.12-7repzgzv.mjs";import"./cli-0.5.12-pdqnfqg3.mjs";import"./cli-0.5.12-c2yq69h9.mjs";import"./cli-0.5.12-kez9r81a.mjs";import"./cli-0.5.12-zbcr8cbk.mjs";import"./cli-0.5.12-2wf629mh.mjs";import"./cli-0.5.12-ghtxkxqx.mjs";import"./cli-0.5.12-xg7a0dy4.mjs";import"./cli-0.5.12-s0h788we.mjs";import"./cli-0.5.12-m22yxpb9.mjs";import"./cli-0.5.12-cw1qs4bg.mjs";import"./cli-0.5.12-yq8ds97a.mjs";import"./cli-0.5.12-qew4mzgp.mjs";import"./cli-0.5.12-gwg1e43k.mjs";import"./cli-0.5.12-36swspqa.mjs";import"./cli-0.5.12-4dn79vqp.mjs";import"./cli-0.5.12-f5pfjvdk.mjs";import"./cli-0.5.12-fqp5s15p.mjs";import"./cli-0.5.12-rc1p9wk6.mjs";import"./cli-0.5.12-x479axrj.mjs";import"./cli-0.5.12-ysy6e9rz.mjs";import"./cli-0.5.12-cqv7z22w.mjs";import"./cli-0.5.12-7cn41yy7.mjs";import{kZ as E,lZ as O,mZ as c,rZ as l}from"./cli-0.5.12-34cd39ve.mjs";import{tZ as y,uZ as V,vZ as v,wZ as R,xZ as M,zZ as i}from"./cli-0.5.12-m2gh4ewc.mjs";import"./cli-0.5.12-c6whkxmz.mjs";import"./cli-0.5.12-w3hy6c60.mjs";import"./cli-0.5.12-5d3qcvf5.mjs";import"./cli-0.5.12-f141d2qa.mjs";import"./cli-0.5.12-khvy67r3.mjs";import{Z2 as _,_2 as d}from"./cli-0.5.12-pycnjaeg.mjs";import"./cli-0.5.12-8v5w8znc.mjs";import"./cli-0.5.12-ctqkj6ve.mjs";import"./cli-0.5.12-cbevkwg4.mjs";import"./cli-0.5.12-w00qj83e.mjs";import"./cli-0.5.12-nbcjwzzn.mjs";import"./cli-0.5.12-3t06hwwh.mjs";import"./cli-0.5.12-057c2cp2.mjs";import"./cli-0.5.12-ecgx5n54.mjs";import{Bca as W,Jca as g,Q6 as H,S6 as T,q8 as A,s8 as w}from"./cli-0.5.12-g4wb8mj2.mjs";import"./cli-0.5.12-vp5smcs1.mjs";import"./cli-0.5.12-y2kq7fe8.mjs";import"./cli-0.5.12-7vxpqr5a.mjs";import"./cli-0.5.12-hvtcdcev.mjs";import"./cli-0.5.12-asbn3d19.mjs";import"./cli-0.5.12-k1crwtdc.mjs";import"./cli-0.5.12-2jxq8gnm.mjs";import{fga as Y,iga as I}from"./cli-0.5.12-p5ps8gf0.mjs";import"./cli-0.5.12-hp0508v9.mjs";import"./cli-0.5.12-tfdwzg37.mjs";import"./cli-0.5.12-60w1a0bs.mjs";import"./cli-0.5.12-c8f4daey.mjs";import"./cli-0.5.12-0qasc0ef.mjs";import"./cli-0.5.12-f8850v2j.mjs";import"./cli-0.5.12-495d826j.mjs";import"./cli-0.5.12-rt0k97yj.mjs";import"./cli-0.5.12-1vyz0tt9.mjs";import{aoa as S}from"./cli-0.5.12-xzeb7mtb.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 W(Error(`[TmuxBackend] Failed to get pane count for ${J} (exit ${Q.code}): ${Q.stderr}`)),null;return A(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();T();g();w();d();i();l();o();D=Promise.resolve();p(N)});s();export{N as TmuxBackend};
