import{a9 as D,P,D as j,a8 as M,g as p,r as u,a6 as U}from"./index-B4jeNr7A.js";const R=Symbol("userRecruitment");function g(){const r=P(),m=j(),o=u([]),s=u([]),S=u(!1),y=u(null),l=u(null),i=u(null),h=p(()=>o.value.length),w=p(()=>o.value.length+s.value.length);async function v(){S.value=!0,y.value=null;try{const e=r.allServerDomains(),[n,d]=await Promise.all([Promise.allSettled(e.map(async t=>{const a=await r.clientForServer(t).getMyRecruitmentInvites();return{domain:t,invites:a.invites}})),Promise.allSettled(e.map(async t=>{const a=await r.clientForServer(t).getMyRecruitmentRequests();return{domain:t,requests:a.requests}}))]),f=[];for(const t of n)if(t.status==="fulfilled")for(const a of t.value.invites)f.push({...a,sourceServer:t.value.domain});const c=[];for(const t of d)if(t.status==="fulfilled")for(const a of t.value.requests)c.push({...a,sourceServer:t.value.domain});o.value=f,s.value=c}catch(e){y.value=e instanceof Error?e.message:"Failed to load invitations",o.value=[],s.value=[]}finally{S.value=!1}}async function q(e){i.value=e.id,l.value=null;try{await r.clientForServer(e.sourceServer).updateMyRecruitmentInvite(e.id,{state:"ACCEPTED"}),await m.loadCommunities(),await v()}catch(n){l.value=n instanceof Error?n.message:"Failed to accept invitation"}finally{i.value=null}}async function E(e){i.value=e.id,l.value=null;try{await r.clientForServer(e.sourceServer).updateMyRecruitmentInvite(e.id,{state:"REJECTED"}),await v()}catch(n){l.value=n instanceof Error?n.message:"Failed to reject invitation"}finally{i.value=null}}async function F(e){i.value=e.id,l.value=null;try{await r.clientForServer(e.sourceServer).deleteMyRecruitmentRequest(e.id),await v()}catch(n){l.value=n instanceof Error?n.message:"Failed to withdraw request"}finally{i.value=null}}async function I(e){i.value=e.id,l.value=null;try{const d=await r.clientForCommunity(e,m.currentUser?.homeServer).createCommunityRecruitmentRequest(e.id),f=M(r.resolveServerDomain(e.homeServer,m.currentUser?.homeServer)??e.homeServer),c=s.value.findIndex(a=>a.id===d.id),t={...d,sourceServer:f};c>=0?s.value=s.value.map((a,C)=>C===c?t:a):s.value=[...s.value,t]}catch(n){l.value=n instanceof Error?n.message:"Failed to request to join"}finally{i.value=null}}return{invites:o,requests:s,loading:S,error:y,actionError:l,actionInProgressId:i,pendingInviteCount:h,pendingCount:w,load:v,acceptInvite:q,rejectInvite:E,withdrawRequest:F,requestJoin:I}}function A(){const r=g();return U(R,r),r}function J(){const r=D(R,null);return r||g()}export{A as p,J as u};
//# sourceMappingURL=useUserRecruitment-CVnGe1PI.js.map
