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"./api-client-bbJLzRVE.js";import{n as r}from"./settings-store-B5e8CNie.js";import"./vendor-mermaid-v5g7E-EQ.js";import{n as i}from"./utils-B1vxU5xS.js";import{t as a}from"./panel-store-BETELS-N.js";import{Dt as o,J as s,Tt as c,ft as l,ot as u}from"./index-48KAFlD7.js";import{n as d,r as f,t as p}from"./vendor-xterm-vh96p1Au.js";var m=e(n(),1),h={background:`#0f1419`,foreground:`#e5e7eb`,cursor:`#e5e7eb`,selectionBackground:`#3b82f640`,black:`#1a1f2e`,red:`#ef4444`,green:`#10b981`,yellow:`#f59e0b`,blue:`#3b82f6`,magenta:`#a855f7`,cyan:`#06b6d4`,white:`#e5e7eb`,brightBlack:`#6b7280`,brightRed:`#f87171`,brightGreen:`#34d399`,brightYellow:`#fbbf24`,brightBlue:`#60a5fa`,brightMagenta:`#c084fc`,brightCyan:`#22d3ee`,brightWhite:`#f9fafb`},g={background:`#ffffff`,foreground:`#1a1f2e`,cursor:`#1a1f2e`,selectionBackground:`#2563eb30`,black:`#1a1f2e`,red:`#dc2626`,green:`#059669`,yellow:`#d97706`,blue:`#2563eb`,magenta:`#9333ea`,cyan:`#0891b2`,white:`#f8fafc`,brightBlack:`#64748b`,brightRed:`#ef4444`,brightGreen:`#10b981`,brightYellow:`#f59e0b`,brightBlue:`#3b82f6`,brightMagenta:`#a855f7`,brightCyan:`#06b6d4`,brightWhite:`#ffffff`};function _(e){return e===`system`?window.matchMedia(`(prefers-color-scheme: dark)`).matches?h:g:e===`light`?g:h}var v=`RESIZE:`;function y(e){let{sessionId:t,containerRef:n}=e,i=(0,m.useRef)(null),a=(0,m.useRef)(null),o=(0,m.useRef)(null),s=(0,m.useRef)(null),c=(0,m.useRef)(0),[l,u]=(0,m.useState)(!1),[h,g]=(0,m.useState)(!1),[y,b]=(0,m.useState)(!1),x=e.tabId?`ppm:terminal-session:${e.tabId}`:null,S=(0,m.useRef)((()=>{if(x)try{return localStorage.getItem(x)??t}catch{}return t})()),C=(0,m.useRef)(0),w=(0,m.useCallback)(e=>{let t=o.current;t?.readyState===WebSocket.OPEN&&t.send(e)},[]),T=(0,m.useCallback)(()=>i.current?.getSelection()??``,[]),E=(0,m.useCallback)(()=>{let e=i.current;if(!e)return``;let t=e.buffer.active,n=C.current,r=t.baseY+t.cursorY,a=[];for(let e=n;e<=r;e++){let n=t.getLine(e);n&&a.push(n.translateToString(!0))}for(;a.length>0&&a[a.length-1].trim()===``;)a.pop();return a.join(`
`)},[]),D=(0,m.useCallback)(()=>{let e=i.current,t=o.current;e&&t?.readyState===WebSocket.OPEN&&t.send(`${v}${e.cols},${e.rows}`)},[]),O=(0,m.useCallback)(()=>{if(s.current&&clearTimeout(s.current),o.current?.close(),o.current=null,S.current=`new`,x)try{localStorage.removeItem(x)}catch{}c.current=0,b(!1),u(!1),g(!1),setTimeout(()=>k(),0)},[]),k=(0,m.useCallback)(()=>{s.current&&=(clearTimeout(s.current),null),o.current&&=(o.current.onclose=null,o.current.close(),null);let t=i.current;if(!t)return;let n=window.location.protocol===`https:`?`wss:`:`ws:`,r=e.projectName??``,a=S.current,l=`/ws/project/${encodeURIComponent(r)}/terminal/${a}`,d=`${n}//${window.location.host}${l}`,f=new WebSocket(d);o.current=f,f.onopen=()=>{u(!0),g(!1),c.current=0,D()},f.onmessage=e=>{if(typeof e.data==`string`){if(e.data.startsWith(`{`))try{let n=JSON.parse(e.data);if(n.type){if(n.type===`session`&&n.id&&(S.current=n.id,x))try{localStorage.setItem(x,n.id)}catch{}n.type===`error`&&t.write(`\r\n\x1b[31mError: ${n.message}\x1b[0m\r\n`),n.type===`exited`&&b(!0);return}}catch{}t.write(e.data)}},f.onclose=()=>{u(!1),A()},f.onerror=()=>{f.close()}},[D]);function A(){let e=Math.min(1e3*2**c.current,3e4);c.current++,g(!0),s.current=setTimeout(()=>{k()},e)}return(0,m.useEffect)(()=>{let e=n.current;if(!e)return;let t=new f({cursorBlink:!0,fontSize:13,fontFamily:`var(--font-mono)`,theme:_(r.getState().theme)}),c=new d,l=new p;t.loadAddon(c),t.loadAddon(l),t.open(e),c.fit(),i.current=t,a.current=c,t.onData(e=>{if(e.includes(`\r`)||e.includes(`
`)){let e=t.buffer.active;C.current=e.baseY+e.cursorY+1}let n=o.current;n?.readyState===WebSocket.OPEN&&n.send(e)}),k();let u=new ResizeObserver(()=>{try{c.fit(),D()}catch{}});u.observe(e);let m=r.getState().theme,h=r.subscribe(e=>{e.theme!==m&&(m=e.theme,t.options.theme=_(e.theme))});return()=>{h(),u.disconnect(),s.current&&clearTimeout(s.current),o.current?.close(),o.current=null,t.dispose(),i.current=null,a.current=null}},[]),{connected:l,reconnecting:h,exited:y,sendData:w,getSelection:T,getLastCommandOutput:E,restart:O}}var b=t(),x=[{label:`Tab`,value:`	`},{label:`Esc`,value:`\x1B`},{label:`Ctrl`,value:null,isModifier:!0},{label:`↑`,value:`\x1B[A`},{label:`↓`,value:`\x1B[B`},{label:`←`,value:`\x1B[D`},{label:`→`,value:`\x1B[C`}],S=(0,m.memo)(function({metadata:e,tabId:t}){let n=e?.sessionId??`new`,r=e?.projectName,d=(0,m.useRef)(null),{connected:f,reconnecting:p,exited:h,sendData:g,getSelection:_,getLastCommandOutput:v,restart:S}=y({sessionId:n,projectName:r,containerRef:d,tabId:t}),[C,w]=(0,m.useState)(!1),[T,E]=(0,m.useState)(null);(0,m.useEffect)(()=>{let e=window.visualViewport;if(!e)return;function t(){e&&E(e.height)}return e.addEventListener(`resize`,t),()=>e.removeEventListener(`resize`,t)},[]);let D=(0,m.useCallback)(()=>{(d.current?.querySelector(`.xterm-helper-textarea`))?.focus()},[]),O=(0,m.useCallback)(e=>{if(D(),C&&e.length===1){let t=e.toLowerCase().charCodeAt(0)-96;t>=1&&t<=26&&g(String.fromCharCode(t)),w(!1);return}g(e)},[C,g,D]),k=(0,m.useCallback)(async()=>{let e=_();e&&await navigator.clipboard.writeText(e)},[_]),A=(0,m.useCallback)(async()=>{try{let e=await navigator.clipboard.readText();e&&(g(e),D())}catch{}},[g,D]),j=(0,m.useCallback)(()=>{let e=_(),t=e||v();if(!t.trim())return;let n="```bash\n"+t+"\n```",i=e?`Terminal selection`:`Terminal output`,o=!1,c=()=>{o=!0};window.addEventListener(`ppm:send-to-chat:ack`,c,{once:!0}),window.dispatchEvent(new CustomEvent(`ppm:send-to-chat`,{detail:{text:n,label:i,projectName:r}})),window.removeEventListener(`ppm:send-to-chat:ack`,c),o?s.success(`Sent to chat`,{duration:1500}):a.getState().openTab({type:`chat`,title:`Chat`,projectId:null,metadata:{projectName:r,pendingMessage:n},closable:!0})},[_,v,r]),M=typeof window<`u`&&`ontouchstart`in window;return(0,b.jsxs)(`div`,{className:`flex flex-col h-full`,style:T?{maxHeight:`${T}px`}:void 0,children:[(0,b.jsxs)(`div`,{className:`flex items-center gap-2 px-3 py-1 bg-surface border-b border-border text-xs`,children:[(0,b.jsx)(`span`,{className:i(`size-2 rounded-full`,h?`bg-error`:f?`bg-success`:p?`bg-warning`:`bg-error`)}),(0,b.jsx)(`span`,{className:`text-text-secondary`,children:h?`Process exited`:f?`Connected`:p?`Reconnecting...`:`Disconnected`}),h&&(0,b.jsxs)(`button`,{onClick:S,className:`flex items-center gap-1 px-1.5 py-0.5 rounded text-xs bg-surface-elevated text-text-primary hover:bg-primary hover:text-primary-foreground active:bg-primary active:text-primary-foreground transition-colors`,children:[(0,b.jsx)(u,{size:10}),`Restart`]}),(0,b.jsxs)(`button`,{onClick:j,title:`Send to Chat`,className:`flex items-center gap-1 px-1.5 py-0.5 rounded text-xs bg-surface-elevated text-text-primary hover:bg-primary hover:text-primary-foreground active:bg-primary active:text-primary-foreground transition-colors ml-auto`,children:[(0,b.jsx)(l,{size:10}),`Chat`]}),(0,b.jsx)(`span`,{className:`text-text-subtle font-mono`,children:n})]}),(0,b.jsx)(`div`,{ref:d,className:`flex-1 min-h-0 bg-background p-1`}),M&&(0,b.jsxs)(`div`,{className:`flex items-center gap-1 px-2 py-1.5 bg-surface border-t border-border overflow-x-auto`,children:[(0,b.jsx)(`button`,{onClick:k,className:`px-2 py-1.5 rounded text-xs min-w-[36px] min-h-[32px] bg-surface-elevated text-text-primary active:bg-primary active:text-primary-foreground transition-colors select-none`,children:(0,b.jsx)(c,{size:14})}),(0,b.jsx)(`button`,{onClick:A,className:`px-2 py-1.5 rounded text-xs min-w-[36px] min-h-[32px] bg-surface-elevated text-text-primary active:bg-primary active:text-primary-foreground transition-colors select-none`,children:(0,b.jsx)(o,{size:14})}),(0,b.jsx)(`button`,{onClick:j,className:`px-2 py-1.5 rounded text-xs min-w-[36px] min-h-[32px] bg-surface-elevated text-text-primary active:bg-primary active:text-primary-foreground transition-colors select-none`,"aria-label":`Send to Chat`,children:(0,b.jsx)(l,{size:14})}),(0,b.jsx)(`div`,{className:`w-px h-5 bg-border mx-0.5`}),x.map(e=>(0,b.jsx)(`button`,{onClick:()=>{e.label===`Ctrl`?w(!C):e.value&&O(e.value)},className:i(`px-3 py-1.5 rounded text-xs font-mono min-w-[36px] min-h-[32px]`,`bg-surface-elevated text-text-primary active:bg-primary active:text-primary-foreground`,`transition-colors select-none`,e.label===`Ctrl`&&C&&`bg-primary text-primary-foreground`),children:e.label},e.label))]})]})});export{S as TerminalTab};