import{a as l,u as v,j as e}from"./vendor-query-Brcc40-Y.js";import{L as u}from"./vendor-react-DP6BW_9B.js";import{T as N,S as d}from"./Skeleton-DGpeJz1z.js";import{T as y,i as b,C as k,n as w,A as C}from"./index-tN-ddyeb.js";import"./vendor-utils-ui5PHgZB.js";import"./vendor-zustand-CVDiRiMG.js";import"./vendor-form-BQNAly2Z.js";const S={search:(s,x="all")=>y.get(`/search?q=${encodeURIComponent(s)}&type=${x}`).then(r=>r.data)},E=[{key:"all",label:"All"},{key:"expense",label:"Expenses"},{key:"group",label:"Groups"},{key:"user",label:"People"}];function T({result:s}){return e.jsxs(u,{to:`/expenses/${s.id}`,className:"flex items-center gap-3 rounded-xl p-3 hover:bg-bg-elevated transition-all",children:[e.jsx("div",{className:"flex h-10 w-10 flex-shrink-0 items-center justify-center rounded-xl bg-bg-elevated text-xl",children:k[s.category]??"📦"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-medium text-ink truncate",children:s.description}),e.jsx("p",{className:"text-xs text-ink-muted",children:s.groupName??"Personal"})]}),e.jsx("p",{className:"text-sm font-semibold text-ink flex-shrink-0",children:w(s.amount,s.currency)})]})}function L({result:s}){return e.jsxs(u,{to:`/groups/${s.id}`,className:"flex items-center gap-3 rounded-xl p-3 hover:bg-bg-elevated transition-all",children:[e.jsx("div",{className:"flex h-10 w-10 flex-shrink-0 items-center justify-center rounded-xl bg-primary-subtle text-lg font-bold text-primary ring-1 ring-primary/20",children:s.name[0]}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-medium text-ink truncate",children:s.name}),e.jsxs("p",{className:"text-xs text-ink-muted",children:[s.memberCount," members · ",s.currency]})]}),e.jsx("svg",{className:"h-4 w-4 text-ink-faint flex-shrink-0",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M9 5l7 7-7 7"})})]})}function R({result:s}){return e.jsxs("div",{className:"flex items-center gap-3 rounded-xl p-3",children:[e.jsx(C,{name:s.name,src:s.avatarUrl,size:"sm"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-medium text-ink truncate",children:s.name}),e.jsx("p",{className:"text-xs text-ink-muted truncate",children:s.email})]})]})}function A({result:s}){return s.type==="expense"?e.jsx(T,{result:s}):s.type==="group"?e.jsx(L,{result:s}):s.type==="user"?e.jsx(R,{result:s}):null}const P={expense:"Expenses",group:"Groups",user:"People"};function z(){const[s,x]=l.useState(""),[r,h]=l.useState("all"),[a,f]=l.useState(""),m=l.useRef(null);l.useEffect(()=>{var t;(t=m.current)==null||t.focus()},[]),l.useEffect(()=>{const t=setTimeout(()=>f(s.trim()),350);return()=>clearTimeout(t)},[s]);const{data:n,isLoading:c,isFetching:g}=v({queryKey:["search",a,r],queryFn:()=>S.search(a,r),enabled:a.length>=2,staleTime:1e4}),o=(n==null?void 0:n.results)??[],j=r==="all"?["expense","group","user"].map(t=>({type:t,items:o.filter(i=>i.type===t)})).filter(t=>t.items.length>0):[{type:r,items:o}];return e.jsxs("div",{className:"flex flex-col",children:[e.jsx(N,{title:"Search"}),e.jsxs("div",{className:"mx-auto w-full max-w-2xl space-y-4 p-4 lg:p-6 animate-fade-in",children:[e.jsxs("div",{className:"relative",children:[e.jsx("svg",{className:"absolute left-4 top-1/2 -translate-y-1/2 h-5 w-5 text-ink-muted",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:1.5,d:"M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"})}),e.jsx("input",{ref:m,type:"search",value:s,onChange:t=>x(t.target.value),placeholder:"Search expenses, groups, people…",className:`w-full rounded-2xl border border-bg-overlay bg-bg-elevated pl-12 pr-4 py-3.5 text-sm text-ink placeholder-ink-faint
                       focus:border-primary focus:outline-none focus:ring-2 focus:ring-primary/30 transition-all`}),(c||g)&&a.length>=2&&e.jsx("div",{className:"absolute right-4 top-1/2 -translate-y-1/2 h-4 w-4 animate-spin rounded-full border-2 border-primary border-t-transparent"})]}),e.jsx("div",{className:"flex gap-1 rounded-xl bg-bg-elevated p-1",children:E.map(({key:t,label:i})=>e.jsx("button",{onClick:()=>h(t),className:b("flex-1 rounded-lg py-2 text-xs font-medium transition-all",r===t?"bg-primary text-ink shadow-soft":"text-ink-muted hover:text-ink"),children:i},t))}),a.length<2&&e.jsxs("div",{className:"flex flex-col items-center gap-3 py-16 text-center",children:[e.jsx("div",{className:"text-4xl",children:"🔍"}),e.jsx("p",{className:"text-sm text-ink-muted",children:"Type at least 2 characters to search"})]}),a.length>=2&&!c&&o.length===0&&e.jsxs("div",{className:"flex flex-col items-center gap-3 py-16 text-center",children:[e.jsx("div",{className:"text-4xl",children:"😕"}),e.jsxs("p",{className:"text-ink-muted font-medium",children:['No results for "',a,'"']}),e.jsx("p",{className:"text-sm text-ink-muted",children:"Try different keywords or change the filter."})]}),c&&a.length>=2&&e.jsx("div",{className:"card divide-y divide-bg-overlay overflow-hidden",children:[1,2,3].map(t=>e.jsxs("div",{className:"flex items-center gap-3 p-3 animate-pulse",children:[e.jsx(d,{className:"h-10 w-10 rounded-xl flex-shrink-0"}),e.jsxs("div",{className:"flex-1 space-y-1.5",children:[e.jsx(d,{className:"h-4 w-1/2"}),e.jsx(d,{className:"h-3 w-1/3"})]})]},t))}),!c&&o.length>0&&e.jsxs("div",{className:"space-y-4",children:[j.map(({type:t,items:i})=>e.jsxs("section",{children:[r==="all"&&e.jsx("p",{className:"text-xs font-semibold text-ink-muted uppercase tracking-wider mb-2",children:P[t]}),e.jsx("div",{className:"card divide-y divide-bg-overlay overflow-hidden",children:i.map(p=>e.jsx(A,{result:p},p.id))})]},t)),e.jsxs("p",{className:"text-center text-xs text-ink-faint",children:[(n==null?void 0:n.totalCount)??0," result",((n==null?void 0:n.totalCount)??0)!==1?"s":""]})]})]})]})}export{z as default};
