import{h as e,I as a,Z as s,aw as i,aV as r}from"./c6eedc03a-CsEFTfjy.js";import{u as n}from"./vendor-query-BwvgHhJ1.js";import{r as t}from"./vendor-react-Dp9pL_Wn.js";import{b0 as o,b1 as u,b2 as g,b3 as c}from"./index-CBEPphRm.js";const p="groups_list",m="group",d=e=>e.groups??[],b=s=>{const{activeOrganization:i}=e(),r=i?.uuid,n=a();return o(`/api/organizations/${r}/groups?page_size=1000`,{queryKey:[p,r,"all"],enabled:!!r&&n&&(s?.enabled??!0),items:d})},l=(i,r)=>{const{activeOrganization:n}=e(),t=n?.uuid,o=a();return s(`/api/organizations/${t}/groups/${i}`,{queryKey:[m,t,i],enabled:!!t&&!!i&&o&&(r?.enabled??!0)})},z=(a={})=>{const{activeOrganization:s}=e(),r=s?.uuid,t=n();return i(`/api/organizations/${r}/groups`,"POST",{enabled:!!r,...a,onSuccess(e,s,i){t.setQueryData([p,r,"all"],g("groups",e)),a.onSuccess?.(e,s,i)}})},$=(a,s={})=>{const{activeOrganization:r}=e(),t=r?.uuid,o=n();return i(`/api/organizations/${t}/groups/${a}`,"PUT",{enabled:!!t&&!!a,...s,onSuccess(e,i,r){o.setQueryData([p,t,"all"],c("groups",s=>s.group_uuid===a?e:s)),o.setQueryData([m,t,a],e),s.onSuccess?.(e,i,r)}})},y=(a,s={})=>{const{activeOrganization:r}=e(),t=r?.uuid,o=n();return i(`/api/organizations/${t}/groups/${a}`,"DELETE",{enabled:!!t&&!!a,...s,onSuccess(e,i,r){o.setQueryData([p,t,"all"],u("groups",e=>e.group_uuid===a)),o.removeQueries({queryKey:[m,t,a]}),s.onSuccess?.(e,i,r)}})},_="group_members",S=1e3,v=(i,r)=>{const{activeOrganization:n}=e(),t=n?.uuid,o=a(),u=new URLSearchParams;r?.pageSize&&u.set("page_size",String(r.pageSize));const g=u.toString();return s(`/api/organizations/${t}/groups/${i}/members${g?`?${g}`:""}`,{queryKey:[_,t,i,r?.pageSize],enabled:!!t&&!!i&&o&&(r?.enabled??!0),...void 0!==r?.staleTime&&{staleTime:r.staleTime}})},f=(s,i)=>{const{activeOrganization:n}=e(),o=n?.uuid,u=a(),g=`/api/organizations/${o}/groups/${s}/members?page_size=${i?.pageSize??1e3}`,{data:c,isLoading:p,hasNextPage:m,fetchNextPage:d,isFetchingNextPage:b}=r(g,{queryKey:[_,o,s,"all"],pageParamName:"page_token",nextPageField:"next_page_token",enabled:!!o&&!!s&&u&&(i?.enabled??!0)});return{data:t.useMemo(()=>{if(!c)return;const e=c.pages.flatMap(e=>e.members??[]),a=c.pages[c.pages.length-1];return{members:e,next_page_token:a?.next_page_token??""}},[c]),isLoading:p,hasNextPage:m,fetchNextPage:d,isFetchingNextPage:b}};function h(e,a){if(!e||0===a.length||"pages"in e)return e;const s=new Set(a.map(e=>e.member_id));return{...e,members:[...(e.members??[]).filter(e=>!s.has(e.member_id)),...a]}}const O=(a={})=>{const{activeOrganization:s}=e(),r=s?.uuid;return i(e=>`/api/organizations/${r}/groups/${e.groupUuid}/members:batchAdd`,"POST",{enabled:!!r,transformVariables:e=>({member_ids:e.member_ids}),...a})},P=(a,s={})=>{const{activeOrganization:r}=e(),t=r?.uuid,o=n();return i(e=>`/api/organizations/${t}/groups/${a}/members/${e.memberId}`,"DELETE",{enabled:!!t&&!!a,...s,transformVariables:()=>{},onSuccess(e,i,r){o.setQueriesData({queryKey:[_,t,a]},e=>function(e,a){if(e)return"pages"in e?{...e,pages:e.pages.map(e=>({...e,members:a(e.members??[])}))}:{...e,members:a(e.members??[])}}(e,e=>e.filter(e=>e.member_id!==i.memberId))),s.onSuccess?.(e,i,r)}})},x="group_visibility",D=["discover","share_with","view_members"],T=(i,r)=>{const{activeOrganization:n}=e(),t=n?.uuid,o=a();return s(`/api/organizations/${t}/groups/${i}/visibility`,{queryKey:[x,t,i],enabled:!!t&&!!i&&o&&(r?.enabled??!0)})},q=(a={})=>{const{activeOrganization:s}=e(),r=s?.uuid,t=n();return i(e=>`/api/organizations/${r}/groups/${e.groupUuid}/visibility`,"PUT",{enabled:!!r,transformVariables:({policies:e})=>({policies:e}),...a,onSuccess(e,s,i){t.setQueryData([x,r,s.groupUuid],e),a.onSuccess?.(e,s,i)}})},U=100,E=(a={})=>{const{activeOrganization:s}=e(),r=s?.uuid,t=n();return i(`/api/organizations/${r}/groups:batchSetVisibility`,"PUT",{enabled:!!r,transformVariables:({groupUuids:e,policies:a})=>({group_uuids:e,policies:a}),...a,onSuccess(e,s,i){for(const a of e.results)t.setQueryData([x,r,a.group_uuid],{policies:a.policies});a.onSuccess?.(e,s,i)}})},Q=(a={})=>{const{activeOrganization:s}=e(),r=s?.uuid,t=n();return i(e=>`/api/organizations/${r}/groups/${e.groupUuid}/project_shares`,"DELETE",{enabled:!!r,transformVariables:()=>({}),...a,onSuccess(e,s,i){t.setQueryData([j,r,s.groupUuid],{count:0}),a.onSuccess?.(e,s,i)}})},j="group_project_shares",K=(i,r)=>{const{activeOrganization:n}=e(),t=n?.uuid,o=a();return s(`/api/organizations/${t}/groups/${i}/project_shares`,{queryKey:[j,t,i],enabled:!!t&&!!i&&o&&(r?.enabled??!0)})};export{U as B,_ as G,p as a,m as b,l as c,v as d,D as e,T as f,q as g,E as h,z as i,$ as j,f as k,O as l,P as m,Q as n,K as o,S as p,h as q,y as r,b as u};
