import{eU as e,b as n,x as s,w as a}from"./ca9c256b9-7brFE8T2.js";import{r as i,k as r}from"./vendor--leCAzSo.js";const t=e=>n=>{if(!n)return;const s=n.pages.length-1;return{...n,pages:n.pages.map((n,a)=>e(n,a===s))}},o=(e,n)=>e[n]??[],c=(e,n,s)=>({...e,[n]:s}),u=(e,n)=>t((s,a)=>a?c(s,e,[...o(s,e),n]):s),l=(e,n)=>t(s=>c(s,e,o(s,e).filter(e=>!n(e)))),p=(e,n)=>t(s=>c(s,e,o(s,e).map(n))),g=(n,s)=>{const{items:a,queryKey:r,enabled:t,pageParamName:o,nextPageField:c}=s,{data:u,isPending:l,isError:p,hasNextPage:g,isFetchingNextPage:d,fetchNextPage:b,refetch:f}=e(n,{queryKey:r,pageParamName:o??"page_token",nextPageField:c??"next_page_token",enabled:t});i.useEffect(()=>{!g||d||p||b({cancelRefetch:!1})},[g,d,p,b]);const m=i.useMemo(()=>u&&!g?u.pages.flatMap(a):void 0,[u,g,a]);return{data:m,isPending:void 0===m,isLoading:!(!t||!l&&(!g||p)),isError:p,refetch:f}},d="role_assignments",b="account_bindings",f=e=>e.assignments??[];async function m(e,n,s,a){const i=[];let r;do{if(a?.current)return null;const t=new URLSearchParams;t.set("page_size",String(100)),r&&t.set("page_token",r);const o=await e(`/api/organizations/${n}/roles/${s}/assignments?${t.toString()}`,{});if(!o.ok||a?.current)return null;const c=await o.json();i.push(...c.assignments??[]),r=c.next_page_token||void 0}while(r);return i}const y=(e,s)=>{const{activeOrganization:a}=n(),i=a?.uuid;return g(`/api/organizations/${i}/roles/${e}/assignments?page_size=100`,{queryKey:[d,"by_role",i,e],enabled:!!i&&!!e&&(s?.enabled??!0),items:f})},_=(e,a)=>{const{activeOrganization:r}=n(),t=r?.uuid,o=s("Query error"),c=!!t&&(a?.enabled??!0),[u,l]=i.useState(null),[p,g]=i.useState(0),d=i.useRef(new Set),b=i.useRef(0),f=i.useRef(0),m=i.useCallback(async e=>{const n=[];for(let i=0;i<e.length;i+=100)n.push(e.slice(i,i+100));const s=await Promise.all(n.map(async e=>await o(`/api/organizations/${t}/rbac/account-bindings:query`,{method:"POST",body:JSON.stringify({account_uuids:e})})));if(1===s.length)return s[0];const a={bindings:[],groups:{},roles:{}};for(const i of s)a.bindings.push(...i.bindings),Object.assign(a.groups,i.groups),Object.assign(a.roles,i.roles);return a},[o,t]),y=i.useRef(void 0);i.useEffect(()=>{const n=e[0];void 0!==y.current&&y.current!==n&&(f.current++,b.current=0,d.current=new Set,g(0),l(null)),y.current=n},[e]),i.useEffect(()=>{if(!c)return;const n=e.slice(b.current);if(0===n.length)return;b.current=e.length;const s=f.current;for(const e of n){const n=e.filter(e=>!d.current.has(e));if(0===n.length)continue;const a=()=>{for(const e of n)d.current.add(e);g(d.current.size)},i=e=>{s===f.current&&(a(),l(n=>n?{bindings:[...n.bindings,...e.bindings],groups:{...n.groups,...e.groups},roles:{...n.roles,...e.roles}}:e))},r=()=>{s===f.current&&a()};m(n).then(i).catch(r)}},[c,e,m]);const _=i.useCallback(e=>p>0&&d.current.has(e),[p]);return{bindingsData:u,bindingsLoadedCount:p,hasMemberBindings:_}},h=(e,s)=>{const{activeOrganization:a}=n(),i=a?.uuid;return g(`/api/organizations/${i}/role-assignments?principal_id=${e}&page_size=100`,{queryKey:[d,"by_principal",i,e],enabled:!!i&&!!e&&(s?.enabled??!0),items:f})},z=(e={})=>{const{activeOrganization:s}=n(),i=s?.uuid,t=r();return a(e=>`/api/organizations/${i}/roles/${e.roleUuid}/assignments`,"POST",{enabled:!!i,transformVariables:e=>({principal_type:e.principal_type,principal_id:e.principal_id}),...e,onSuccess(n,s,a){const r=u("assignments",n);t.setQueryData([d,"by_role",i,s.roleUuid],r),t.setQueryData([d,"by_principal",i,s.principal_id],r),e.onSuccess?.(n,s,a)}})},$=(e={})=>{const{activeOrganization:s}=n(),i=s?.uuid,t=r();return a(e=>`/api/organizations/${i}/roles/${e.roleUuid}/assignments/${e.principalId}?principal_type=${e.principalType}`,"DELETE",{enabled:!!i,...e,onSuccess(n,s,a){const r=e=>l("assignments",e);t.setQueryData([d,"by_role",i,s.roleUuid],r(e=>e.principal_id===s.principalId)),t.setQueryData([d,"by_principal",i,s.principalId],r(e=>e.role_uuid===s.roleUuid)),e.onSuccess?.(n,s,a)}})},P="role_permissions",S=e=>e.permissions??[],v=(e,s)=>{const{activeOrganization:a}=n(),i=a?.uuid;return g(`/api/organizations/${i}/roles/${e}/permissions?page_size=100`,{queryKey:[P,i,e],enabled:!!i&&!!e&&(s?.enabled??!0),items:S})},O=(e,n,s,a)=>{e.setQueryData([P,n,s],e=>({pages:[{permissions:a(e?.pages.flatMap(e=>e.permissions??[])),next_page_token:""}],pageParams:[void 0]}))};export{b as A,d as R,P as a,h as b,z as c,$ as d,v as e,m as f,y as g,g as h,u as i,p as j,l as r,O as s,_ as u};
