import{a as e}from"./chunk-Cyuzqnbw.js";import{i as t,r as n}from"./bundle-mjs-DssaXR6m.js";import{x as r}from"./useBaseUiId-DL38u1cQ.js";import{t as i}from"./pause-DhMeMeEd.js";import{t as a}from"./play-D3mjFMmX.js";import{t as o}from"./trash-2-D5W0vize.js";import{n as s,s as c}from"./api-DTaM-PBA.js";import{t as l}from"./button-C5SbvNQM.js";import{t as u}from"./input-H0CxWzt0.js";var d=r(`arrow-down-to-line`,[[`path`,{d:`M12 17V3`,key:`1cwfxf`}],[`path`,{d:`m6 11 6 6 6-6`,key:`12ii2o`}],[`path`,{d:`M19 21H5`,key:`150jfl`}]]),f=e(t(),1),p=n(),m=2e3,h=[{key:`all`,label:`All`},{key:`warn`,label:`Warnings`},{key:`error`,label:`Errors`}],g={info:0,warn:1,error:2},_={info:`var(--muted-foreground)`,warn:`oklch(0.62 0.17 75)`,error:`oklch(0.58 0.22 27)`};function v(){let[e,t]=(0,f.useState)([]),[n,r]=(0,f.useState)(`all`),[v,y]=(0,f.useState)(``),[b,x]=(0,f.useState)(!0),[S,C]=(0,f.useState)(!1),w=(0,f.useRef)(null),T=(0,f.useRef)(null);(0,f.useEffect)(()=>{let e=!0,n=null,r=null;s(`/api/logs?limit=1000`).then(n=>e&&t(n.logs)).catch(()=>{});let i=()=>{n=c(`/api/logs/stream`),n.onopen=()=>e&&C(!0),n.onclose=()=>{e&&(C(!1),r=setTimeout(i,1500))},n.onmessage=e=>{try{let n=JSON.parse(e.data);t(e=>{if(e.length&&e[e.length-1].seq>=n.seq)return e;let t=[...e,n];return t.length>m?t.slice(-2e3):t})}catch{}}};return i(),()=>{e=!1,r&&clearTimeout(r),n?.close()}},[]);let E=(0,f.useMemo)(()=>{let t=n===`all`?0:g[n],r=v.trim().toLowerCase();return e.filter(e=>g[e.level]>=t&&(!r||e.text.toLowerCase().includes(r)))},[e,n,v]);return(0,f.useEffect)(()=>{b&&w.current?.scrollIntoView({block:`end`})},[E,b]),(0,p.jsxs)(`div`,{className:`flex h-full flex-col gap-3 p-6`,children:[(0,p.jsxs)(`div`,{className:`flex flex-wrap items-center gap-2`,children:[(0,p.jsx)(`h1`,{className:`mr-2 text-lg font-semibold`,children:`Logs`}),(0,p.jsx)(`div`,{className:`flex items-center gap-1 rounded-md border p-0.5`,style:{borderColor:`var(--border)`},children:h.map(e=>(0,p.jsx)(`button`,{onClick:()=>r(e.key),className:`rounded px-2 py-1 text-xs`,style:{background:n===e.key?`var(--primary)`:`transparent`,color:n===e.key?`var(--primary-foreground)`:`var(--muted-foreground)`},children:e.label},e.key))}),(0,p.jsx)(u,{value:v,onChange:e=>y(e.target.value),placeholder:`Filter…`,className:`h-8 w-48`}),(0,p.jsxs)(`span`,{className:`ml-auto flex items-center gap-1.5 text-xs`,style:{color:`var(--muted-foreground)`},children:[(0,p.jsx)(`span`,{className:`size-2 rounded-full`,style:{background:S?`oklch(0.7 0.17 145)`:`var(--muted-foreground)`}}),S?`live`:`reconnecting…`,` · `,E.length,` lines`]}),(0,p.jsxs)(l,{variant:`outline`,size:`sm`,onClick:()=>x(e=>!e),children:[b?(0,p.jsx)(i,{className:`size-4`}):(0,p.jsx)(a,{className:`size-4`}),b?`Pause scroll`:`Resume`]}),(0,p.jsxs)(l,{variant:`outline`,size:`sm`,onClick:()=>{w.current?.scrollIntoView({block:`end`}),x(!0)},children:[(0,p.jsx)(d,{className:`size-4`}),` Bottom`]}),(0,p.jsxs)(l,{variant:`outline`,size:`sm`,onClick:()=>t([]),children:[(0,p.jsx)(o,{className:`size-4`}),` Clear`]})]}),(0,p.jsx)(`div`,{ref:T,onScroll:()=>{let e=T.current;e&&x(e.scrollHeight-e.scrollTop-e.clientHeight<40)},className:`min-h-0 flex-1 overflow-auto rounded-md border font-mono text-xs leading-relaxed`,style:{borderColor:`var(--border)`,background:`var(--card)`},children:E.length===0?(0,p.jsxs)(`div`,{className:`p-4`,style:{color:`var(--muted-foreground)`},children:[`No log lines`,v||n!==`all`?` match the filter`:` yet`,`.`]}):(0,p.jsxs)(`div`,{className:`p-2`,children:[E.map(e=>(0,p.jsxs)(`div`,{className:`flex gap-2 whitespace-pre-wrap break-words px-1 py-0.5`,children:[(0,p.jsx)(`span`,{style:{color:`var(--muted-foreground)`},children:e.ts.slice(11,23)}),(0,p.jsx)(`span`,{style:{color:_[e.level],minWidth:`2.6rem`},children:e.level}),(0,p.jsx)(`span`,{style:{color:e.level===`error`?_.error:`var(--foreground)`},children:e.text})]},e.seq)),(0,p.jsx)(`div`,{ref:w})]})})]})}export{v as LogsPage};