import{ez as p,qz as o}from"./cli-0.5.3-jbzr8k4s.mjs";import"./cli-0.5.3-me9jkw92.mjs";import"./cli-0.5.3-bqmztzjc.mjs";import"./cli-0.5.3-ytqxzjff.mjs";import"./cli-0.5.3-h6n812td.mjs";import"./cli-0.5.3-hg8c6qbh.mjs";import"./cli-0.5.3-39jr2yrx.mjs";import"./cli-0.5.3-42e2paz8.mjs";import"./cli-0.5.3-mn45xfck.mjs";import"./cli-0.5.3-m884a3pk.mjs";import"./cli-0.5.3-4f9xxrem.mjs";import"./cli-0.5.3-gr8nsa2k.mjs";import"./cli-0.5.3-yj9nbd9d.mjs";import"./cli-0.5.3-55kd38ff.mjs";import"./cli-0.5.3-e6bjcfwg.mjs";import"./cli-0.5.3-4dbxtwh3.mjs";import"./cli-0.5.3-m0kfmb79.mjs";import"./cli-0.5.3-8zq9vdtg.mjs";import"./cli-0.5.3-vmx7agj7.mjs";import"./cli-0.5.3-h8xs2818.mjs";import"./cli-0.5.3-wcx7cnw5.mjs";import"./cli-0.5.3-9zbakvbf.mjs";import"./cli-0.5.3-hx550gz2.mjs";import"./cli-0.5.3-842pf3t4.mjs";import"./cli-0.5.3-wxead1ct.mjs";import"./cli-0.5.3-s1hz35ye.mjs";import"./cli-0.5.3-g0svn6bx.mjs";import"./cli-0.5.3-sfdh3pkx.mjs";import"./cli-0.5.3-d8tjnxcj.mjs";import{CY as l,vY as E,wY as O,xY as c}from"./cli-0.5.3-6zfw1732.mjs";import{EY as y,FY as V,GY as v,HY as R,IY as M,KY as i}from"./cli-0.5.3-ptjdgcsy.mjs";import"./cli-0.5.3-tp3jye12.mjs";import"./cli-0.5.3-vqmxwet2.mjs";import"./cli-0.5.3-rch86nhn.mjs";import"./cli-0.5.3-zg2apr51.mjs";import"./cli-0.5.3-ngnshpb7.mjs";import"./cli-0.5.3-e58xste9.mjs";import"./cli-0.5.3-6163zevg.mjs";import"./cli-0.5.3-jmkkd5ph.mjs";import{t2 as _,u2 as d}from"./cli-0.5.3-1vt6twff.mjs";import"./cli-0.5.3-7017ez65.mjs";import"./cli-0.5.3-ea94acj4.mjs";import"./cli-0.5.3-3k22nqvy.mjs";import"./cli-0.5.3-63q4g3zt.mjs";import"./cli-0.5.3-dyhbm5kp.mjs";import"./cli-0.5.3-vpcrbd8n.mjs";import"./cli-0.5.3-jdw3ajfv.mjs";import"./cli-0.5.3-qg4a557t.mjs";import{H6 as T,P6 as H,R6 as w,p8 as W,r8 as g,z6 as A}from"./cli-0.5.3-t65dek0m.mjs";import"./cli-0.5.3-kavpx97n.mjs";import"./cli-0.5.3-9bdwhdys.mjs";import"./cli-0.5.3-ttc72eq3.mjs";import"./cli-0.5.3-g75gdzrp.mjs";import"./cli-0.5.3-1rg2r7n7.mjs";import"./cli-0.5.3-6j2jk6dt.mjs";import"./cli-0.5.3-66650sba.mjs";import"./cli-0.5.3-9vh3vpap.mjs";import{Nfa as Y,Qfa as I}from"./cli-0.5.3-n3cxyxpg.mjs";import"./cli-0.5.3-kbvzmaax.mjs";import"./cli-0.5.3-7wdgps7z.mjs";import"./cli-0.5.3-c8bcrqa4.mjs";import"./cli-0.5.3-qrvd6cmy.mjs";import"./cli-0.5.3-2dk1sk8w.mjs";import{Xma as S}from"./cli-0.5.3-0df3r78b.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};
