const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["../assets/xterm.DFuMZ0ql.css"])))=>i.map(i=>d[i]);
import{_ as b}from"./PPVm8Dsz.js";async function N(e){const{Terminal:p}=await b(async()=>{const{Terminal:r}=await import("./BTGVxaYV.js").then(i=>i.x);return{Terminal:r}},[],import.meta.url),{FitAddon:_}=await b(async()=>{const{FitAddon:r}=await import("./BlxrFPDK.js").then(i=>i.a);return{FitAddon:r}},[],import.meta.url),{WebLinksAddon:k}=await b(async()=>{const{WebLinksAddon:r}=await import("./BjXFD2Pv.js").then(i=>i.a);return{WebLinksAddon:r}},[],import.meta.url);await b(()=>Promise.resolve({}),__vite__mapDeps([0]),import.meta.url);const d=new _,n=new p({allowTransparency:!0,cursorBlink:!0,cursorStyle:"block",fontFamily:"'SF Mono', 'Fira Code', 'Cascadia Code', 'Menlo', monospace",fontSize:e.fontSize||14,theme:{background:"#0a0a0f",black:"#0a0a0f",blue:"#3b82f6",brightBlack:"#64748b",brightBlue:"#60a5fa",brightCyan:"#67e8f9",brightGreen:"#4ade80",brightMagenta:"#c4b5fd",brightRed:"#f87171",brightWhite:"#f8fafc",brightYellow:"#fbbf24",cursor:"#e2e8f0",cursorAccent:"#0a0a0f",cyan:"#38bdf8",foreground:"#e2e8f0",green:"#22c55e",magenta:"#a78bfa",red:"#ef4444",selectionBackground:"rgba(99, 102, 241, 0.3)",white:"#e2e8f0",yellow:"#f59e0b"}});n.loadAddon(d),n.loadAddon(new k),n.open(e.container),e.readOnly&&e.initialCols&&e.initialRows?n.resize(e.initialCols,e.initialRows):d.fit();const v=new Set(["f","g","h","j","n","o","p","q","s","t","u","w"]);n.attachCustomKeyEventHandler(r=>{const i=r.key.toLowerCase();return r.metaKey?i==="c"||i==="v"?!0:!v.has(i):!0});let u=null;if(e.terminalId&&e.apiKey&&!e.readOnly){const r=e.terminalId,i=e.apiKey;u=l=>{(async()=>{try{if(!l.clipboardData)return;const y=Array.from(l.clipboardData.items).find(w=>w.type.startsWith("image/"));if(!y)return;const O=y.getAsFile();if(!O)return;l.preventDefault();const m=new FileReader,T=await new Promise((w,P)=>{m.onload=()=>{w(m.result)},m.onerror=P,m.readAsDataURL(O)});(await fetch(`/api/terminals/${r}/paste-image`,{body:JSON.stringify({image:T}),headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json"},method:"POST"})).ok&&a?.readyState===WebSocket.OPEN&&a.send(JSON.stringify({data:"",type:"input"}))}catch{}})()},e.container.addEventListener("paste",u)}let a=null,s=null,f=1e3,o=!1,c=0;function g(){if(o)return;s&&clearTimeout(s);const r=Math.random()*f*.5;s=setTimeout(()=>{f=Math.min(f*2,3e4),h()},f+r)}async function h(){if(o)return;let r;try{r=await e.getTicket()}catch{o||(e.onDisconnect?.(),g());return}if(o)return;const i=c>0?`&lastSeq=${String(c)}`:"";a=new WebSocket(`${e.wsUrl}?ticket=${r}&caps=snapshot${i}`),a.onopen=()=>{f=1e3,e.onReconnect?.()},a.onmessage=l=>{const t=JSON.parse(l.data);if(t.type==="output"){if(typeof t.seq=="number"){if(t.seq<=c)return;c=t.seq}n.write(t.data??"")}else if(t.type==="snapshot")n.reset(),typeof t.seq=="number"&&(c=t.seq),n.write(t.data??"");else if(t.type==="scrollback")n.write(t.data??"");else if(t.type==="exit")n.write(`\r
\x1B[90m[Process exited with code ${String(t.code)}]\x1B[0m\r
`),o=!0,s&&clearTimeout(s),e.onExit?.(t.code??0);else if(t.type==="output-dropped"){const y=typeof t.bytes=="number"&&t.bytes>0?`[${String(t.bytes)} bytes dropped — resyncing…]`:"[resyncing…]";n.write(`\r
\x1B[33m${y}\x1B[0m\r
`)}else t.type==="activity"?e.onActivity?.(t.active??!1):t.type==="cwd"?e.onCwd?.(t.path??""):t.type==="resize"&&t.cols&&t.rows&&n.resize(t.cols,t.rows)},a.onclose=()=>{o||(e.onDisconnect?.(),g())}}h(),n.onData(r=>{e.readOnly||a?.readyState===WebSocket.OPEN&&a.send(JSON.stringify({data:r,type:"input"}))});const S=new ResizeObserver(()=>{e.readOnly||!e.container.offsetWidth||!e.container.offsetHeight||(d.fit(),a?.readyState===WebSocket.OPEN&&a.send(JSON.stringify({cols:n.cols,rows:n.rows,type:"resize"})))});S.observe(e.container);function A(){o=!0,s&&clearTimeout(s),u&&e.container.removeEventListener("paste",u),S.disconnect(),a?.close(),n.dispose()}function E(r){a?.readyState===WebSocket.OPEN&&a.send(JSON.stringify({data:r,type:"input"}))}return{dispose:A,fitAddon:d,getLastSeq:()=>c,sendInput:E,term:n}}function R(e,p){e.readyState===WebSocket.OPEN&&e.send(JSON.stringify({signal:p,type:"signal"}))}export{N as createTerminal,R as sendSignal};
