import{o as e}from"./rolldown-runtime-FhOqtrmT.js";import{b as t,x as n}from"./vendor-markdown-0Mxgxy0L.js";import"./vendor-ui-UXCWAcmi.js";import{t as r}from"./wifi-cAuzu8sM.js";import{t as i}from"./api-client-bbJLzRVE.js";import{J as a,Tt as o,gt as s,jt as c,mt as l,nt as u,wt as d}from"./index-48KAFlD7.js";var f=e(n(),1),p=t();function m(){let[e,t]=(0,f.useState)(``),[n,m]=(0,f.useState)([]),[h,g]=(0,f.useState)(!1),[_,v]=(0,f.useState)(null),[y,b]=(0,f.useState)(null),x=(0,f.useCallback)(async()=>{try{m(await i.get(`/api/preview/tunnels`))}catch(e){console.warn(`[ports] failed to fetch tunnels`,e)}},[]);(0,f.useEffect)(()=>{x();let e=setInterval(x,1e4);return()=>clearInterval(e)},[x]);let S=async e=>{let r=n.find(t=>t.port===e);if(r){window.open(r.url,`_blank`),t(``);return}g(!0),v(null);try{let n=await i.post(`/api/preview/tunnel`,{port:e});window.open(n.url,`_blank`),t(``),await x()}catch(t){v(t.message||`Failed to start tunnel for port ${e}`)}finally{g(!1)}},C=async e=>{try{await i.del(`/api/preview/tunnel/${e}`),await x()}catch(t){a.error(t.message||`Failed to stop tunnel for port ${e}`)}},w=(e,t)=>{navigator.clipboard.writeText(t).then(()=>{b(e),a.success(`URL copied`),setTimeout(()=>b(null),2e3)}).catch(()=>{a.error(`Failed to copy URL`)})};return(0,p.jsxs)(`div`,{className:`flex flex-col h-full w-full bg-background`,children:[(0,p.jsxs)(`div`,{className:`p-4 md:p-6 border-b border-border bg-surface`,children:[(0,p.jsxs)(`div`,{className:`flex items-center gap-2 mb-3`,children:[(0,p.jsx)(r,{className:`size-5 text-primary`}),(0,p.jsx)(`h2`,{className:`text-base font-medium text-text-primary`,children:`Port Forwarding`})]}),(0,p.jsxs)(`form`,{onSubmit:t=>{t.preventDefault();let n=parseInt(e,10);n>=1&&n<=65535?S(n):v(`Port must be 1-65535`)},className:`flex items-center gap-2`,children:[(0,p.jsxs)(`div`,{className:`flex-1 flex items-center gap-2 px-3 py-2.5 rounded-lg bg-background border border-border focus-within:border-primary/50 transition-colors`,children:[(0,p.jsx)(`span`,{className:`text-sm text-text-subtle shrink-0`,children:`localhost:`}),(0,p.jsx)(`input`,{type:`number`,value:e,onChange:e=>{t(e.target.value),v(null)},placeholder:`3000`,min:1,max:65535,className:`flex-1 bg-transparent text-sm text-text-primary outline-none placeholder:text-text-subtle min-w-0 [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none`})]}),(0,p.jsx)(`button`,{type:`submit`,disabled:h||!e,className:`px-4 py-2.5 rounded-lg bg-primary text-primary-foreground text-sm font-medium hover:bg-primary/90 disabled:opacity-50 transition-colors shrink-0 min-w-[72px] flex items-center justify-center`,children:h?(0,p.jsx)(l,{className:`size-4 animate-spin`}):`Forward`})]}),_&&(0,p.jsx)(`p`,{className:`text-sm text-red-400 mt-2`,children:_}),h&&(0,p.jsxs)(`div`,{className:`flex items-center gap-2 text-sm text-text-secondary mt-2`,children:[(0,p.jsx)(l,{className:`size-3.5 animate-spin`}),(0,p.jsx)(`span`,{children:`Starting tunnel...`})]})]}),(0,p.jsx)(`div`,{className:`flex-1 overflow-y-auto p-4 md:p-6`,children:n.length===0?(0,p.jsxs)(`div`,{className:`flex flex-col items-center justify-center h-full gap-3 text-center`,children:[(0,p.jsx)(s,{className:`size-10 text-text-subtle`}),(0,p.jsx)(`p`,{className:`text-sm text-text-secondary max-w-xs`,children:`No active ports. Forward a port to access your local dev server from anywhere.`})]}):(0,p.jsx)(`div`,{className:`space-y-2`,children:n.map(e=>(0,p.jsxs)(`div`,{className:`flex items-center gap-3 p-3 rounded-lg bg-surface border border-border`,children:[(0,p.jsxs)(`div`,{className:`shrink-0 px-2 py-1 rounded bg-primary/10 text-primary text-xs font-mono font-medium`,children:[`:`,e.port]}),(0,p.jsx)(`span`,{className:`flex-1 text-xs text-text-secondary truncate min-w-0`,children:e.url}),(0,p.jsxs)(`div`,{className:`flex items-center shrink-0`,children:[(0,p.jsx)(`button`,{onClick:()=>window.open(e.url,`_blank`),className:`p-2.5 rounded-md hover:bg-surface-elevated transition-colors`,title:`Open in browser`,children:(0,p.jsx)(d,{className:`size-4 text-text-secondary`})}),(0,p.jsx)(`button`,{onClick:()=>w(e.port,e.url),className:`p-2.5 rounded-md hover:bg-surface-elevated transition-colors`,title:`Copy URL`,children:y===e.port?(0,p.jsx)(c,{className:`size-4 text-green-400`}):(0,p.jsx)(o,{className:`size-4 text-text-secondary`})}),(0,p.jsx)(`button`,{onClick:()=>C(e.port),className:`p-2.5 rounded-md hover:bg-red-500/10 transition-colors`,title:`Stop tunnel`,children:(0,p.jsx)(u,{className:`size-4 text-red-400`})})]})]},e.port))})})]})}export{m as PortForwardingTab};