import{v as C,r as n,j as e,I as w,z as N,Q as _,B as p,L as x,b1 as D,af as f,q as k}from"./index-Dbk1Plmn.js";import{P as I}from"./page_shell-C0CL85dt.js";import{D as P}from"./data-table-B-cqS3KC.js";import{S as Q,a as T,b as E,c as L,d as R}from"./select-B273iZTf.js";import{D as B,a as F,b as K,c as O,d as $,e as q}from"./dialog-CIA-8Qg9.js";import{A as G}from"./agent_grant_form-BOodcTsk.js";import{s as z,a as H}from"./query_loading-BFETHugg.js";import{Q as M}from"./query_refresh_indicator-1_XqkpX1.js";import{u as V,d as U}from"./use_agents-DbRumskw.js";import{A as J}from"./arrow-left-C_rgOt55.js";import{P as W}from"./plus-DrFK9jPU.js";import"./index-dCMWtMMp.js";import"./Combination-DUY7QPO8.js";import"./index-D3OXdV3z.js";import"./chevron-down-BMaycUN9.js";import"./check-C3ZzJelL.js";import"./label-BKULM0qV.js";import"./textarea-BDInZ0Bq.js";import"./trash-2-Bj69O07h.js";const X=[{value:"active",label:"Active"},{value:"all",label:"All"},{value:"suspended",label:"Suspended"},{value:"revoked",label:"Revoked"}];function Y(s){switch(s){case"active":return"default";case"suspended":return"secondary";case"revoked":return"destructive";default:return"outline"}}function Z(s){return s.match_thumbprint?`thumb:${s.match_thumbprint}`:s.match_sub&&s.match_iss?`sub:${s.match_sub} (iss:${s.match_iss})`:s.match_sub?`sub:${s.match_sub}`:"—"}function ee(s){return s.capabilities.length?s.capabilities.map(i=>i.op).join(", "):"0 ops"}function ve(){var g;const[s]=C(),[i,b]=n.useState(()=>{const t=s.get("status");return t==="active"||t==="suspended"||t==="revoked"||t==="all"?t:"active"}),[u,j]=n.useState(()=>s.get("q")??""),[d,l]=n.useState(!1),[v,m]=n.useState(null),a=V({status:i,q:u.trim()||void 0}),o=U(),h=n.useMemo(()=>{const t=s.get("promote");return t!=="1"&&t!=="true"?null:{sub:s.get("sub"),iss:s.get("iss"),thumbprint:s.get("thumbprint"),label:s.get("label")}},[s]),c=((g=a.data)==null?void 0:g.grants)??[],y=[{header:"Label",accessorKey:"label",cell:({row:t})=>e.jsx(x,{to:`/agents/grants/${encodeURIComponent(t.original.grant_id)}`,className:"font-medium text-foreground hover:underline",children:t.original.label})},{header:"Status",accessorKey:"status",cell:({row:t})=>e.jsx(f,{variant:Y(t.original.status),children:t.original.status})},{header:"Identity",id:"identity",cell:({row:t})=>e.jsx("span",{className:"break-all font-mono text-xs",children:Z(t.original)})},{header:"Capabilities",id:"capabilities",cell:({row:t})=>ee(t.original)},{header:"Last used",accessorKey:"last_used_at",cell:({getValue:t})=>k(t()??void 0)},{header:"Source",accessorKey:"import_source",cell:({getValue:t})=>{const r=t();return r?e.jsx(f,{variant:"outline",children:r}):"—"}}];async function S(t){m(null);try{await o.mutateAsync(t),l(!1)}catch(r){const A=r instanceof Error?r.message:"Failed to create grant";m(A)}}return h&&!d&&!o.isSuccess&&l(!0),e.jsxs(I,{title:"Agent grants",titleIcon:e.jsx(D,{className:"h-5 w-5"}),description:e.jsxs("span",{children:["First-class records that admit a verified AAuth identity as the grant's owning user. Every grant is its own entity (entity_type"," ",e.jsx("code",{className:"font-mono",children:"agent_grant"}),") so observation history doubles as the audit log."]}),actions:e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[H(a)?e.jsx(M,{}):null,e.jsx(p,{asChild:!0,variant:"ghost",size:"sm",children:e.jsxs(x,{to:"/agents",children:[e.jsx(J,{className:"mr-1 h-4 w-4"}),"Agents"]})}),e.jsxs(B,{open:d,onOpenChange:l,children:[e.jsx(F,{asChild:!0,children:e.jsxs(p,{size:"sm",children:[e.jsx(W,{className:"mr-1 h-4 w-4"}),"New grant"]})}),e.jsxs(K,{className:"max-w-2xl",children:[e.jsxs(O,{children:[e.jsx($,{children:"Create agent grant"}),e.jsx(q,{children:"Admit one AAuth identity as your user with the listed capabilities."})]}),e.jsx(G,{identityHint:h??void 0,submitLabel:"Create grant",isSubmitting:o.isPending,errorMessage:v,showCancel:!0,onCancel:()=>l(!1),onSubmit:t=>S(t)})]})]})]}),children:[e.jsxs("div",{className:"flex flex-wrap items-end gap-3",children:[e.jsx(w,{placeholder:"Search by label, subject, thumbprint…",value:u,onChange:t=>j(t.target.value),className:"w-[320px]"}),e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Status"}),e.jsxs(Q,{value:i,onValueChange:t=>b(t),children:[e.jsx(T,{className:"w-[160px]",children:e.jsx(E,{})}),e.jsx(L,{children:X.map(t=>e.jsx(R,{value:t.value,children:t.label},t.value))})]})]}),a.data&&e.jsxs("p",{className:"text-sm text-muted-foreground",children:[c.length," grant",c.length===1?"":"s"]})]}),z(a)?e.jsx(N,{rows:8,cols:6}):a.error?e.jsx(_,{title:"Could not load agent grants",children:a.error.message}):e.jsx(P,{columns:y,data:c})]})}export{ve as default};
