import{r as e}from"./rolldown-runtime-hePW80VL.js";import{n as t,t as n}from"./jsx-runtime-DE3RlOCf.js";import{t as r}from"./useQuery-DxCFSWSJ.js";import{n as i}from"./apiClient-CXiRj2uz.js";import{a,i as o,l as s,u as c}from"./useAuth-CTys35u0.js";import{t as l}from"./client-a7uKC1qR.js";var u=e(t(),1),d=e(n(),1),f=(0,u.createContext)(void 0);function p({children:e}){let{data:t,isLoading:n}=o(),{data:r,isLoading:l}=a(),u=s(),p=c(),m=e=>r?.permissions?.includes(e)??!1,h=e=>r?.roles?.some(t=>t.role_name===e)??!1,g=t?.is_staff||t?.is_superuser||!1,_={user:t??null,permissions:r??null,isAuthenticated:!!t&&i.hasValidToken(),isLoading:n,login:u.mutate,logout:p.mutate,hasPermission:m,hasRole:h,isAdmin:g};return(0,d.jsx)(f.Provider,{value:_,children:e})}function m(){let e=(0,u.useContext)(f);if(e===void 0)throw Error(`useAuth must be used within an AuthProvider`);return e}var h={canCreate:!0,canRead:!0,canUpdate:!0,canDelete:!0,canList:!0,isLoading:!1},g={canCreate:!0,canRead:!0,canUpdate:!0,canDelete:!0,canList:!0,isLoading:!0};function _(e){return{canCreate:e.can_create,canRead:e.can_read,canUpdate:e.can_update,canDelete:e.can_delete,canList:e.can_list,isLoading:!1}}function v(e,t){return{canCreate:e.canCreate||t.canCreate,canRead:e.canRead||t.canRead,canUpdate:e.canUpdate||t.canUpdate,canDelete:e.canDelete||t.canDelete,canList:e.canList||t.canList,isLoading:!1}}function y(e,t){let{isAdmin:n,permissions:i,isLoading:a}=m(),o=t?.screenId,s=t?.schemaName??e,c=i?.roles?.map(e=>e.id)??[],{data:u,isLoading:d}=r({queryKey:[`screen-perms`,o??s],queryFn:async()=>{let e={page_size:200};return o?e.screen=o:s&&(e.search=s),((await l.get(`/api/v1/auth/object-permissions/`,{params:e}))?.results??[]).filter(e=>c.includes(e.role))},enabled:c.length>0&&(!!o||!!s),staleTime:3e5});if(n)return h;if(a||d)return g;if(c.length===0)return h;let f=u??[];return f.length===0?h:f.filter(e=>e.is_active!==!1).reduce((e,t)=>{let n=_(t);return e?v(e,n):n},null)||h}export{p as n,m as r,y as t};