import{a as e,c as t,f as n,l as r,t as i}from"./widget-shared-ysjNKbt7.js";import{a,i as o,n as s,r as c}from"./highlight-WLBKzBhu.js";import{_ as l,a as u,b as d,c as f,d as p,f as m,g as h,h as g,i as _,m as v,n as y,o as b,p as x,r as S,s as C,t as w,u as T,v as E,y as D}from"./index-B3EFxtWE.js";var O=D(`file-x`,[[`path`,{d:`M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z`,key:`1oefj6`}],[`path`,{d:`M14 2v5a1 1 0 0 0 1 1h5`,key:`wfsgrz`}],[`path`,{d:`m14.5 12.5-5 5`,key:`b62r18`}],[`path`,{d:`m9.5 12.5 5 5`,key:`1rk7el`}]]),k=n(r(),1),A=t();function j({value:e,src:t,lang:n,maxHeight:r=`24rem`,ctx:a}){let o=(0,k.useMemo)(()=>w(t,e,a.fileKey,a.storage),[t,e,a.fileKey,a.storage]);return o.ok?e==null||e===``?(0,A.jsx)(i,{}):(0,A.jsx)(M,{url:o.url,pathForLang:o.key??o.url,lang:n,maxHeight:r,detail:P(o)}):(0,A.jsx)(N,{reason:o.reason})}function M({url:t,pathForLang:n,lang:r,maxHeight:w,detail:D}){let O=d({queryKey:[`rows-text-widget`,t],queryFn:({pageParam:e})=>u(t,e,e+y-1),initialPageParam:0,getNextPageParam:e=>e.isFull?void 0:e.endByte+1,staleTime:1/0,retry:1}),j=(0,k.useMemo)(()=>(O.data?.pages??[]).reduce(C,S),[O.data]),M=O.isPending&&O.isFetching,N=O.isFetchingNextPage,P=O.error?_(O.error):null,F=(0,k.useMemo)(()=>r&&r.length>0?r:s(n),[r,n]),[I,L]=(0,k.useState)(()=>a(F)||F===`plaintext`);(0,k.useEffect)(()=>{if(F===`plaintext`||a(F)){L(!0);return}L(!1);let e=!1;return c(F).then(t=>{!e&&t&&L(!0)}),()=>{e=!0}},[F]);let R=(0,k.useMemo)(()=>{let e=f(j.text);return!j.done&&!j.text.endsWith(`
`)&&e.length>0&&e.pop(),e},[j.text,j.done]),z=(0,k.useMemo)(()=>!I||R.length===0||F===`plaintext`?null:R.map(e=>o(e,F)),[R,F,I]),B=!j.done&&j.text.length>0,V=j.totalBytes!==null&&j.totalBytes>0?Math.min(100,Math.round(j.bytesLoaded/j.totalBytes*100)):null;return M&&j.text.length===0?(0,A.jsx)(`div`,{className:`flex w-full flex-col gap-1.5 rounded-md border bg-muted/30 p-3`,children:Array.from({length:6}).map((e,t)=>(0,A.jsx)(T,{className:`h-3 w-full`},t))}):P&&j.text.length===0?(0,A.jsxs)(m,{variant:`destructive`,className:`py-2`,children:[(0,A.jsx)(E,{className:`size-4`}),(0,A.jsx)(v,{className:`text-xs`,children:`Failed to read text file`}),(0,A.jsxs)(x,{className:`flex flex-col gap-2`,children:[(0,A.jsx)(`span`,{className:`font-mono text-[11px] break-all`,children:D}),(0,A.jsx)(`span`,{className:`text-xs`,children:P}),(0,A.jsxs)(p,{variant:`outline`,size:`sm`,onClick:()=>void O.refetch(),disabled:O.isFetching,className:`self-start`,children:[O.isFetching?(0,A.jsx)(h,{className:`size-4 animate-spin`}):(0,A.jsx)(g,{className:`size-4`}),`Retry`]})]})]}):j.text.length===0&&j.done?(0,A.jsx)(i,{text:`(empty file)`}):(0,A.jsxs)(`div`,{className:`flex w-full flex-col overflow-hidden rounded-md border bg-muted/30`,children:[(0,A.jsxs)(`div`,{className:`flex items-center justify-between gap-2 border-b bg-background/50 px-2 py-1 text-[11px]`,children:[(0,A.jsxs)(`div`,{className:`flex min-w-0 items-center gap-1.5`,children:[B&&(0,A.jsxs)(`span`,{className:`flex shrink-0 items-center gap-1 rounded bg-amber-500/15 px-1 py-0.5 font-medium text-amber-700 dark:text-amber-400`,title:`Only part of the file has been loaded — click 'Load more' for the rest.`,children:[(0,A.jsx)(E,{className:`size-3`}),`Partial`,V===null?``:` · ${V}%`]}),(0,A.jsx)(`span`,{className:`truncate font-mono text-muted-foreground`,title:D,children:e(D)})]}),(0,A.jsxs)(`span`,{className:`shrink-0 text-muted-foreground`,children:[R.length,` line`,R.length===1?``:`s`,` ·`,` `,b(j.bytesLoaded),j.totalBytes!==null&&` / ${b(j.totalBytes)}`]})]}),(0,A.jsx)(`div`,{className:`hljs overflow-auto p-2 font-mono text-[11px] leading-relaxed`,style:{maxHeight:w},children:R.map((e,t)=>{let n=z?.[t]??null;return(0,A.jsx)(`div`,{className:`whitespace-pre-wrap break-words`,children:n===null?e:(0,A.jsx)(`span`,{dangerouslySetInnerHTML:{__html:n}})},t)})}),!j.done&&j.text.length>0&&(0,A.jsxs)(`div`,{className:`flex items-center justify-end gap-2 border-t bg-background/50 px-2 py-1`,children:[P&&(0,A.jsx)(`span`,{className:`mr-auto truncate text-[11px] text-destructive`,title:P,children:P}),(0,A.jsxs)(p,{type:`button`,size:`sm`,variant:`outline`,className:`h-6 px-2 text-[11px]`,disabled:N,onClick:()=>void O.fetchNextPage(),children:[N?(0,A.jsx)(h,{className:`size-3.5 animate-spin`}):(0,A.jsx)(l,{className:`size-3.5`}),`Load more`]})]})]})}function N({reason:e}){return(0,A.jsxs)(`div`,{className:`flex items-start gap-2 rounded-md border border-dashed border-destructive/40 bg-destructive/5 px-3 py-2 text-xs italic text-destructive`,children:[(0,A.jsx)(O,{className:`mt-0.5 size-4 shrink-0`}),(0,A.jsx)(`span`,{className:`min-w-0 break-all`,children:e})]})}function P(e){return e.ok?e.rendered:``}export{j as WidgetText,j as default};