import{v as D,r as i,w as b,i as E,j as e,L as T,x as h,T as L,q as k,y as R,I as q,z as A,Q as _,o as Q,A as U,H as $,J as K,K as F}from"./index-Dbk1Plmn.js";import{P as M}from"./page_shell-C0CL85dt.js";import{D as O}from"./data-table-B-cqS3KC.js";import{s as I}from"./query_loading-BFETHugg.js";const z=150,B=25,G=25;function u(t){return t.entity_id??t.id??""}function W(){var j,S,N;const[t,x]=D(),o=t.get("search")??"",[m,p]=i.useState(o),[n,C]=i.useState(o.trim());i.useEffect(()=>{p(o)},[o]),i.useEffect(()=>{const s=window.setTimeout(()=>{const a=m.trim();if(C(a),a===o)return;const r=new URLSearchParams(t);a?r.set("search",a):r.delete("search"),x(r,{replace:!0})},z);return()=>window.clearTimeout(s)},[m,t,x,o]);const c=b({queryKey:["search-page","entities",n],queryFn:()=>K({search:n,limit:B,include_snapshots:!0}),enabled:E()&&n.length>0}),l=b({queryKey:["search-page","sources",n],queryFn:()=>F({search:n,limit:G}),enabled:E()&&n.length>0}),w=i.useMemo(()=>[{header:"Name",accessorFn:s=>{var a,r;return s.canonical_name||((a=s.snapshot)==null?void 0:a.name)||((r=s.snapshot)==null?void 0:r.title)||u(s)},cell:({row:s})=>{var r,v;const a=u(s.original);return e.jsx(T,{to:`/entities/${encodeURIComponent(a)}`,className:"font-medium text-primary hover:underline",children:String(s.original.canonical_name||((r=s.original.snapshot)==null?void 0:r.name)||((v=s.original.snapshot)==null?void 0:v.title)||h(a))})}},{header:"Type",accessorKey:"entity_type",cell:({getValue:s})=>e.jsx(L,{type:s()})},{header:"Last Observation",accessorKey:"last_observation_at",cell:({getValue:s})=>k(s())},{header:"ID",accessorFn:s=>u(s),cell:({getValue:s})=>e.jsx("span",{className:"font-mono text-xs text-muted-foreground",children:h(s(),12)})}],[]),g=((j=c.data)==null?void 0:j.entities)??[],d=((S=l.data)==null?void 0:S.sources)??[],y=((N=c.data)==null?void 0:N.total)??0,f=n.length>0,P=f?`${y.toLocaleString()} entity matches${d.length?`, ${d.length.toLocaleString()} source matches loaded`:""}`:"Search entities and sources from one place.";return e.jsxs(M,{title:"Search",description:P,children:[e.jsx("div",{className:"max-w-xl",children:e.jsxs("div",{className:"relative",children:[e.jsx(R,{className:"absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground"}),e.jsx(q,{placeholder:"Search entities and sources…",value:m,onChange:s=>p(s.target.value),className:"pl-9","aria-label":"Search entities and sources"})]})}),f?e.jsxs("div",{className:"space-y-8",children:[e.jsxs("section",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold",children:"Entities"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:c.data?`${y.toLocaleString()} total matches`:"Search matches from entity snapshots and canonical names."})]}),I(c)?e.jsx(A,{rows:8,cols:4}):c.error?e.jsx(_,{title:"Could not load entity matches",children:c.error.message}):g.length>0?e.jsx(O,{columns:w,data:g}):e.jsx("div",{className:"rounded-md border p-4 text-sm text-muted-foreground",children:"No matching entities."})]}),e.jsxs("section",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold",children:"Sources"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Matching raw files, uploads, and stored source material."})]}),I(l)?e.jsx(Q,{rows:6}):l.error?e.jsx(_,{title:"Could not load source matches",children:l.error.message}):d.length>0?e.jsx("div",{className:"space-y-2",children:d.map(s=>e.jsx(H,{source:s},s.id))}):e.jsx("div",{className:"rounded-md border p-4 text-sm text-muted-foreground",children:"No matching sources."})]})]}):e.jsx("div",{className:"rounded-lg border border-dashed p-6 text-sm text-muted-foreground",children:"Type a keyword to search across entities and sources."})]})}function H({source:t}){return e.jsx("div",{className:"rounded-md border p-3",children:e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx(T,{to:`/sources/${encodeURIComponent(t.id)}`,className:"block truncate text-sm font-medium text-primary hover:underline",children:U(t)}),e.jsx("p",{className:"mt-1 text-sm text-muted-foreground",children:$(t)})]}),e.jsx("span",{className:"shrink-0 font-mono text-xs text-muted-foreground",children:h(t.id,12)})]})})}export{W as default};
