import{o as n}from"./rolldown-runtime-iuO8RvI4.js";import{At as e}from"./vendor-all-0-fRSUBfCr.js";import{Hm as s,Ll as r,zm as i}from"./shared-0-C4kdVF7Y.js";import{Mi as a}from"./vendor-frame-CwiylG4Y.js";import{d as t,f as o}from"./shared-3-D-Ux4aGn.js";import{k as c}from"./shared-2-CMUBhYOn.js";var u=n(a()),l="role_assignments",p="account_bindings",d=n=>n.assignments??[];async function g(n,e,s,r){const i=[];let a;do{if(r?.current)return null;const t=new URLSearchParams;t.set("page_size",String(100)),a&&t.set("page_token",a);const o=await n(`/api/organizations/${e}/roles/${s}/assignments?${t.toString()}`,{});if(!o.ok||r?.current)return null;const c=await o.json();i.push(...c.assignments??[]),a=c.next_page_token||void 0}while(a);return i}var f=(n,e)=>{const{activeOrganization:s}=r(),i=s?.uuid;return c(`/api/organizations/${i}/roles/${n}/assignments?page_size=100`,{queryKey:[l,"by_role",i,n],enabled:!!i&&!!n&&(e?.enabled??!0),items:d})},m=(n,e)=>{const{activeOrganization:i}=r(),a=i?.uuid,t=s("Query error"),o=!!a&&(e?.enabled??!0),[c,l]=(0,u.useState)(null),[p,d]=(0,u.useState)(0),g=(0,u.useRef)(new Set),f=(0,u.useRef)(new Set),m=(0,u.useRef)(0),b=(0,u.useRef)(0),y=(0,u.useCallback)(async n=>{const e=[];for(let i=0;i<n.length;i+=100)e.push(n.slice(i,i+100));const s=await Promise.all(e.map(async n=>await t(`/api/organizations/${a}/rbac/account-bindings:query`,{method:"POST",body:JSON.stringify({account_uuids:n})})));if(1===s.length)return s[0];const r={bindings:[],groups:{},roles:{}};for(const i of s)r.bindings.push(...i.bindings),Object.assign(r.groups,i.groups),Object.assign(r.roles,i.roles);return r},[t,a]),_=(0,u.useRef)(void 0);return(0,u.useEffect)(()=>{const e=n[0];void 0!==_.current&&_.current!==e&&(b.current++,m.current=0,g.current=new Set,f.current=new Set,d(0),l(null)),_.current=e},[n]),(0,u.useEffect)(()=>{if(!o)return;const e=n.slice(m.current);if(0===e.length)return;m.current=n.length;const s=b.current;for(const n of e){const e=n.filter(n=>!g.current.has(n));if(0===e.length)continue;const r=()=>{for(const n of e)g.current.add(n);d(g.current.size)},i=n=>{s===b.current&&(r(),l(e=>e?{bindings:[...e.bindings,...n.bindings],groups:{...e.groups,...n.groups},roles:{...e.roles,...n.roles}}:n))},a=()=>{if(s===b.current){for(const n of e)f.current.add(n);r()}};y(e).then(i).catch(a)}},[o,n,y]),{bindingsData:c,bindingsLoadedCount:p,hasMemberBindings:(0,u.useCallback)(n=>p>0&&g.current.has(n),[p]),isBindingsFailed:(0,u.useCallback)(n=>p>0&&f.current.has(n),[p])}},b=(n,e)=>{const{activeOrganization:s}=r(),i=s?.uuid;return c(`/api/organizations/${i}/role-assignments?principal_id=${n}&page_size=100`,{queryKey:[l,"by_principal",i,n],enabled:!!i&&!!n&&(e?.enabled??!0),items:d})},y=(n={})=>{const{activeOrganization:s}=r(),a=s?.uuid,o=e();return i(n=>`/api/organizations/${a}/roles/${n.roleUuid}/assignments`,"POST",{enabled:!!a,transformVariables:n=>({principal_type:n.principal_type,principal_id:n.principal_id}),...n,onSuccess(e,s,r){const i=t("assignments",e);o.setQueryData([l,"by_role",a,s.roleUuid],i),o.setQueryData([l,"by_principal",a,s.principal_id],i),n.onSuccess?.(e,s,r)}})},_=(n={})=>{const{activeOrganization:s}=r(),a=s?.uuid,t=e();return i(n=>`/api/organizations/${a}/roles/${n.roleUuid}/assignments/${n.principalId}?principal_type=${n.principalType}`,"DELETE",{enabled:!!a,...n,onSuccess(e,s,r){const i=n=>o("assignments",n);t.setQueryData([l,"by_role",a,s.roleUuid],i(n=>n.principal_id===s.principalId)),t.setQueryData([l,"by_principal",a,s.principalId],i(n=>n.role_uuid===s.roleUuid)),n.onSuccess?.(e,s,r)}})};export{m as a,_ as c,y as i,l as n,b as o,g as r,f as s,p as t};