import{w as A,i as Q,W as q,r,j as e,y as K,I as o,z as B,Q as J,B as d,ar as E,T as L,q as R}from"./index-Dbk1Plmn.js";import{q as z}from"./relationships-D_VYvxI6.js";import{c as G}from"./use_mutations-BiggAKwe.js";import{P as H}from"./page_shell-C0CL85dt.js";import{D as M}from"./data-table-B-cqS3KC.js";import{E as U}from"./entity_link-qYsgSBpl.js";import{S as V}from"./source_link-Bg790zHv.js";import{A as W}from"./agent_badge-BZd6ZAh5.js";import{u as Z}from"./agent_filter-DUlgHUAb.js";import{J as $}from"./json_viewer-ClIBr-BE.js";import{O as X}from"./pagination-BoJeck8D.js";import{L as c}from"./label-BKULM0qV.js";import{T as Y}from"./textarea-BDInZ0Bq.js";import{D as ee,a as te,b as se,c as ae,d as re,e as ie,f as oe}from"./dialog-CIA-8Qg9.js";import{s as ne,a as le}from"./query_loading-BFETHugg.js";import{Q as de}from"./query_refresh_indicator-1_XqkpX1.js";import{P as ce}from"./plus-DrFK9jPU.js";import"./chevron-right-L2MnFoxW.js";import"./chevron-down-BMaycUN9.js";import"./Combination-DUY7QPO8.js";function ue(i){return A({queryKey:["observations",i],queryFn:()=>z(i),placeholderData:q,enabled:Q()})}const u=25;function we(){var C,D;const[i,N]=r.useState(""),[m,I]=r.useState(""),[x,O]=r.useState(""),[p,n]=r.useState(0),[y,h]=r.useState(""),[g,f]=r.useState(""),[j,v]=r.useState("{}"),_=G(),s=ue({entity_id:i||void 0,entity_type:m||void 0,source_id:x||void 0,limit:u,offset:p}),[l,T]=r.useState(null),b=((C=s.data)==null?void 0:C.observations)??[],{filterRows:F,AgentFilterControl:w}=Z(b),S=F(b),P=[{header:"Entity",accessorKey:"entity_id",cell:({row:t})=>e.jsxs("div",{children:[e.jsx(U,{id:t.original.entity_id}),e.jsx("div",{className:"mt-0.5",children:e.jsx(L,{type:t.original.entity_type})})]})},{header:"Source",accessorKey:"source_id",cell:({getValue:t})=>{const a=t();return a?e.jsx(V,{id:a}):e.jsx("span",{className:"text-muted-foreground",children:"—"})}},{header:"Priority",accessorKey:"source_priority"},{header:"Specificity",accessorKey:"specificity_score",cell:({getValue:t})=>{var a;return((a=t())==null?void 0:a.toFixed(2))??"—"}},{header:"Observed",accessorKey:"observed_at",cell:({getValue:t})=>R(t())},{header:"Agent",id:"agent",cell:({row:t})=>e.jsx(W,{provenance:t.original.provenance??null})},{header:"Fields",id:"field_count",cell:({row:t})=>{const a=t.original.fields,k=a?Object.keys(a).length:0;return e.jsxs(d,{variant:"ghost",size:"sm",onClick:()=>T(l===t.original.id?null:t.original.id),children:[k," fields ",l===t.original.id?"▾":"▸"]})}}];return e.jsxs(H,{title:"Observations",description:s.data?`${s.data.total.toLocaleString()} total`:void 0,actions:e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[le(s)?e.jsx(de,{}):null,e.jsxs(ee,{children:[e.jsx(te,{asChild:!0,children:e.jsxs(d,{size:"sm",children:[e.jsx(ce,{className:"h-3 w-3 mr-1"})," Create"]})}),e.jsxs(se,{children:[e.jsxs(ae,{children:[e.jsx(re,{children:"Create Observation"}),e.jsx(ie,{children:"Directly create an observation for an entity."})]}),e.jsxs("div",{className:"grid gap-3",children:[e.jsxs("div",{children:[e.jsx(c,{children:"Entity ID"}),e.jsx(o,{value:y,onChange:t=>h(t.target.value)})]}),e.jsxs("div",{children:[e.jsx(c,{children:"Entity Type"}),e.jsx(o,{value:g,onChange:t=>f(t.target.value)})]}),e.jsxs("div",{children:[e.jsx(c,{children:"Fields (JSON)"}),e.jsx(Y,{value:j,onChange:t=>v(t.target.value),rows:5,className:"font-mono text-xs"})]})]}),e.jsx(oe,{children:e.jsx(d,{onClick:()=>{try{const t=JSON.parse(j);_.mutate({entity_id:y,entity_type:g,fields:t},{onSuccess:()=>{E.success("Observation created"),h(""),f(""),v("{}")}})}catch{E.error("Invalid JSON in fields")}},children:"Create"})})]})]})]}),children:[e.jsxs("div",{className:"flex flex-wrap items-end gap-3",children:[e.jsxs("div",{className:"relative min-w-[180px]",children:[e.jsx(K,{className:"absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground"}),e.jsx(o,{placeholder:"Filter by entity ID…",value:i,onChange:t=>{N(t.target.value),n(0)},className:"pl-9"})]}),e.jsx(o,{placeholder:"Entity type…",value:m,onChange:t=>{I(t.target.value),n(0)},className:"w-[150px]"}),e.jsx(o,{placeholder:"Source ID…",value:x,onChange:t=>{O(t.target.value),n(0)},className:"w-[180px]"}),e.jsx(w,{})]}),ne(s)?e.jsx(B,{rows:12,cols:7}):s.error?e.jsx(J,{title:"Could not load observations",children:s.error.message}):e.jsxs(e.Fragment,{children:[e.jsx(M,{columns:P,data:S}),l&&e.jsx("div",{className:"rounded-md border p-4 mt-2",children:e.jsx($,{data:(D=S.find(t=>t.id===l))==null?void 0:D.fields,defaultExpanded:!0})}),s.data&&s.data.total>u&&e.jsx(X,{offset:p,limit:u,total:s.data.total,onPageChange:n})]})]})}export{we as default};
