import{u as K}from"./useUserRecruitment-CJpxhdxR.js";import{ad as T,P as U,D as z,X as E,ac as v,g as l,r as d,aa as I}from"./index-BhMpD06x.js";const b=Symbol("communityBrowser");function D(t){return[...t].sort((u,m)=>{const o=u.name.localeCompare(m.name,void 0,{sensitivity:"base"});return o!==0?o:u.id.localeCompare(m.id)})}function J(t,u,m,o){return u.has(t.id)?"member":m.some(i=>i.community.id===t.id)?"accept-invite":o.some(i=>i.community.id===t.id)?"cancel-request":t.allowsJoinRequests?"join":"none"}function H(t,u,m,o){return u==="accept-invite"?m.find(i=>i.community.id===t.id)?.id??t.id:u==="cancel-request"?o.find(i=>i.community.id===t.id)?.id??t.id:t.id}function g(){const t=U(),u=z(),{communities:m}=E(u),o=K(),i=d([]),c=d(null),y=d(!1),p=d(null),f=l(()=>v(t.homeDomain)),S=l(()=>new Set(m.value.map(e=>e.id)));function q(e){return o.invites.value.find(n=>n.community.id===e)??null}function B(e){return o.requests.value.find(n=>n.community.id===e)??null}function R(e){return J(e,S.value,o.invites.value,o.requests.value)}function F(e){return v(e.homeServer)!==f.value}const j=l(()=>{const e=new Map;for(const n of i.value){const a=v(n.homeServer),r=e.get(a)??[];r.push(n),e.set(a,r)}return e}),w=l(()=>t.allServerDomains().map(e=>{const n=v(e);return{serverDomain:n,isRemote:n!==f.value,label:n===f.value?"This server":n,communities:D(j.value.get(n)??[])}})),h=l(()=>w.value.find(e=>e.serverDomain===c.value)??null),x=l(()=>h.value?.communities??[]);function A(e){c.value=v(e)}function M(){c.value&&w.value.some(e=>e.serverDomain===c.value)||(c.value=f.value)}async function P(e){const n=new Map;for(const r of o.invites.value)e.has(r.community.id)||n.set(r.community.id,r.community);for(const r of o.requests.value)e.has(r.community.id)||n.set(r.community.id,r.community);const a=[];return await Promise.all([...n.values()].map(async r=>{try{const C=await t.clientForCommunity(r,u.currentUser?.homeServer).getCommunity(r.id);a.push(C)}catch{}})),a}async function k(){y.value=!0,p.value=null;try{await o.load();const e=await Promise.allSettled(t.allServerDomains().map(async s=>(await t.clientForServer(s).getCommunities()).communities)),n=[];for(const s of e)s.status==="fulfilled"&&n.push(...s.value);const a=new Map;for(const s of n)a.set(s.id,s);const r=await P(new Set(a.keys()));for(const s of r)a.set(s.id,s);i.value=D([...a.values()]),M()}catch(e){p.value=e instanceof Error?e.message:"Failed to load communities",i.value=[]}finally{y.value=!1}}return{communities:i,selectedServerDomain:c,loading:y,error:p,membershipIds:S,servers:w,selectedServer:h,communitiesForSelectedServer:x,inviteForCommunity:q,requestForCommunity:B,getRowAction:R,isRemoteCommunity:F,selectServer:A,load:k}}function L(){const t=g();return I(b,t),t}function N(){const t=T(b,null);return t||g()}export{H as a,L as p,J as r,N as u};
//# sourceMappingURL=useCommunityBrowser-B0bzLltT.js.map
