import{n as e,o as t}from"./chunk-DR8-3Aex.js";import{Rn as n,b as r,d as i,f as a,nt as o,t as s,x as c}from"./src-Di342QoJ.js";import{t as l}from"./jsx-runtime-D-D469L8.js";import{U as u,z as d}from"./debug-otxXY3j-.js";import{sS as f,uS as p}from"./loadAgentsDir-C_wFhytF.js";import{t as m}from"./useKeybinding-DPOUuymz.js";import{a as h,t as g}from"./bridgeConfig-pjDV5Z2l.js";import{n as _,r as v}from"./bridgeEnabled-D9jI6X-w.js";import{a as y,n as b}from"./types-C0nqJFkV.js";import{t as x}from"./Dialog-91X6ZGg9.js";import{t as S}from"./ListItem-DCQhApdJ.js";import{i as C,o as w,t as T}from"./cliLaunch-Citfs01Q.js";import{resolve as E}from"path";function D({onDone:e}){let[t,n]=(0,N.useState)(I),[r,i]=(0,N.useState)(null);return(0,N.useEffect)(()=>{if(F&&!F.killed){n(`running`);return}let t=!1;return(async()=>{let r=await k();if(!t){if(r){e(r,{display:`system`});return}n(`starting`);try{A(),t||(n(`running`),I=`running`,e(`Remote Control Server started. Use /remote-control-server to manage.`,{display:`system`}))}catch(r){if(!t){let t=d(r);n(`error`),i(t),I=`error`,e(`Remote Control Server failed to start: ${t}`,{display:`system`})}}}})(),()=>{t=!0}},[]),t===`running`&&F&&!F.killed?(0,P.jsx)(O,{onDone:e}):null}function O({onDone:e}){p(`remote-control-server-dialog`);let[t,n]=(0,N.useState)(2),s=L.slice(-5);function l(){j(),e(`Remote Control Server stopped.`,{display:`system`})}function u(){j();try{A(),e(`Remote Control Server restarted.`,{display:`system`})}catch(t){e(`Failed to restart: ${d(t)}`,{display:`system`})}}function f(){e(void 0,{display:`skip`})}return o({"select:next":()=>n(e=>(e+1)%3),"select:previous":()=>n(e=>(e-1+3)%3),"select:accept":()=>{t===0?l():t===1?u():f()}},{context:`Select`}),(0,P.jsx)(a,{title:`Remote Control Server`,onCancel:f,hideInputGuide:!0,children:(0,P.jsxs)(c,{flexDirection:`column`,gap:1,children:[(0,P.jsxs)(r,{children:[`Remote Control Server is`,` `,(0,P.jsx)(r,{bold:!0,color:`success`,children:`running`}),F?` (PID: ${F.pid})`:``]}),s.length>0&&(0,P.jsxs)(c,{flexDirection:`column`,children:[(0,P.jsx)(r,{dimColor:!0,children:`Recent logs:`}),s.map((e,t)=>(0,P.jsx)(r,{dimColor:!0,children:e},t))]}),(0,P.jsxs)(c,{flexDirection:`column`,children:[(0,P.jsx)(i,{isFocused:t===0,children:(0,P.jsx)(r,{children:`Stop server`})}),(0,P.jsx)(i,{isFocused:t===1,children:(0,P.jsx)(r,{children:`Restart server`})}),(0,P.jsx)(i,{isFocused:t===2,children:(0,P.jsx)(r,{children:`Continue`})})]}),(0,P.jsx)(r,{dimColor:!0,children:`Enter to select · Esc to continue`})]})})}async function k(){return await _()||(g()?null:b)}function A(){let e=E(`.`),t=w(T([`daemon`,`start`,`--dir=${e}`]),{cwd:e,stdio:[`ignore`,`pipe`,`pipe`],detached:!1});F=t,L=[],t.stdout?.on(`data`,e=>{let t=e.toString().trimEnd().split(`
`);for(let e of t)L.push(e),L.length>R&&L.shift()}),t.stderr?.on(`data`,e=>{let t=e.toString().trimEnd().split(`
`);for(let e of t)L.push(`[err] ${e}`),L.length>R&&L.shift()}),t.on(`exit`,(e,t)=>{F=null,I=`stopped`,L.push(`[daemon] exited (code=${e??`unknown`}, signal=${t})`)}),t.on(`error`,e=>{F=null,I=`error`,L.push(`[daemon] error: ${e.message}`)})}function j(){if(F&&!F.killed){F.kill(`SIGTERM`);let e=F.pid;setTimeout(()=>{try{e&&process.kill(e,0),F&&!F.killed&&F.kill(`SIGKILL`)}catch{}},1e4)}F=null,I=`stopped`}async function M(e,t,n){return(0,P.jsx)(D,{onDone:e})}var N,P,F,I,L,R;e((()=>{n(),N=t(n(),1),v(),h(),y(),x(),S(),f(),s(),m(),C(),u(),P=l(),F=null,I=`stopped`,L=[],R=50}))();export{M as call};