import{a as e,s as t,x as n}from"./useAuthor-K0SVHIbr.js";import{u as r}from"./esm-0y5H0J9W.js";import{t as i}from"./useMutation-B3AB1JC6.js";import{t as a}from"./useNostrPublish-D4pB8G4O.js";import{t as o}from"./admins-7SV8oI_M.js";var s=`agora-organizers`,c=`pathos-organizers`;function l(e){return e.length===0?null:e.reduce((e,t)=>t.created_at>e.created_at?t:e)}function u(e){if(!e)return[];let t=[];for(let n of e.tags)n[0]===`p`&&n[1]&&n[3]&&t.push({pubkey:n[1],countryCode:n[3].toUpperCase()});return t}function d(){let{nostr:d}=e(),f=n(),{mutateAsync:p}=a(),{data:m,isLoading:h}=t({queryKey:[`organizers`],queryFn:async e=>{let t=AbortSignal.any([e.signal,AbortSignal.timeout(5e3)]),n=l(await d.query([{kinds:[30078],authors:o,"#d":[s,c],limit:50}],{signal:t}));return{organizers:u(n),lastUpdatedBy:n?.pubkey??``,lastUpdatedAt:n?.created_at??0}},staleTime:6e4}),g=m?.organizers??[];async function _(e){let t=l(await d.query([{kinds:[30078],authors:o,"#d":[s,c],limit:50}],{signal:AbortSignal.timeout(1e4)})),n=e(u(t)).map(e=>[`p`,e.pubkey,``,e.countryCode.toUpperCase()]);return p({kind:30078,content:``,tags:[[`d`,s],[`alt`,`Agora organizer appointments`],...n],prev:t??void 0})}return{organizers:g,isLoading:h,addOrganizer:i({mutationFn:async({npub:e,countryCode:t})=>{let n;try{n=r.decode(e).data}catch{throw Error(`Invalid npub format`)}let i=t.toUpperCase();return await _(e=>e.some(e=>e.pubkey===n&&e.countryCode.toUpperCase()===i)?e:[...e,{pubkey:n,countryCode:i}]),{pubkey:n,countryCode:i}},onSuccess:()=>{f.invalidateQueries({queryKey:[`organizers`]})}}),removeOrganizer:i({mutationFn:async({npub:e,countryCode:t})=>{let n;try{n=r.decode(e).data}catch{throw Error(`Invalid npub format`)}let i=t.toUpperCase();await _(e=>e.filter(e=>e.pubkey!==n||e.countryCode.toUpperCase()!==i))},onSuccess:()=>{f.invalidateQueries({queryKey:[`organizers`]})}}),getOrganizersByCountry:e=>{let t=e.toUpperCase();return g.filter(e=>e.countryCode.toUpperCase()===t)},isOrganizer:(e,t)=>{if(t){let n=t.toUpperCase();return g.some(t=>t.pubkey===e&&t.countryCode.toUpperCase()===n)}return g.some(t=>t.pubkey===e)}}}export{d as t};