import{r as e}from"./rolldown-runtime-Dw2cE7zH.js";import{O as t,k as n}from"./vendor-markdown-DngssFHR.js";import{c as r,l as i,u as a}from"./vendor-react-Cts2Wj-s.js";import{$ as o,A as s,N as c,O as l,Q as u,T as d,Z as ee,at as te,g as ne,i as re,k as f,lt as p,nt as m,pt as ie,rt as ae,w as h}from"./vendor-mui-Ckcy4Lfd.js";import"./vendor-md-editor-DC6xr_29.js";import{F as oe,I as g,Y as _,ft as v,ht as se}from"./vendor-mui-icons-606Bohdn.js";import{_ as y,f as b,i as x,p as ce,u as S}from"./ui-BLXHSX1T.js";import{t as le}from"./usePagination-DY41LU0X.js";import{n as ue,r as C}from"./api-DCKbsCUZ.js";var w=e(n(),1),T=t();function E(e){return e==null?``:e<1024?`${e} B`:e<1024*1024?`${(e/1024).toFixed(1)} KB`:`${(e/1024/1024).toFixed(1)} MB`}function D(){let{projectId:e}=i(),t=r(),{palette:n}=ie(),[D,O]=a(),[k,A]=(0,w.useState)([]),[j,M]=(0,w.useState)(!0),[N,P]=(0,w.useState)(null),[F,I]=(0,w.useState)(D.get(`dir`)||`.`),{page:L,setPage:R,setTotal:z,totalPages:B,offset:V,pageSize:H}=le(25),[U,W]=(0,w.useState)(null),[de,G]=(0,w.useState)(!1),[K,q]=(0,w.useState)(D.get(`q`)||``),J=e=>{let t=new URLSearchParams(D);e&&e!==`.`?t.set(`dir`,e):t.delete(`dir`),t.delete(`q`),O(t,{replace:!0})},Y=(0,w.useCallback)(async t=>{if(e){M(!0),P(null);try{let{items:n,total:r}=await ue(e,{directory:t||`.`,limit:H,offset:V});A(n),z(r),I(t)}catch(e){P(e instanceof Error?e.message:String(e))}finally{M(!1)}}},[e,H,V,z]);(0,w.useEffect)(()=>{Y(D.get(`dir`)||`.`)},[Y]);let X=(0,w.useCallback)(async t=>{if(!e||!t.trim()){W(null);return}G(!0);try{W(await C(e,t.trim()))}catch{}finally{G(!1)}},[e]);(0,w.useEffect)(()=>{let e=D.get(`q`);e?.trim()&&X(e)},[]);let fe=()=>{let e=new URLSearchParams(D);K.trim()?e.set(`q`,K.trim()):e.delete(`q`),O(e,{replace:!0}),X(K)},pe=n=>{n.kind===`directory`?(W(null),R(1),J(n.filePath),Y(n.filePath)):t(`/${e}/files/view/${n.filePath}`)},Z=e=>{W(null),R(1),J(e),Y(e)},me=()=>{let e=F.split(`/`).slice(0,-1);Z(e.length>0?e.join(`/`):`.`)},Q=F&&F!==`.`?F.split(`/`):[],$=[...U??k].sort((e,t)=>e.kind===`directory`&&t.kind!==`directory`?-1:e.kind!==`directory`&&t.kind===`directory`?1:e.filePath.localeCompare(t.filePath));return(0,T.jsxs)(u,{children:[(0,T.jsx)(ce,{breadcrumbs:[{label:`Files`}]}),(0,T.jsx)(b,{children:(0,T.jsx)(re,{fullWidth:!0,size:`small`,placeholder:`Search files...`,value:K,onChange:e=>q(e.target.value),onKeyDown:e=>e.key===`Enter`&&fe(),slotProps:{input:{startAdornment:(0,T.jsx)(c,{position:`start`,children:(0,T.jsx)(v,{})})}}})}),!U&&(0,T.jsxs)(u,{sx:{display:`flex`,alignItems:`center`,gap:1,mb:2},children:[F&&F!==`.`&&(0,T.jsx)(o,{icon:(0,T.jsx)(g,{}),label:`Up`,size:`small`,onClick:me,clickable:!0}),(0,T.jsxs)(ee,{children:[(0,T.jsx)(s,{component:`button`,variant:`body2`,underline:`hover`,onClick:()=>Z(`.`),color:!F||F===`.`?`text.primary`:`inherit`,children:`root`}),Q.map((e,t)=>{let n=Q.slice(0,t+1).join(`/`),r=t===Q.length-1;return(0,T.jsx)(s,{component:`button`,variant:`body2`,underline:r?`none`:`hover`,onClick:()=>!r&&Z(n),color:r?`text.primary`:`inherit`,children:e},n)})]})]}),U&&(0,T.jsxs)(m,{variant:`caption`,sx:{color:n.custom.textMuted,mb:1,display:`block`},children:[U.length,` result`,U.length===1?``:`s`,` `,(0,T.jsx)(s,{component:`button`,onClick:()=>{q(``),W(null);let e=new URLSearchParams(D);e.delete(`q`),O(e,{replace:!0})},children:`clear`})]}),!U&&(0,T.jsx)(u,{sx:{mb:2},children:(0,T.jsx)(S,{page:L,totalPages:B,onPageChange:R,onRefresh:()=>Y(F)})}),N&&(0,T.jsx)(ae,{severity:`error`,sx:{mb:2},children:N}),j||de?(0,T.jsx)(u,{sx:{display:`flex`,justifyContent:`center`,py:4},children:(0,T.jsx)(te,{})}):$.length===0?(0,T.jsx)(x,{icon:(0,T.jsx)(oe,{}),title:U?`No files found`:`Empty directory`,description:U?`Try a different search query`:void 0}):(0,T.jsx)(p,{variant:`outlined`,children:(0,T.jsx)(f,{dense:!0,disablePadding:!0,children:$.map(e=>(0,T.jsxs)(l,{onClick:()=>pe(e),sx:{borderBottom:`1px solid ${n.custom.border}`},children:[(0,T.jsx)(d,{sx:{minWidth:36},children:e.kind===`directory`?(0,T.jsx)(se,{sx:{color:`#f9a825`}}):(0,T.jsx)(_,{sx:{color:n.custom.textMuted}})}),(0,T.jsx)(h,{primary:(0,T.jsxs)(u,{sx:{display:`flex`,alignItems:`center`,gap:1},children:[(0,T.jsx)(`span`,{children:U?e.filePath:e.fileName}),`score`in e&&(0,T.jsx)(y,{label:`${(e.score*100).toFixed(0)}%`,color:`primary`,size:`small`})]})}),(0,T.jsxs)(ne,{direction:`row`,spacing:1,alignItems:`center`,children:[e.language&&(0,T.jsx)(m,{variant:`caption`,sx:{color:n.custom.textMuted},children:e.language}),(0,T.jsx)(m,{variant:`caption`,sx:{color:n.custom.textMuted,minWidth:60,textAlign:`right`},children:e.kind===`directory`?e.fileCount>0?`${e.fileCount} files`:``:E(e.size)})]})]},e.filePath))})}),!U&&(0,T.jsx)(u,{sx:{mt:2},children:(0,T.jsx)(S,{page:L,totalPages:B,onPageChange:R,onRefresh:()=>Y(F)})})]})}export{D as default};