import{r as a,j as e,L as R,T as A,z,Q as B,I as b,B as p,ar as D}from"./index-Dbk1Plmn.js";import{a as J}from"./use_schemas-DMLDIw9M.js";import{u as F}from"./use_agents-DbRumskw.js";import{i as K}from"./use_mutations-BiggAKwe.js";import{P as G}from"./page_shell-C0CL85dt.js";import{D as q}from"./data-table-B-cqS3KC.js";import{O as U}from"./pagination-BoJeck8D.js";import{L as x}from"./label-BKULM0qV.js";import{T}from"./textarea-BDInZ0Bq.js";import{D as V,a as H,b as Y,c as Z,d as W,e as X,f as ee}from"./dialog-CIA-8Qg9.js";import{s as se,a as P}from"./query_loading-BFETHugg.js";import{Q as te}from"./query_refresh_indicator-1_XqkpX1.js";import{g as ae}from"./agent_grant_key-BDL9K-EB.js";import{P as re}from"./plus-DrFK9jPU.js";import"./relationships-D_VYvxI6.js";import"./chevron-right-L2MnFoxW.js";import"./Combination-DUY7QPO8.js";const ne=[10,25,50,100];function we(){var N,w,_;const r=J(),g=F({status:"all"}),O=K(),[f,y]=a.useState(""),[j,E]=a.useState(`{
  "fields": {}
}`),[S,M]=a.useState(`{
  "merge_policies": {}
}`),[c,I]=a.useState(""),[l,m]=a.useState(0),[i,L]=a.useState(25),u=((N=r.data)==null?void 0:N.schemas)??[],n=a.useMemo(()=>{const s=c.trim().toLowerCase();return s?u.filter(t=>{const o=t.entity_type.toLowerCase(),d=String(t.schema_version??"").toLowerCase(),h=(t.field_names??[]).join(" ").toLowerCase();return o.includes(s)||d.includes(s)||h.includes(s)}):u},[u,c]);a.useEffect(()=>{m(0)},[c]),a.useEffect(()=>{m(s=>{if(n.length===0)return 0;const t=Math.max(0,Math.ceil(n.length/i)*i-i);return Math.min(s,t)})},[n.length,i]);const Q=a.useMemo(()=>n.slice(l,l+i),[n,l,i]),v=a.useMemo(()=>{var d,h;const s=((d=r.data)==null?void 0:d.schemas)??[],t=((h=g.data)==null?void 0:h.grants)??[],o=new Map;for(const C of s)o.set(C.entity_type,ae(t,C.entity_type).length);return o},[(w=r.data)==null?void 0:w.schemas,(_=g.data)==null?void 0:_.grants]),k=a.useMemo(()=>[{header:"Entity Type",accessorKey:"entity_type",cell:({row:s})=>e.jsx(R,{to:`/schemas/${encodeURIComponent(s.original.entity_type)}`,className:"font-medium text-primary hover:underline",children:e.jsx(A,{type:s.original.entity_type})})},{header:"Version",accessorKey:"schema_version"},{header:"Fields",accessorKey:"field_names",cell:({getValue:s})=>{const t=s();return t?t.length:"—"}},{header:"Active",accessorKey:"active",cell:({getValue:s})=>s()!==!1?"Yes":"No"},{header:()=>e.jsxs("span",{className:"block max-w-[5rem] leading-tight",children:["Grants",e.jsx("span",{className:"mt-0.5 block text-[11px] font-normal text-muted-foreground",children:"Admission"})]}),id:"admission_grants",cell:({row:s})=>{const t=s.original.entity_type,o=v.get(t)??0;return e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsx("span",{className:"tabular-nums text-muted-foreground",children:o}),o>0?e.jsx(R,{to:`/schemas/${encodeURIComponent(t)}#admission-grants`,className:"text-xs text-primary hover:underline",children:"View"}):null]})}}],[v]),$=a.useMemo(()=>{if(!r.data)return;const s=r.data.total??r.data.schemas.length,t=c.trim();return`${t&&n.length!==s?`${n.length} of ${s} match — `:t&&n.length===s?`${s} match — `:`${s} registered — `}Admission grants column counts agent_grant capabilities that list each entity_type (or *).`},[r.data,c,n.length]);return e.jsx(G,{title:"Schemas",description:$,actions:e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[P(r)||P(g)?e.jsx(te,{}):null,e.jsxs(V,{children:[e.jsx(H,{asChild:!0,children:e.jsxs(p,{size:"sm",children:[e.jsx(re,{className:"h-3 w-3 mr-1"})," Register"]})}),e.jsxs(Y,{className:"max-w-2xl",children:[e.jsxs(Z,{children:[e.jsx(W,{children:"Register Schema"}),e.jsx(X,{children:"Register a new entity type schema."})]}),e.jsxs("div",{className:"grid gap-3",children:[e.jsxs("div",{children:[e.jsx(x,{children:"Entity Type"}),e.jsx(b,{value:f,onChange:s=>y(s.target.value),placeholder:"my_entity_type"})]}),e.jsxs("div",{children:[e.jsx(x,{children:"Schema Definition (JSON)"}),e.jsx(T,{value:j,onChange:s=>E(s.target.value),rows:6,className:"font-mono text-xs"})]}),e.jsxs("div",{children:[e.jsx(x,{children:"Reducer Config (JSON)"}),e.jsx(T,{value:S,onChange:s=>M(s.target.value),rows:4,className:"font-mono text-xs"})]})]}),e.jsx(ee,{children:e.jsx(p,{onClick:()=>{try{O.mutate({entity_type:f,schema_definition:JSON.parse(j),reducer_config:JSON.parse(S),activate:!0},{onSuccess:()=>{D.success("Schema registered"),y("")}})}catch{D.error("Invalid JSON")}},children:"Register"})})]})]})]}),children:se(r)?e.jsx(z,{rows:8,cols:5}):r.error?e.jsx(B,{title:"Could not load schemas",children:r.error.message}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-wrap items-end justify-between gap-3",children:[e.jsx(b,{placeholder:"Search entity type, version, field name…",value:c,onChange:s=>I(s.target.value),className:"w-full min-w-[200px] max-w-md","aria-label":"Filter schemas"}),e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Rows per page"}),e.jsx("div",{className:"flex rounded-md border border-border p-0.5",children:ne.map(s=>e.jsx(p,{type:"button",variant:i===s?"secondary":"ghost",size:"sm",className:"h-7 px-2 text-xs",onClick:()=>{L(s),m(0)},children:s},s))})]})]}),e.jsx(q,{columns:k,data:Q,emptyLabel:c.trim()?"No schemas match the current filter.":"No registered schemas."}),n.length>0?e.jsx(U,{offset:l,limit:i,total:n.length,onPageChange:m}):null]})})}export{we as default};
