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{N as o,Q as s,X as c,at as l,i as u,it as d,rt as f}from"./vendor-mui-Ckcy4Lfd.js";import"./vendor-md-editor-DC6xr_29.js";import{$ as p,A as m,J as h,ft as g}from"./vendor-mui-icons-606Bohdn.js";import{f as _,i as v,p as y,r as b,u as x,x as S}from"./ui-BLXHSX1T.js";import{D as C,E as w,F as T,R as E,V as D,k as O,z as k}from"./index-JB73_g7R.js";import{t as A}from"./usePagination-DY41LU0X.js";import"./NoteForm-rqkev4Oq.js";var j=e(n(),1);function M(e,t=25){let[n,r]=(0,j.useState)([]),[i,a]=(0,j.useState)(!1),[o,s]=(0,j.useState)(null),{page:c,setPage:l,total:u,setTotal:d,totalPages:f,offset:p}=A(t),m=(0,j.useCallback)(async()=>{if(e){a(!0),s(null);try{let{items:n,total:i}=await T(e,{limit:t,offset:p});r(n),d(i)}catch(e){s(e instanceof Error?e.message:String(e))}finally{a(!1)}}},[e,t,p,d]);return(0,j.useEffect)(()=>{m()},[m]),{notes:n,total:u,page:c,setPage:l,totalPages:f,loading:i,error:o,refresh:m,create:(0,j.useCallback)(async t=>{e&&(await C(e,t),m())},[e,m]),update:(0,j.useCallback)(async(t,n)=>{if(!e)return;let i=await k(e,t,n);return r(e=>e.map(e=>e.id===t?i:e)),i},[e]),remove:(0,j.useCallback)(async t=>{e&&(await O(e,t),m())},[e,m])}}var N=t();function P(){let{projectId:e}=i(),t=r(),[n,C]=a(),T=D(`knowledge`),{notes:k,page:A,setPage:P,totalPages:F,loading:I,error:L,refresh:R}=M(e??null,25),[z,B]=(0,j.useState)(n.get(`q`)||``),[V,H]=(0,j.useState)(null),[U,W]=(0,j.useState)(!1),[G,K]=(0,j.useState)(null),[q,J]=(0,j.useState)(!1);S(e??null,(0,j.useCallback)(e=>{e.type.startsWith(`note:`)&&R()},[R]));let Y=(0,j.useCallback)(async t=>{if(!e||!t.trim()){H(null);return}W(!0);try{H(await E(e,t.trim()))}catch{}finally{W(!1)}},[e]);(0,j.useEffect)(()=>{let e=n.get(`q`);e?.trim()&&Y(e)},[]);let X=()=>{let e=new URLSearchParams(n);z.trim()?e.set(`q`,z.trim()):e.delete(`q`),C(e,{replace:!0}),Y(z)},Z=()=>{B(``),H(null);let e=new URLSearchParams(n);e.delete(`q`),C(e,{replace:!0})},Q=V??k;return(0,N.jsxs)(s,{children:[(0,N.jsx)(y,{breadcrumbs:[{label:`Knowledge`}],actions:T?(0,N.jsx)(c,{variant:`contained`,startIcon:(0,N.jsx)(h,{}),onClick:()=>t(`new`),children:`New Note`}):void 0}),(0,N.jsx)(_,{children:(0,N.jsx)(u,{fullWidth:!0,size:`small`,placeholder:`Semantic search notes...`,value:z,onChange:e=>B(e.target.value),onKeyDown:e=>e.key===`Enter`&&X(),slotProps:{input:{startAdornment:(0,N.jsx)(o,{position:`start`,children:(0,N.jsx)(g,{})}),endAdornment:z&&(0,N.jsx)(o,{position:`end`,children:(0,N.jsx)(d,{size:`small`,onClick:Z,children:(0,N.jsx)(p,{})})})}}})}),V&&(0,N.jsxs)(s,{sx:{mb:1,color:`text.secondary`,fontSize:`0.75rem`},children:[V.length,` result`,V.length===1?``:`s`,` found`]}),!V&&(0,N.jsx)(s,{sx:{mb:2},children:(0,N.jsx)(x,{page:A,totalPages:F,onPageChange:P,onRefresh:R})}),L&&(0,N.jsx)(f,{severity:`error`,sx:{mb:2},children:L}),I||U?(0,N.jsx)(s,{sx:{display:`flex`,justifyContent:`center`,py:4},children:(0,N.jsx)(l,{})}):Q.length===0?(0,N.jsx)(v,{icon:(0,N.jsx)(m,{}),title:V?`No matching notes found`:`No notes yet`,description:V?`Try a different search query`:`Create your first note to get started`,action:!V&&T?(0,N.jsx)(c,{variant:`contained`,startIcon:(0,N.jsx)(h,{}),onClick:()=>t(`new`),children:`New Note`}):void 0}):(0,N.jsx)(s,{sx:{display:`grid`,gridTemplateColumns:{xs:`1fr`,md:`1fr 1fr`},gap:2},children:Q.map(e=>(0,N.jsx)(w,{note:e,score:`score`in e?e.score:void 0,onClick:()=>t(e.id),onEdit:T?()=>t(`${e.id}/edit`):void 0,onDelete:T?()=>K(e):void 0},e.id))}),!V&&(0,N.jsx)(s,{sx:{mt:2},children:(0,N.jsx)(x,{page:A,totalPages:F,onPageChange:P,onRefresh:R})}),(0,N.jsx)(b,{open:!!G,title:`Delete note`,message:`Delete "${G?.title}"? This action cannot be undone.`,confirmLabel:`Delete`,confirmColor:`error`,loading:q,onCancel:()=>K(null),onConfirm:async()=>{if(!(!e||!G)){J(!0);try{await O(e,G.id),K(null),R()}finally{J(!1)}}}})]})}export{P as default};