import{r as e}from"./chunk-DECur_0Z.js";import{a as t}from"./auth-store-GibZbQPL.js";import{t as n}from"./router-DlQvj2uD.js";import{t as r}from"./react-CO2uhaBc.js";import{$r as i,Ai as a,Ci as o,Di as s,Do as c,Ei as l,Ga as u,Gr as d,Jr as f,Kr as p,Mi as m,Ni as h,Oi as g,Pi as ee,Pp as te,Qr as ne,Si as re,Ti as ie,Wr as _,Xr as v,Yr as ae,Zr as y,_i as oe,_p as b,ai as x,bi as se,ci as S,di as ce,ei as C,fi as le,gi as ue,gp as w,hi as de,ii as T,ji as fe,jr as E,ki as D,kp as O,li as k,mi as pe,ni as me,oi as A,os as he,pi as ge,qr as _e,ri as j,si as ve,ti as M,ui as N,vi as ye,wi as be,xi as xe,yi as Se,zf as Ce}from"./messages-d1hRBHLP.js";import{r as P}from"./route-Bea3HGvi.js";import{o as F,s as I,z as L}from"./search-provider-Zrg-SPXk.js";import{t as R}from"./button-CbdgUDvS.js";import{t as we}from"./confirm-dialog-CIBixfaW.js";import{a as z,i as B,n as Te,r as V,t as Ee}from"./select-Cv_sRi68.js";import{t as De}from"./separator-CE12xF6y.js";import{t as Oe}from"./switch-BE0OlU-S.js";import{t as H}from"./createLucideIcon-Br0Bd5k2.js";import{t as U}from"./skeleton-DwLAyCLM.js";import{n as ke,t as Ae}from"./copy-to-clipboard--eZunBzP.js";import{t as je}from"./eye-off-CNvoUNxR.js";import{t as Me}from"./eye-CnTTVkb2.js";import{n as Ne,r as Pe,t as Fe}from"./empty-state-0yHuPXxG.js";import{n as Ie,t as Le}from"./main-A2K7nB2i.js";import{n as Re,t as ze}from"./trash-2-BBsEOAHm.js";import{t as Be}from"./shield-check-CiWq1V4z.js";import{i as Ve,o as He,r as Ue,s as We,t as Ge}from"./dialog-SgRLLGoQ.js";import{n as W}from"./dist-Bk35TVDN.js";import{c as G,s as Ke}from"./zod-CQemhjci.js";import{a as K,c as qe,i as q,l as Je,n as J,o as Y,r as Ye,s as X,t as Xe}from"./form-Db7d571a.js";import{t as Z}from"./input-CusCjhBW.js";import{t as Ze}from"./page-header-DkeFH2Rh.js";import{t as Qe}from"./textarea-DIBKWAfY.js";import{t as $e}from"./badge-3Uy55c0q.js";var et=H(`arrow-down-a-z`,[[`path`,{d:`m3 16 4 4 4-4`,key:`1co6wj`}],[`path`,{d:`M7 20V4`,key:`1yoxec`}],[`path`,{d:`M20 8h-5`,key:`1vsyxs`}],[`path`,{d:`M15 10V6.5a2.5 2.5 0 0 1 5 0V10`,key:`ag13bf`}],[`path`,{d:`M15 14h5l-5 6h5`,key:`ur5jdg`}]]),tt=H(`arrow-up-a-z`,[[`path`,{d:`m3 8 4-4 4 4`,key:`11wl7u`}],[`path`,{d:`M7 4v16`,key:`1glfcx`}],[`path`,{d:`M20 8h-5`,key:`1vsyxs`}],[`path`,{d:`M15 10V6.5a2.5 2.5 0 0 1 5 0V10`,key:`ag13bf`}],[`path`,{d:`M15 14h5l-5 6h5`,key:`ur5jdg`}]]),nt=H(`pencil`,[[`path`,{d:`M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z`,key:`1a8usu`}],[`path`,{d:`m15 5 4 4`,key:`1mk7zo`}]]),rt=e(Ae(),1),Q=e(r(),1),$=te(),it=Ke({name:G().min(1,_()),notify_url:G().optional(),ip_whitelist:G().optional()});function at({open:e,onOpenChange:n,currentRow:r,onSuccess:a}){let o=Je({resolver:qe(it),defaultValues:{name:``,notify_url:``,ip_whitelist:``}}),s=t.useMutation(`post`,`/admin/api/v1/api-keys`),c=t.useMutation(`patch`,`/admin/api/v1/api-keys/{id}`),l=s.data?.data?.secret_key;(0,Q.useEffect)(()=>{e&&o.reset({name:r?.name??``,notify_url:r?.notify_url??``,ip_whitelist:r?.ip_whitelist??``})},[e,r,o]);function u(e){e||(o.reset({name:``,notify_url:``,ip_whitelist:``}),s.reset(),c.reset()),n(e)}function m(e){if(r?.id){c.mutate({params:{path:{id:r.id}},body:{name:e.name,notify_url:e.notify_url,ip_whitelist:e.ip_whitelist||void 0}},{onSuccess:()=>{W.success(p()),n(!1),a?.()}});return}s.mutate({body:{name:e.name,notify_url:e.notify_url,ip_whitelist:e.ip_whitelist||void 0}},{onSuccess:()=>{W.success(d()),a?.()}})}function h(){u(!1)}let g=s.isPending||c.isPending;return(0,$.jsx)(Ge,{onOpenChange:u,open:e,children:(0,$.jsxs)(Ue,{children:[(0,$.jsxs)(He,{children:[(0,$.jsx)(We,{children:r?M():C()}),(0,$.jsx)(Ve,{children:r?i():ne()})]}),(0,$.jsx)(Xe,{...o,children:(0,$.jsxs)(`form`,{className:`space-y-4`,onSubmit:o.handleSubmit(m),children:[(0,$.jsx)(q,{control:o.control,name:`name`,render:({field:e})=>(0,$.jsxs)(K,{children:[(0,$.jsx)(Y,{children:j()}),(0,$.jsx)(J,{children:(0,$.jsx)(Z,{placeholder:me(),...e})}),(0,$.jsx)(X,{})]})}),(0,$.jsx)(q,{control:o.control,name:`notify_url`,render:({field:e})=>(0,$.jsxs)(K,{children:[(0,$.jsxs)(`div`,{className:`flex items-center justify-between gap-2`,children:[(0,$.jsx)(Y,{children:S()}),(0,$.jsx)($e,{variant:`secondary`,children:b()})]}),(0,$.jsx)(J,{children:(0,$.jsx)(Z,{disabled:!0,placeholder:`https://example.com/callback`,...e})}),(0,$.jsx)(Ye,{children:w()}),(0,$.jsx)(X,{})]})}),(0,$.jsx)(q,{control:o.control,name:`ip_whitelist`,render:({field:e})=>(0,$.jsxs)(K,{children:[(0,$.jsxs)(`div`,{className:`flex items-center justify-between gap-2`,children:[(0,$.jsx)(Y,{children:y()}),(0,$.jsx)($e,{variant:`secondary`,children:b()})]}),(0,$.jsx)(J,{children:(0,$.jsx)(Qe,{disabled:!0,placeholder:`127.0.0.1 10.0.0.2`,...e})}),(0,$.jsx)(Ye,{children:w()}),(0,$.jsx)(X,{})]})}),!r&&l?(0,$.jsxs)(`div`,{className:`rounded-lg border bg-muted/40 p-3 text-sm`,children:[(0,$.jsx)(`div`,{className:`mb-1 font-medium text-amber-600`,children:v()}),(0,$.jsx)(`div`,{className:`break-all font-mono text-muted-foreground`,children:l})]}):null,(0,$.jsxs)(`div`,{className:`flex justify-end gap-2`,children:[(0,$.jsx)(R,{onClick:h,type:`button`,variant:`outline`,children:!r&&l?ae():O()}),(!l||r)&&(0,$.jsxs)(R,{disabled:g,type:`submit`,children:[g&&E(),!g&&r&&f(),!(g||r)&&_e()]})]})]})})]})})}function ot({data:e,isLoading:t,onAction:n,onCopySecret:r,onToggleSecret:i,secretStates:a}){let[o,s]=(0,Q.useState)(new Set);function l(e){try{return`${new URL(e).protocol}//••••••••`}catch{return`••••••••••••••••`}}function d(e){return e?.visible?e.loading?k():e.value||`-`:`••••••••••••••••`}function f(e,t){return e?t?e:l(e):`-`}return t?(0,$.jsx)(`ul`,{className:`faded-bottom no-scrollbar grid gap-4 overflow-auto pt-4 pb-16 md:grid-cols-2 lg:grid-cols-3`,children:[`api-key-skeleton-1`,`api-key-skeleton-2`,`api-key-skeleton-3`,`api-key-skeleton-4`,`api-key-skeleton-5`,`api-key-skeleton-6`].map(e=>(0,$.jsx)(`li`,{"aria-hidden":`true`,className:`rounded-lg border`,children:(0,$.jsxs)(`div`,{className:`flex h-full flex-col overflow-hidden p-4`,children:[(0,$.jsxs)(`div`,{className:`mb-6 flex items-center justify-between gap-3`,children:[(0,$.jsxs)(`div`,{className:`min-w-0 flex-1 space-y-2`,children:[(0,$.jsx)(U,{className:`h-5 w-32 max-w-full`}),(0,$.jsx)(U,{className:`h-3 w-20`})]}),(0,$.jsx)(U,{className:`h-6 w-11 rounded-full`})]}),(0,$.jsxs)(`div`,{className:`mb-6 flex-1 space-y-3`,children:[(0,$.jsxs)(`div`,{className:`flex gap-2`,children:[(0,$.jsx)(U,{className:`mt-1 h-3 w-12 shrink-0`}),(0,$.jsx)(U,{className:`h-4 w-28 min-w-0 flex-1`})]}),(0,$.jsxs)(`div`,{className:`flex gap-2`,children:[(0,$.jsx)(U,{className:`mt-1 h-3 w-12 shrink-0`}),(0,$.jsxs)(`div`,{className:`flex min-w-0 flex-1 items-start gap-2`,children:[(0,$.jsxs)(`div`,{className:`min-w-0 flex-1 space-y-2`,children:[(0,$.jsx)(U,{className:`h-4 w-full`}),(0,$.jsx)(U,{className:`h-4 w-9/12`})]}),(0,$.jsx)(U,{className:`size-8 shrink-0 rounded-md`}),(0,$.jsx)(U,{className:`size-8 shrink-0 rounded-md`})]})]}),[`w-28`,`w-full`,`w-11/12`,`w-24`,`w-32`].map(e=>(0,$.jsxs)(`div`,{className:`flex gap-2`,children:[(0,$.jsx)(U,{className:`mt-1 h-3 w-12 shrink-0`}),(0,$.jsx)(U,{className:`h-4 min-w-0 flex-1 ${e}`})]},e))]}),(0,$.jsx)(`div`,{className:`-mx-4 mt-auto -mb-4 border-t`,children:(0,$.jsx)(`div`,{className:`grid grid-cols-3`,children:[`api-key-action-skeleton-1`,`api-key-action-skeleton-2`,`api-key-action-skeleton-3`].map((e,t)=>(0,$.jsx)(`div`,{className:`flex h-11 items-center justify-center px-4 ${t<2?`border-r`:``}`,children:(0,$.jsx)(U,{className:`h-4 w-16`})},e))})})]})},e))}):e.length?(0,$.jsx)(`ul`,{className:`faded-bottom no-scrollbar grid gap-4 overflow-auto pt-4 pb-16 md:grid-cols-2 lg:grid-cols-3`,children:e.map(e=>{let t=e.status===1,l=String(e.id??e.pid??``),p=a[l],m=o.has(l),h=[{label:`PID`,value:e.pid??`-`},{label:A(),value:d(p),visible:p?.visible,onToggle:()=>i(e),onCopy:()=>r(e)},{label:S(),value:f(e.notify_url,m),visible:m,onToggle:e.notify_url?()=>s(e=>{let t=new Set(e);return t.has(l)?t.delete(l):t.add(l),t}):void 0},{label:ve(),value:e.ip_whitelist||`-`},{label:ge(),value:I(e.call_count??0,0)},{label:le(),value:F(e.last_used_at)||ce()},{label:c(),value:F(e.updated_at)||`-`}];return(0,$.jsx)(`li`,{className:`rounded-lg border hover:shadow-md`,children:(0,$.jsxs)(`div`,{className:`flex h-full flex-col overflow-hidden p-4`,children:[(0,$.jsxs)(`div`,{className:`mb-6 flex items-center justify-between`,children:[(0,$.jsx)(`div`,{className:`min-w-0`,children:(0,$.jsx)(`h2`,{className:`truncate font-semibold`,children:e.name||e.pid||`#${e.id}`})}),(0,$.jsx)(Oe,{checked:t,onCheckedChange:()=>n(t?`disable`:`enable`,e)})]}),(0,$.jsxs)(`div`,{className:`mb-6 flex-1 space-y-2 text-muted-foreground text-sm`,children:[(0,$.jsxs)(`div`,{className:`flex gap-2`,children:[(0,$.jsx)(`span`,{className:`w-16 shrink-0 text-foreground/80`,children:`PID`}),(0,$.jsx)(`span`,{className:`min-w-0 flex-1 break-all`,children:e.pid??`-`})]}),h.filter(e=>e.label!==`PID`).map(e=>(0,$.jsxs)(`div`,{className:`flex gap-2`,children:[(0,$.jsx)(`span`,{className:`w-16 shrink-0 text-foreground/80`,children:e.label}),(0,$.jsxs)(`div`,{className:`flex min-w-0 flex-1 items-start gap-2`,children:[(0,$.jsx)(`span`,{className:`min-w-0 flex-1 break-all`,children:e.value}),e.onCopy&&(0,$.jsx)(R,{className:`size-8 shrink-0`,onClick:e.onCopy,size:`icon-sm`,variant:`ghost`,children:(0,$.jsx)(ke,{className:`h-4.5 w-4.5`})}),e.onToggle&&(0,$.jsx)(R,{className:`size-8 shrink-0`,onClick:e.onToggle,size:`icon-sm`,variant:`ghost`,children:e.visible?(0,$.jsx)(je,{className:`h-4.5 w-4.5`}):(0,$.jsx)(Me,{className:`h-4.5 w-4.5`})})]})]},e.label))]}),(0,$.jsx)(`div`,{className:`-mx-4 mt-auto -mb-4 border-t`,children:(0,$.jsxs)(`div`,{className:`grid grid-cols-3`,children:[(0,$.jsxs)(R,{className:`h-11 w-full rounded-none border-r px-0 text-muted-foreground hover:bg-muted/60 hover:text-foreground`,onClick:()=>n(`edit`,e),size:`sm`,variant:`ghost`,children:[(0,$.jsx)(nt,{className:`h-4 w-4`}),x()]}),(0,$.jsxs)(R,{className:`h-11 w-full rounded-none border-r px-0 text-muted-foreground hover:bg-muted/60 hover:text-foreground`,onClick:()=>n(`rotate-secret`,e),size:`sm`,variant:`ghost`,children:[(0,$.jsx)(L,{className:`h-4 w-4`}),T()]}),(0,$.jsxs)(R,{className:`h-11 w-full rounded-none px-0 text-destructive hover:bg-destructive/8 hover:text-destructive`,onClick:()=>n(`delete`,e),size:`sm`,variant:`ghost`,children:[(0,$.jsx)(ze,{className:`h-4 w-4 text-destructive`}),u()]})]})})]})},e.id??e.pid)})}):(0,$.jsx)(`div`,{className:`pt-4`,children:(0,$.jsx)(Fe,{description:N()})})}var st=P(`/_authenticated/keys/`),ct=new Map([[`all`,ee()],[`enabled`,h()],[`disabled`,m()]]);function lt(){let{filter:e=``,type:r=`all`,sort:i=`asc`}=st.useSearch(),c=st.useNavigate(),u=t.useQuery(`get`,`/admin/api/v1/api-keys`),d=t.useMutation(`delete`,`/admin/api/v1/api-keys/{id}`),f=t.useMutation(`post`,`/admin/api/v1/api-keys/{id}/status`),p=t.useMutation(`get`,`/admin/api/v1/api-keys/{id}/secret`),te=t.useMutation(`post`,`/admin/api/v1/api-keys/{id}/rotate-secret`),ne=t.useMutation(`get`,`/admin/api/v1/api-keys/{id}/stats`),[_,v]=(0,Q.useState)(!1),[ae,y]=(0,Q.useState)(null),[b,x]=(0,Q.useState)(null),[S,C]=(0,Q.useState)(null),[w,T]=(0,Q.useState)({}),[E,O]=(0,Q.useState)(i),[k,me]=(0,Q.useState)(r),[A,_e]=(0,Q.useState)(e);async function j(){await n.invalidateQueries({queryKey:[`get`,`/admin/api/v1/api-keys`]})}async function ve(){if(!b?.row.id)return;let e=b.row.id;b.action===`delete`?(await d.mutateAsync({params:{path:{id:e}}}),W.success(fe())):(b.action===`enable`||b.action===`disable`)&&(await f.mutateAsync({params:{path:{id:e}},body:{status:b.action===`enable`?1:2}}),W.success(he())),await j(),x(null)}function M(e){return String(e.id??e.pid??``)}async function N(e,t){let n=M(e),r=t?.visible??w[n]?.visible??!1;T(e=>({...e,[n]:{loading:!0,value:e[n]?.value??``,visible:r}}));try{let t=await p.mutateAsync({params:{path:{id:e.id}}}),i=typeof t.data==`object`&&t.data&&`secret_key`in t.data?String(t.data.secret_key??``):``;return T(e=>({...e,[n]:{loading:!1,value:i||`-`,visible:r}})),i||`-`}catch{return T(e=>({...e,[n]:{loading:!1,value:e[n]?.value??``,visible:r}})),W.error(a()),null}}async function P(e){if(!e.id){W.error(D());return}let t=M(e),n=w[t];if(n?.visible){T(e=>({...e,[t]:{...n,visible:!1}}));return}if(n?.value){T(e=>({...e,[t]:{...n,visible:!0}}));return}await N(e,{visible:!0})}async function F(e){if(!e.id){W.error(D());return}let t=M(e),n=w[t]?.value??``;if(n||=await N(e,{visible:w[t]?.visible??!1})??``,!n||n===`-`){W.error(g());return}(0,rt.default)(n),W.success(s())}async function L(e){let t=await te.mutateAsync({params:{path:{id:e.id}}}),n=M(e);T(e=>({...e,[n]:{loading:!1,value:t.data?.secret_key??`-`,visible:!0}})),W.success(l()),await j()}async function Oe(e){let t=await ne.mutateAsync({params:{path:{id:e.id}}});C({apiKey:e.pid??`#${e.id}`,stats:t.data??{}})}async function H(e,t){if(!t.id){W.error(D());return}if(e===`edit`){y(t),v(!0);return}if(e===`delete`){x({action:e,row:t});return}switch(e){case`enable`:case`disable`:await f.mutateAsync({params:{path:{id:t.id}},body:{status:e===`enable`?1:2}}),W.success(he()),await j();break;case`rotate-secret`:await L(t);break;case`view-stats`:await Oe(t);break;default:break}}let U=[...u.data?.data??[]].sort((e,t)=>{let n=(e.name??e.pid??`#${e.id??``}`).toLowerCase(),r=(t.name??t.pid??`#${t.id??``}`).toLowerCase();return E===`asc`?n.localeCompare(r):r.localeCompare(n)}).filter(e=>k===`enabled`?e.status===1:k===`disabled`?e.status!==1:!0).filter(e=>{let t=A.trim().toLowerCase();return t?(e.pid??``).toLowerCase().includes(t)||(e.name??``).toLowerCase().includes(t)||(e.notify_url??``).toLowerCase().includes(t):!0});return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(Ie,{}),(0,$.jsxs)(Le,{fixed:!0,children:[(0,$.jsx)(Ze,{actions:(0,$.jsxs)(R,{onClick:()=>{y(null),v(!0)},children:[(0,$.jsx)(Re,{className:`mr-2 h-4 w-4`}),ie()]}),description:be(),title:o()}),(0,$.jsxs)(`div`,{className:`my-4 flex items-end justify-between sm:my-0 sm:items-center`,children:[(0,$.jsxs)(`div`,{className:`flex flex-col gap-4 sm:my-4 sm:flex-row`,children:[(0,$.jsx)(Z,{className:`h-9 w-40 lg:w-62.5`,onChange:e=>{_e(e.target.value),c({search:t=>({...t,filter:e.target.value||void 0})})},placeholder:re(),value:A}),(0,$.jsxs)(Ee,{onValueChange:e=>{me(e),c({search:t=>({...t,type:e===`all`?void 0:e})})},value:k,children:[(0,$.jsx)(B,{className:`w-36`,children:(0,$.jsx)(z,{children:ct.get(k)})}),(0,$.jsxs)(Te,{children:[(0,$.jsx)(V,{value:`all`,children:ee()}),(0,$.jsx)(V,{value:`enabled`,children:h()}),(0,$.jsx)(V,{value:`disabled`,children:m()})]})]})]}),(0,$.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,$.jsxs)(R,{onClick:j,variant:`outline`,children:[(0,$.jsx)(Pe,{className:`mr-2 h-4 w-4`}),Ce()]}),(0,$.jsxs)(Ee,{onValueChange:e=>{O(e),c({search:t=>({...t,sort:e})})},value:E,children:[(0,$.jsx)(B,{className:`w-16`,children:(0,$.jsx)(z,{children:(0,$.jsx)(Ne,{size:18})})}),(0,$.jsxs)(Te,{align:`end`,children:[(0,$.jsx)(V,{value:`asc`,children:(0,$.jsxs)(`div`,{className:`flex items-center gap-4`,children:[(0,$.jsx)(tt,{size:16}),(0,$.jsx)(`span`,{children:xe()})]})}),(0,$.jsx)(V,{value:`desc`,children:(0,$.jsxs)(`div`,{className:`flex items-center gap-4`,children:[(0,$.jsx)(et,{size:16}),(0,$.jsx)(`span`,{children:se()})]})})]})]})]})]}),(0,$.jsx)(De,{className:`shadow-sm`}),(0,$.jsx)(ot,{data:U,isLoading:u.isLoading,onAction:H,onCopySecret:F,onToggleSecret:P,secretStates:w})]}),(0,$.jsx)(at,{currentRow:ae,onOpenChange:e=>{v(e),e||y(null)},onSuccess:()=>u.refetch(),open:_}),b&&(0,$.jsx)(we,{confirmText:b.action===`delete`?Se():ye(),desc:(0,$.jsx)(`span`,{children:oe({target:String(b.row.pid??b.row.id??``),action:b.action})}),destructive:b.action===`delete`,handleConfirm:ve,onOpenChange:()=>x(null),open:!0,title:b.action===`delete`?ue():de()}),(0,$.jsx)(Ge,{onOpenChange:()=>C(null),open:!!S,children:(0,$.jsxs)(Ue,{children:[(0,$.jsxs)(He,{children:[(0,$.jsxs)(We,{className:`flex items-center gap-2`,children:[(0,$.jsx)(Be,{className:`h-4 w-4`}),pe()]}),(0,$.jsx)(Ve,{children:S?.apiKey})]}),(0,$.jsxs)(`div`,{className:`grid gap-4 sm:grid-cols-2`,children:[(0,$.jsxs)(`div`,{className:`rounded-lg border p-4`,children:[(0,$.jsxs)(`div`,{className:`mb-2 flex items-center gap-2 text-muted-foreground text-sm`,children:[(0,$.jsx)(Me,{className:`h-4 w-4`}),` `,ge()]}),(0,$.jsx)(`div`,{className:`font-semibold text-2xl`,children:I(S?.stats.call_count??0,0)})]}),(0,$.jsxs)(`div`,{className:`rounded-lg border p-4`,children:[(0,$.jsxs)(`div`,{className:`mb-2 flex items-center gap-2 text-muted-foreground text-sm`,children:[(0,$.jsx)(Pe,{className:`h-4 w-4`}),` `,le()]}),(0,$.jsx)(`div`,{className:`font-medium text-sm`,children:S?.stats.last_used_at??ce()})]})]})]})})]})}var ut=lt;export{ut as component};