import{n as e,o as t,r as n,t as r}from"./jsx-runtime-chunk.js";/* empty css           */import{t as i}from"./xterm-chunk2.js";import{n as a}from"./addon-fit-chunk.js";var o=t(n(),1),s=t(e(),1),c=r(),l=location.pathname.replace(/^\/+/,``).split(`/`),u=decodeURIComponent(l[1]??``),d=l[2]??`0`;function f(){let[e,t]=(0,o.useState)(u),[n,r]=(0,o.useState)(d),[s,l]=(0,o.useState)(`connecting`),[f,p]=(0,o.useState)(`Connecting…`),[m,h]=(0,o.useState)(!1),g=(0,o.useRef)(null),_=(0,o.useRef)(``);(0,o.useEffect)(()=>{let e=new URLSearchParams(location.search).get(`token`);if(e){_.current=e,h(!0),history.replaceState(null,``,location.pathname);return}let t=e=>{l(`disconnected`),p(e)};if(!window.opener){t(`Must be opened from VirtualProg dashboard`);return}let n=e=>{if(e.origin!==location.origin||e.data?.type!==`VP_CONSOLE_TOKEN`)return;let i=e.data.token;if(!i){t(`Invalid session token received`);return}_.current=i,h(!0),window.removeEventListener(`message`,n),clearTimeout(r)};window.addEventListener(`message`,n),window.opener.postMessage({type:`VP_CONSOLE_READY`},location.origin);let r=setTimeout(()=>{window.removeEventListener(`message`,n),t(`Timed out waiting for session token`)},8e3);return()=>{window.removeEventListener(`message`,n),clearTimeout(r)}},[]);let v=(0,o.useEffectEvent)(()=>{let o=`/ws/console/${encodeURIComponent(u)}/${d}?token=${encodeURIComponent(_.current)}`,s={};(async()=>{try{let e=await fetch(`/api/console-config?name=${encodeURIComponent(u)}&port=${d}&token=${encodeURIComponent(_.current)}`);if(e.ok){let n=await e.json();t(n.vmName??u),r(String(n.portIndex??d)),s=n.xtermTheme??{},n.wsPath&&(o=n.wsPath);let i=document.documentElement.style;i.setProperty(`--con-bg`,n.bgHex??`#1e1e1e`),i.setProperty(`--con-toolbar-bg`,n.toolbarBg??`#111111`),i.setProperty(`--con-toolbar-border`,n.toolbarBorder??`#333333`),i.setProperty(`--con-title-color`,n.titleColor??`#aaaaaa`),i.setProperty(`--con-title-strong`,n.titleStrong??`#f0f0f0`)}}catch{}if(document.title=`Console — ${e}`,!g.current)return;let c=new i({cursorBlink:!0,fontFamily:`Menlo, "Courier New", monospace`,fontSize:14,scrollback:5e3,theme:s}),f=new a;c.loadAddon(f),c.open(g.current),f.fit(),new ResizeObserver(()=>f.fit()).observe(g.current);let m=(location.protocol===`https:`?`wss:`:`ws:`)+`//`+location.host+o,h=null,v=null,y=!1,b=()=>{y=!1,l(`connecting`),p(`Connecting…`),h=new WebSocket(m),h.binaryType=`arraybuffer`,h.onopen=()=>{l(`connected`),p(`Connected`),c.write(`\r\n\x1b[32m[✔ Connected to ${e} serial port ${n}]\x1b[0m\r\n`)},h.onmessage=e=>{typeof e.data==`string`?c.write(e.data):c.write(new Uint8Array(e.data))},h.onclose=e=>{e.code===1e3||y?(l(`disconnected`),p(`VM stopped`),c.write(`\r
\x1B[33m[Connection closed — VM stopped]\x1B[0m\r
`)):(l(`disconnected`),p(`Disconnected`),c.write(`\r
\x1B[31m[Disconnected — reconnecting in 3s…]\x1B[0m\r
`),v=setTimeout(b,3e3))},h.onerror=()=>{l(`disconnected`),p(`Error`)}};c.onData(e=>{h?.readyState===WebSocket.OPEN&&h.send(e)}),window.addEventListener(`beforeunload`,()=>{y=!0,v!==null&&clearTimeout(v),h?.close()}),b()})()});return(0,o.useEffect)(()=>{m&&v()},[m]),(0,c.jsxs)(`div`,{className:`con-root`,children:[(0,c.jsxs)(`div`,{id:`con-toolbar`,children:[(0,c.jsxs)(`span`,{className:`con-title`,id:`con-title`,children:[`VirtualProg Console — `,(0,c.jsx)(`strong`,{children:e}),`\xA0[port `,n,`]`]}),(0,c.jsx)(`span`,{id:`con-status`,className:s===`connected`?`connected`:s===`connecting`?`connecting`:`disconnected`,children:f})]}),(0,c.jsx)(`div`,{id:`con-terminal`,ref:g})]})}var p=document.getElementById(`root`);p&&s.createRoot(p).render((0,c.jsx)(f,{}));