import{r as n,a as c,j as e}from"./index-CO5E0NEG.js";function X({embedded:f=!1}){const[u,C]=n.useState([]),[V,W]=n.useState(!0),[v,l]=n.useState(""),[o,M]=n.useState(!1),[k,R]=n.useState(!1),[b,B]=n.useState(null),[s,E]=n.useState(null),i=n.useRef(null),[p,I]=n.useState(""),[T,D]=n.useState(""),[j,y]=n.useState(null),[P,$]=n.useState(""),[N,H]=n.useState(""),[h,w]=n.useState(null),[m,d]=n.useState(null),x=n.useRef({}),[S,z]=n.useState(""),g=n.useCallback(()=>{c.listSandboxes().then(C).catch(()=>{}).finally(()=>W(!1))},[]);n.useEffect(()=>{g(),c.getContainerStatus().then(t=>B(t.available)).catch(()=>B(!1)),c.getImageStatus("the-companion:latest").then(t=>E(t)).catch(()=>{}),c.getHome().then(({cwd:t})=>z(t)).catch(()=>{})},[g]),n.useEffect(()=>{if(!s||s.status!=="pulling"){i.current&&(clearInterval(i.current),i.current=null);return}return i.current||(i.current=setInterval(()=>{c.getImageStatus("the-companion:latest").then(t=>E(t)).catch(()=>{})},2e3)),()=>{i.current&&(clearInterval(i.current),i.current=null)}},[s]);function L(){c.pullImage("the-companion:latest").then(t=>{t.state&&E(t.state)}).catch(()=>{})}async function A(){const t=p.trim();if(t){R(!0);try{await c.createSandbox(t,{initScript:T||void 0}),I(""),D(""),M(!1),l(""),g()}catch(r){l(r instanceof Error?r.message:String(r))}finally{R(!1)}}}function Z(t){x.current={},y(t.slug),$(t.name),H(t.initScript||""),d(null),l("")}function q(){x.current={},y(null),d(null),w(null),l("")}async function K(){if(j){x.current={},w(null);try{await c.updateSandbox(j,{name:P.trim()||void 0,initScript:N||void 0}),y(null),d(null),l(""),g()}catch(t){l(t instanceof Error?t.message:String(t))}}}async function U(t){try{await c.deleteSandbox(t),j===t&&y(null),g()}catch(r){l(r instanceof Error?r.message:String(r))}}async function F(t){if(!S)return;const r={};x.current=r,w(t),d(null);try{const a=await c.testInitScript(t,S,N);if(x.current!==r)return;d(a)}catch(a){if(x.current!==r)return;d({success:!1,exitCode:-1,output:a instanceof Error?a.message:String(a)})}finally{x.current===r&&w(null)}}const G=b===null?null:b?e.jsx("span",{className:"text-[10px] px-2 py-1 rounded-md bg-green-500/10 text-green-500 font-medium",children:"Docker"}):e.jsx("span",{className:"text-[10px] px-2 py-1 rounded-md bg-amber-500/10 text-amber-500 font-medium",children:"No Docker"});function J(){if(!b)return null;const t=(s==null?void 0:s.status)==="pulling";return e.jsxs("div",{className:"rounded-xl bg-cc-card p-3 sm:p-4 mb-5",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx("span",{className:"text-[11px] font-medium text-cc-muted",children:"Base Image"}),e.jsx("code",{className:"text-[10px] font-mono-code text-cc-fg",children:"the-companion:latest"}),(s==null?void 0:s.status)==="ready"&&e.jsx("span",{className:"text-[10px] px-1.5 py-0.5 rounded bg-green-500/10 text-green-500",children:"Ready"}),(s==null?void 0:s.status)==="pulling"&&e.jsxs("span",{className:"text-[10px] px-1.5 py-0.5 rounded bg-amber-500/10 text-amber-500 flex items-center gap-1",children:[e.jsx("span",{className:"w-2.5 h-2.5 border border-amber-500/30 border-t-amber-500 rounded-full animate-spin"}),"Pulling..."]}),(s==null?void 0:s.status)==="error"&&e.jsx("span",{className:"text-[10px] px-1.5 py-0.5 rounded bg-cc-error/10 text-cc-error",children:"Pull failed"}),(!s||s.status==="idle")&&e.jsx("span",{className:"text-[10px] px-1.5 py-0.5 rounded bg-cc-hover text-cc-muted",children:"Not downloaded"})]}),(s==null?void 0:s.status)!=="ready"&&e.jsx("button",{onClick:L,disabled:t,className:`text-xs px-3 py-2 min-h-[36px] rounded-lg transition-colors shrink-0 ${t?"bg-cc-hover text-cc-muted cursor-not-allowed":"bg-blue-500/10 text-blue-400 hover:bg-blue-500/20 cursor-pointer"}`,children:t?"Pulling...":"Pull"})]}),t&&(s==null?void 0:s.progress)&&s.progress.length>0&&e.jsx("pre",{className:"mt-2 px-3 py-2 text-[10px] font-mono-code bg-cc-code-bg rounded-lg text-cc-muted max-h-[120px] overflow-auto whitespace-pre-wrap",children:s.progress.slice(-20).join(`
`)}),(s==null?void 0:s.status)==="error"&&s.error&&e.jsx("p",{className:"mt-2 text-[10px] text-cc-error",children:s.error})]})}return f?e.jsx("div",{className:"h-full bg-cc-bg text-cc-fg font-sans-ui antialiased overflow-y-auto overflow-x-hidden",children:e.jsxs("div",{className:"max-w-2xl mx-auto px-4 sm:px-6 py-6 sm:py-10 pb-safe",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3 mb-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("h1",{className:"text-lg font-semibold text-cc-fg",children:"Sandboxes"}),e.jsx("p",{className:"mt-0.5 text-[13px] text-cc-muted leading-relaxed",children:"Reusable sandbox configurations for containerized sessions."})]}),e.jsx("div",{className:"flex items-center gap-2 shrink-0",children:G})]}),J(),e.jsxs("div",{className:"flex items-center gap-2 mt-4 mb-5",children:[e.jsx("div",{className:"flex-1"}),e.jsxs("button",{onClick:()=>M(!o),className:`flex items-center gap-1.5 px-3.5 py-2.5 min-h-[44px] rounded-lg text-sm font-medium transition-colors cursor-pointer shrink-0 ${o?"bg-cc-active text-cc-fg":"bg-cc-primary hover:bg-cc-primary-hover text-white"}`,children:[e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",className:"w-4 h-4",children:o?e.jsx("path",{d:"M18 6 6 18M6 6l12 12"}):e.jsx("path",{d:"M12 5v14M5 12h14"})}),e.jsx("span",{className:"hidden sm:inline",children:o?"Cancel":"New Sandbox"})]})]}),o&&e.jsxs("div",{className:"mb-6 rounded-xl bg-cc-card p-4 sm:p-5 space-y-3",style:{animation:"fadeSlideIn 150ms ease-out"},children:[e.jsx("input",{type:"text",value:p,onChange:t=>I(t.target.value),placeholder:"Sandbox name (e.g. node-project)",className:"w-full px-3 py-2.5 min-h-[44px] text-sm bg-cc-bg rounded-lg text-cc-fg placeholder:text-cc-muted focus:outline-none focus:ring-1 focus:ring-cc-primary/40 transition-shadow",onKeyDown:t=>{t.key==="Enter"&&p.trim()&&A()}}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-[11px] font-medium text-cc-muted mb-1",children:"Init Script (optional)"}),e.jsx("textarea",{value:T,onChange:t=>D(t.target.value),placeholder:`# Runs inside the container before Claude starts
# Example:
bun install
pip install -r requirements.txt`,rows:5,className:"w-full px-3 py-2.5 text-[11px] font-mono-code bg-cc-bg rounded-lg text-cc-fg placeholder:text-cc-muted focus:outline-none focus:ring-1 focus:ring-cc-primary/40 resize-y transition-shadow",style:{minHeight:"80px"}})]}),v&&o&&e.jsx("div",{className:"px-3 py-2 rounded-lg bg-cc-error/10 text-xs text-cc-error",children:v}),e.jsxs("div",{className:"flex items-center justify-between pt-1",children:[e.jsxs("p",{className:"text-[11px] text-cc-muted",children:["Stored in ",e.jsx("code",{className:"text-[10px]",children:"~/.companion/sandboxes/"})]}),e.jsx("button",{onClick:A,disabled:!p.trim()||k,className:`px-4 py-2.5 min-h-[44px] rounded-lg text-sm font-medium transition-colors ${p.trim()&&!k?"bg-cc-primary hover:bg-cc-primary-hover text-white cursor-pointer":"bg-cc-hover text-cc-muted cursor-not-allowed"}`,children:k?"Creating...":"Create"})]})]}),e.jsx("div",{className:"flex items-center gap-2 mb-3 text-[12px] text-cc-muted",children:e.jsxs("span",{children:[u.length," sandbox",u.length!==1?"es":""]})}),V?e.jsx("div",{className:"py-12 text-center text-sm text-cc-muted",children:"Loading sandboxes..."}):u.length===0?e.jsx("div",{className:"py-12 text-center text-sm text-cc-muted",children:"No sandboxes yet."}):e.jsx("div",{className:"space-y-1",children:u.map(t=>j===t.slug?e.jsxs("div",{className:"rounded-xl bg-cc-card p-4 space-y-3",style:{animation:"fadeSlideIn 150ms ease-out"},children:[e.jsx("input",{type:"text",value:P,onChange:a=>$(a.target.value),placeholder:"Sandbox name",className:"w-full px-3 py-2.5 min-h-[44px] text-sm bg-cc-bg rounded-lg text-cc-fg focus:outline-none focus:ring-1 focus:ring-cc-primary/40 transition-shadow"}),e.jsxs("div",{children:[e.jsx("div",{className:"flex items-center justify-between mb-1",children:e.jsx("div",{className:"text-[11px] font-medium text-cc-muted",children:"Init Script (optional)"})}),e.jsx("textarea",{value:N,onChange:a=>{H(a.target.value),d(null)},placeholder:`# Runs inside the container before Claude starts
# Example:
bun install
pip install -r requirements.txt`,rows:5,className:"w-full px-3 py-2.5 text-[11px] font-mono-code bg-cc-bg rounded-lg text-cc-fg placeholder:text-cc-muted focus:outline-none focus:ring-1 focus:ring-cc-primary/40 resize-y transition-shadow",style:{minHeight:"80px"}})]}),h===t.slug&&e.jsxs("div",{className:"flex items-center gap-2 px-3 py-2 rounded-lg bg-amber-500/10 text-xs text-amber-500",children:[e.jsx("span",{className:"w-3 h-3 border-2 border-amber-500/30 border-t-amber-500 rounded-full animate-spin"}),"Testing init script..."]}),m&&h!==t.slug&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("div",{className:`flex items-center gap-2 px-3 py-2 rounded-lg text-xs ${m.success?"bg-green-500/10 text-green-500":"bg-cc-error/10 text-cc-error"}`,children:m.success?"Test passed":`Test failed (exit ${m.exitCode})`}),m.output&&e.jsx("pre",{className:"px-3 py-2 text-[10px] font-mono-code bg-cc-code-bg rounded-lg text-cc-muted max-h-[200px] overflow-auto whitespace-pre-wrap",children:m.output})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[N.trim()&&b&&e.jsx("button",{onClick:()=>void F(t.slug),disabled:h===t.slug||!S,className:`px-3 py-2.5 min-h-[44px] text-sm rounded-lg font-medium transition-colors ${h===t.slug||!S?"bg-cc-hover text-cc-muted cursor-not-allowed":"bg-blue-500/10 text-blue-400 hover:bg-blue-500/20 cursor-pointer"}`,children:h===t.slug?"Testing...":"Test Init Script"}),e.jsx("button",{onClick:q,className:"px-3 py-2.5 min-h-[44px] text-sm rounded-lg text-cc-muted hover:text-cc-fg hover:bg-cc-hover transition-colors cursor-pointer",children:"Cancel"}),e.jsx("button",{onClick:()=>void K(),className:"px-4 py-2.5 min-h-[44px] text-sm rounded-lg font-medium bg-cc-primary hover:bg-cc-primary-hover text-white transition-colors cursor-pointer",children:"Save"})]})]},t.slug):e.jsx(O,{sandbox:t,onStartEdit:()=>Z(t),onDelete:()=>void U(t.slug)},t.slug))}),v&&!o&&e.jsx("div",{className:"mt-4 px-3 py-2 rounded-lg bg-cc-error/10 text-xs text-cc-error",children:v})]})}):e.jsx("div",{className:"h-full bg-cc-bg text-cc-fg font-sans-ui antialiased overflow-y-auto overflow-x-hidden",children:e.jsx("div",{className:"max-w-2xl mx-auto px-4 sm:px-6 py-6 sm:py-10 pb-safe",children:e.jsx("div",{className:"py-12 text-center text-sm text-cc-muted",children:"Use embedded mode to view sandboxes."})})})}function O({sandbox:f,onStartEdit:u,onDelete:C}){return e.jsxs("div",{className:"group flex items-start gap-3 px-3 py-3 min-h-[44px] rounded-lg hover:bg-cc-hover/60 transition-colors",children:[e.jsx("div",{className:"shrink-0 mt-0.5 w-7 h-7 rounded-md bg-cc-primary/10 flex items-center justify-center",children:e.jsxs("svg",{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.5",className:"w-3.5 h-3.5 text-cc-primary",children:[e.jsx("path",{d:"M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z"}),e.jsx("path",{d:"M3.27 6.96 12 12.01l8.73-5.05M12 22.08V12"})]})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsx("span",{className:"text-sm font-medium text-cc-fg truncate",children:f.name})}),e.jsx("p",{className:"mt-0.5 text-xs text-cc-muted",children:f.initScript?"init script":"no init script"})]}),e.jsxs("div",{className:"shrink-0 flex items-center gap-0.5 opacity-100 sm:opacity-0 sm:group-hover:opacity-100 transition-opacity",children:[e.jsx("button",{onClick:u,className:"p-2 min-h-[44px] min-w-[44px] sm:min-h-0 sm:min-w-0 sm:p-1.5 rounded-md text-cc-muted hover:text-cc-fg hover:bg-cc-active transition-colors cursor-pointer","aria-label":"Edit",children:e.jsxs("svg",{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.5",className:"w-3.5 h-3.5",children:[e.jsx("path",{d:"M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"}),e.jsx("path",{d:"M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5Z"})]})}),e.jsx("button",{onClick:C,className:"p-2 min-h-[44px] min-w-[44px] sm:min-h-0 sm:min-w-0 sm:p-1.5 rounded-md text-cc-muted hover:text-cc-error hover:bg-cc-error/10 transition-colors cursor-pointer","aria-label":"Delete",children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.5",className:"w-3.5 h-3.5",children:e.jsx("path",{d:"M3 6h18M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2m3 0v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6h14Z"})})})]})]})}export{X as SandboxManager};
