import{r as s,j as c}from"./vendor-markdown-CkTmV-He.js";import{D as P,o as F}from"./vendor-xterm-D1D2FVe3.js";import{y as L,u as T,a as E,h as I}from"./index-CKH_4wKU.js";function W(u,n){const d=`${window.location.protocol==="https:"?"wss:":"ws:"}//${window.location.host}/ws/terminal/${u}`,t=new WebSocket(d,L());return t.binaryType="arraybuffer",t.onopen=()=>{var e;(e=n.onOpen)==null||e.call(n)},t.onmessage=e=>{if(e.data instanceof ArrayBuffer)n.onData(new Uint8Array(e.data));else try{const a=JSON.parse(e.data);a.type==="exit"&&n.onExit(a.exitCode)}catch{}},t.onerror=()=>{var e;(e=n.onError)==null||e.call(n,"Terminal WebSocket connection error")},t.onclose=e=>{var a;e.wasClean||(a=n.onError)==null||a.call(n,"Terminal connection lost")},{sendInput(e){t.readyState===WebSocket.OPEN&&t.send(JSON.stringify({type:"input",data:e}))},sendResize(e,a){t.readyState===WebSocket.OPEN&&t.send(JSON.stringify({type:"resize",cols:e,rows:a}))},disconnect(){t.close()}}}const V=[{label:"Esc",data:"\x1B"},{label:"Tab",data:"	"},{label:"^C",data:""},{label:"^D",data:""},{label:"^Z",data:""},{label:"^L",data:"\f"},{label:"↑",data:"\x1B[A"},{label:"↓",data:"\x1B[B"},{label:"←",data:"\x1B[D"},{label:"→",data:"\x1B[C"},{label:"|",data:"|"},{label:"~",data:"~"},{label:"-",data:"-"},{label:"/",data:"/"}];function J(){return s.useSyncExternalStore(()=>()=>{},()=>("ontouchstart"in window||navigator.maxTouchPoints>0)&&window.innerWidth<1024,()=>!1)}function K(){const[u,n]=s.useState(0);return s.useEffect(()=>{const h=window.visualViewport;if(!h)return;const d=()=>{const t=Math.max(0,window.innerHeight-h.height);n(t)};return h.addEventListener("resize",d),h.addEventListener("scroll",d),d(),()=>{h.removeEventListener("resize",d),h.removeEventListener("scroll",d)}},[]),u}function U({onWrite:u,onPaste:n}){const h=J(),d=K(),[t,e]=s.useState(!1),a=s.useRef(void 0),w=s.useCallback(o=>{if(t&&o.length===1){const f=o.toUpperCase().charCodeAt(0);if(f>=65&&f<=90){u(String.fromCharCode(f-64)),e(!1),clearTimeout(a.current);return}}u(o),t&&(e(!1),clearTimeout(a.current))},[t,u]),x=s.useCallback(()=>{e(o=>(clearTimeout(a.current),o||(a.current=setTimeout(()=>e(!1),3e3)),!o))},[]);return!h||d===0?null:c.jsxs("div",{className:"fixed left-0 right-0 z-50 flex items-center gap-1 px-2 py-1.5 overflow-x-auto border-t border-cc-border bg-cc-card",style:{bottom:d},onMouseDown:o=>o.preventDefault(),onTouchStart:o=>o.preventDefault(),children:[c.jsx("button",{type:"button",className:`shrink-0 h-7 min-w-9 px-2 rounded text-[11px] font-semibold font-mono-code transition-colors ${t?"bg-cc-primary text-white":"bg-cc-hover text-cc-fg"}`,onClick:x,children:"Ctrl"}),V.map(o=>c.jsx("button",{type:"button",className:"shrink-0 h-7 min-w-8 px-2 rounded bg-cc-hover text-cc-fg text-[11px] font-semibold font-mono-code active:bg-cc-active transition-colors",onClick:()=>w(o.data),children:o.label},o.label)),n&&c.jsx("button",{type:"button",className:"shrink-0 h-7 min-w-9 px-2 rounded bg-cc-hover text-cc-fg text-[11px] font-semibold font-mono-code active:bg-cc-active transition-colors",onClick:n,title:"Paste from clipboard",children:"Paste"})]})}function R(u){return{background:u?"#141413":"#1e1e1e",foreground:"#d4d4d4",cursor:"#d4d4d4",selectionBackground:"rgba(255, 255, 255, 0.2)"}}function _({cwd:u,containerId:n,title:h,onClose:d,embedded:t=!1,visible:e=!0,hideHeader:a=!1}){const w=s.useRef(null),x=s.useRef(null),o=s.useRef(null),f=s.useRef(null),g=s.useRef(null),b=s.useRef(null),y=T(i=>i.darkMode);s.useEffect(()=>{if(!w.current)return;let i=!1;const p="'MesloLGS Nerd Font Mono', 'SF Mono', 'Monaco', 'Menlo', 'Courier New', monospace",S=window.innerWidth<640?11:13;function M(){if(i||!w.current)return;const r=new P({cursorBlink:!0,fontSize:S,fontFamily:p,theme:R(T.getState().darkMode)}),v=new F;r.loadAddon(v),r.open(w.current),x.current=r,o.current=v,E.spawnTerminal(u,r.cols,r.rows,{containerId:n}).then(({terminalId:m})=>{i||(T.getState().setTerminalId(m),g.current=m,f.current=W(m,{onData:l=>r.write(l),onExit:l=>{r.writeln(`\r
[Process exited with code ${l}]`)},onError:l=>{r.writeln(`\r
[${l}]`)},onOpen:()=>{var l;v.fit(),(l=f.current)==null||l.sendResize(r.cols,r.rows)}}))}).catch(m=>{i||r.writeln(`\r
[Failed to start terminal: ${m.message}]`)});const D=r.onData(m=>{var l;return(l=f.current)==null?void 0:l.sendInput(m)}),$=w.current,j=new ResizeObserver(()=>{var m;v.fit(),(m=f.current)==null||m.sendResize(r.cols,r.rows)});j.observe($),b.current=()=>{var l;j.disconnect(),D.dispose(),(l=f.current)==null||l.disconnect(),f.current=null,r.dispose(),x.current=null,o.current=null;const m=g.current;g.current=null,m&&E.killTerminal(m).catch(A=>I(A,"kill terminal"))}}const O=document.fonts.load(`${S}px ${p.split(",")[0]}`),B=new Promise(r=>setTimeout(r,2e3));return Promise.race([O,B]).then(M),()=>{var r;i=!0,(r=b.current)==null||r.call(b),b.current=null}},[u,n]),s.useEffect(()=>{x.current&&(x.current.options.theme=R(y))},[y]),s.useEffect(()=>{var i;e&&(!o.current||!x.current||(o.current.fit(),(i=f.current)==null||i.sendResize(x.current.cols,x.current.rows)))},[e]);const k=s.useCallback(i=>{var p;(p=f.current)==null||p.sendInput(i)},[]),z=s.useCallback(async()=>{var i;try{const p=await navigator.clipboard.readText();p&&((i=f.current)==null||i.sendInput(p))}catch{}},[]),C=c.jsxs("div",{className:`flex flex-col shadow-2xl overflow-hidden border border-cc-border ${t?"h-full":"w-[90vw] max-w-4xl h-[70vh]"}`,style:{background:y?"#141413":"#1e1e1e"},onClick:i=>i.stopPropagation(),children:[!a&&c.jsxs("div",{className:"flex items-center justify-between px-4 py-2 border-b border-cc-border bg-cc-sidebar shrink-0",children:[c.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[c.jsx("svg",{viewBox:"0 0 16 16",fill:"currentColor",className:"w-4 h-4 text-cc-muted shrink-0",children:c.jsx("path",{d:"M2 3a1 1 0 011-1h10a1 1 0 011 1v10a1 1 0 01-1 1H3a1 1 0 01-1-1V3zm2 1.5l3 2.5-3 2.5V4.5zM8.5 10h3v1h-3v-1z"})}),c.jsx("span",{className:"text-xs text-cc-muted font-mono-code truncate",children:h||u})]}),d&&c.jsx("button",{onClick:d,className:"w-6 h-6 flex items-center justify-center rounded-md text-cc-muted hover:text-cc-fg hover:bg-cc-hover transition-colors cursor-pointer shrink-0",children:c.jsx("svg",{viewBox:"0 0 16 16",fill:"none",stroke:"currentColor",strokeWidth:"2",className:"w-3.5 h-3.5",children:c.jsx("path",{d:"M4 4l8 8M12 4l-8 8",strokeLinecap:"round"})})})]}),c.jsx("div",{ref:w,className:`flex-1 min-h-0 ${t?"":"p-1"}`})]}),N=c.jsx(U,{onWrite:k,onPaste:z});return t?c.jsxs("div",{className:`h-full ${e?"":"hidden"}`,children:[C,N]}):c.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/50",children:[C,N]})}export{_ as T};
