import{o as e}from"./rolldown-runtime-BM3Ffeng.js";import{t}from"./chevron-left-Deqsuxdc.js";import{Gy as n,Hy as r,Jd as i,Ly as a,Nf as o,Ua as s,Zv as c,ag as l,dg as u,dv as d,hv as f,i as p,ig as m,jg as h,kp as g,lh as _,m_ as v,mv as y,pv as b,rg as x,ty as S,uv as C}from"./index-DDwwBb9e.js";import{t as w}from"./SettingsLayout-DkhRoxt1.js";var T=e(n()),E=r();function D({children:e}){let t=g(),n=_.user.isAdmin.useQuery({},{retry:!1,refetchOnWindowFocus:!1}),r=n.data?.isAdmin===!0,i=n.isSuccess&&!r||n.isError;return(0,T.useEffect)(()=>{i&&t.push(`/`)},[i,t]),r?(0,E.jsx)(w,{children:e}):(0,E.jsx)(w,{children:(0,E.jsx)(C,{paddingY:20,children:(0,E.jsxs)(u,{gap:3,children:[!i&&(0,E.jsx)(f,{size:`lg`}),(0,E.jsx)(c,{color:`fg.muted`,fontSize:`sm`,children:i?`Access denied`:`Loading Backoffice…`})]})})})}function O({title:e,searchValue:t,onSearchChange:n,searchPlaceholder:r=`Search`,pagination:o,isLoading:l,isFetching:m,error:g,onCreate:_,createLabel:y=`Create`,children:x}){return(0,E.jsxs)(u,{gap:6,width:`full`,align:`start`,children:[(0,E.jsxs)(v,{width:`full`,children:[(0,E.jsx)(S,{children:e}),(0,E.jsx)(h,{}),_&&(0,E.jsxs)(s.HeaderButton,{onClick:_,children:[(0,E.jsx)(i,{size:20}),y]})]}),(0,E.jsx)(p,{value:t,onChange:e=>n(e.target.value),placeholder:r,width:`full`,maxWidth:`480px`}),(0,E.jsx)(b,{width:`full`,overflow:`hidden`,children:(0,E.jsx)(d,{paddingY:0,paddingX:0,children:g?(0,E.jsx)(a,{paddingY:10,paddingX:4,children:(0,E.jsx)(c,{color:`red.500`,fontSize:`sm`,children:g.message})}):l?(0,E.jsx)(a,{paddingY:10,textAlign:`center`,children:(0,E.jsx)(f,{size:`md`})}):(0,E.jsxs)(a,{position:`relative`,width:`full`,overflow:`auto`,children:[m&&(0,E.jsx)(a,{position:`absolute`,top:2,right:2,zIndex:1,color:`fg.muted`,children:(0,E.jsx)(f,{size:`xs`})}),x]})})}),o&&o.total>0&&(0,E.jsx)(k,{...o})]})}function k({page:e,perPage:n,total:r,onPageChange:i}){let a=Math.max(1,Math.ceil(r/n)),s=e>1,l=e<a;return(0,E.jsxs)(v,{width:`full`,justify:`end`,gap:4,children:[(0,E.jsxs)(c,{fontSize:`sm`,color:`fg.muted`,children:[r===0?0:(e-1)*n+1,`–`,Math.min(r,e*n),` of `,r]}),(0,E.jsxs)(v,{gap:1,children:[(0,E.jsx)(y,{size:`sm`,variant:`outline`,disabled:!s,onClick:()=>i(e-1),children:(0,E.jsx)(t,{size:14})}),(0,E.jsx)(y,{size:`sm`,variant:`outline`,disabled:!l,onClick:()=>i(e+1),children:(0,E.jsx)(o,{size:14})})]})]})}function A({children:e}){return(0,E.jsx)(c,{color:`fg.muted`,fontSize:`sm`,children:e??`—`})}function j(e){if(!e)return`—`;let t=typeof e==`string`?new Date(e):e;return Number.isNaN(t.getTime())?`—`:t.toLocaleDateString()}function M(e){if(!e)return`—`;let t=typeof e==`string`?new Date(e):e;return Number.isNaN(t.getTime())?`—`:`${t.toLocaleDateString()} ${t.toLocaleTimeString()}`}function N(e){if(!e)return null;let t=e.split(`-`).map(Number);if(t.length!==3||t.some(e=>Number.isNaN(e)))return null;let[n,r,i]=t;return new Date(n,r-1,i,12,0,0,0).toISOString()}async function P(e,t,n){let r=await fetch(`/api/admin/${e}`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({resource:e,method:t,params:n}),credentials:`include`});if(!r.ok){let n=await r.text().catch(()=>r.statusText);throw Error(`Admin ${e}/${t} failed (${r.status}): ${n}`)}return await r.json()}var F={getList(e,t){let{pagination:n={page:1,perPage:25},sort:r={field:`id`,order:`ASC`},filter:i={}}=t;return P(e,`getList`,{pagination:n,sort:r,filter:i})},getOne(e,t){return P(e,`getOne`,{id:t})},update(e,t,n){return P(e,`update`,{id:t,data:n})},create(e,t){return P(e,`create`,{data:t})}};async function I({userIdToImpersonate:e,reason:t}){let n=await fetch(`/api/admin/impersonate`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({userIdToImpersonate:e,reason:t}),credentials:`include`});if(!n.ok){let e=await n.text().catch(()=>n.statusText);throw Error(`Impersonation failed (${n.status}): ${e}`)}}var L=e=>[`backoffice`,e],R=(e,t)=>[...L(e),`list`,t];function z(e,t,n){return m({queryKey:R(e,t),queryFn:()=>F.getList(e,t),keepPreviousData:!0,...n})}function B(e,t){let n=l();return x({mutationFn:({id:t,data:n})=>F.update(e,t,n),...t,onSuccess:async(r,i,a)=>{await n.invalidateQueries({queryKey:L(e)}),t?.onSuccess?.(r,i,a)}})}function V(e,t){let n=l();return x({mutationFn:t=>F.create(e,t),...t,onSuccess:async(r,i,a)=>{await n.invalidateQueries({queryKey:L(e)}),t?.onSuccess?.(r,i,a)}})}export{O as a,j as c,I as i,M as l,z as n,A as o,B as r,N as s,V as t,D as u};
//# sourceMappingURL=useAdminResource-DhVGZyTo.js.map