import{ma as Y,mW as Z,km as C,dZ as O,jO as D,jH as W,lo as q,mM as R,es as z,ea as E,k9 as d,h1 as V,g0 as B,e3 as G,hv as S,br as H,dn as U,lq as F,aJ as K}from"./BQCsJfSh.js";import{bO as J}from"./DURqPrI0.js";import{n as Q}from"./CSZ2C-U5.js";import{L}from"./RrweKcqb.js";const X=({relays:f,filters:p,element:v,onBackwardExhausted:u,onForwardExhausted:h,at:c=d()})=>{const b=new AbortController,_=J([]),g=new Set;let i=S(6,H),a=[],y=[c-i,c],m=[c,c+i];const I=e=>{for(let t=0;t<a.length;t++)if(a[t].created_at<e.created_at){a.splice(t,0,e);return}a.push(e)},w=e=>{const t=[];for(const s of e)g.has(s.id)||(g.add(s.id),G([y[0],m[1]],s.created_at)?t.push(s):I(s));t.length>0&&(t.sort((s,n)=>s.created_at-n.created_at),_.update(s=>{const n=[];let r=0,l=0;for(;r<s.length&&l<t.length;)s[r].created_at<=t[l].created_at?n.push(s[r++]):n.push(t[l++]);for(;r<s.length;)n.push(s[r++]);for(;l<t.length;)n.push(t[l++]);return n}))},M=()=>{const e=a.splice(0,30);for(const t of e)g.delete(t.id);w(e)},N=O(150,e=>{const t=[];for(const s of new Set(e)){const n=q.getEvent(s);n&&W(p,n)&&t.push(n)}t.length>0&&w(t)}),A=[C(q,"update",O(150,e=>{const{added:t,removed:s}=D(e);if(s.size>0){a=a.filter(r=>!s.has(r.id)),_.update(r=>r.filter(l=>!s.has(l.id)));for(const r of s)g.delete(r)}const n=t.filter(r=>W(p,r)&&f.some(l=>R.getRelays(r.id).has(l)));n.length>0&&w(n)})),C(R,"add",(e,t)=>{f.includes(t)&&N(e)})],k=async(e,t)=>{(await F({relays:f,autoClose:!0,signal:b.signal,filters:p.map(n=>({...n,since:e,until:t}))})).length===0?i=Math.round(i*1.1):i=S(H)},T=z({element:v,delay:300,threshold:5e3,onScroll:async()=>{const[e,t]=y;y=[e-i,e],M(),t>d()-S(2,U)?await k(e,t):a.some(s=>s.created_at<c)||(T.stop(),u==null||u())}}),o=z({element:v,reverse:!0,delay:300,threshold:5e3,onScroll:async()=>{const[e,t]=m;m=[t,t+i],M(),t<d()?await k(e,t):a.some(s=>s.created_at>c)||(o.stop(),h==null||h())}});for(const e of f)w(L(e,p));return{events:_,cleanup:()=>{b.abort(),o.stop(),T.stop(),A.forEach(E)}}},$=({relays:f,filters:p,element:v,onExhausted:u})=>{const h=S(5,H),c=new AbortController,b=new Set;let _=0,g=[d()-h,d()],i=[d(),d()+h];const a=o=>parseInt(V("start",o.tags)||""),y=o=>parseInt(V("end",o.tags)||""),m=J(Y(a,Z(o=>o.id,f.flatMap(o=>Array.from(L(o,p)))))),I=o=>{const e=o.filter(t=>!isNaN(a(t))&&!isNaN(y(t))&&!b.has(t.id));if(e.length!==0){for(const t of e)b.add(t.id);e.sort((t,s)=>a(t)-a(s)),m.update(t=>{const s=new Set(e.map(B)),n=t.filter(P=>!s.has(B(P))),r=[];let l=0,j=0;for(;l<n.length&&j<e.length;)a(n[l])<=a(e[j])?r.push(n[l++]):r.push(e[j++]);for(;l<n.length;)r.push(n[l++]);for(;j<e.length;)r.push(e[j++]);return r})}},w=O(150,o=>{const e=[];for(const t of new Set(o)){const s=q.getEvent(t);s&&W(p,s)&&e.push(s)}e.length>0&&I(e)}),M=[C(q,"update",O(150,o=>{const{added:e,removed:t}=D(o);if(t.size>0){m.update(n=>n.filter(r=>!t.has(r.id)));for(const n of t)b.delete(n)}const s=e.filter(n=>W(p,n));s.length>0&&I(s)})),C(R,"add",(o,e)=>{f.includes(e)&&w(o)})],N=(o,e)=>{const t=Q(o,e).map(String);F({relays:f,autoClose:!0,signal:c.signal,filters:[{kinds:[K],"#D":t}]})},A=()=>{++_===2&&(u==null||u())},k=z({element:v,reverse:!0,onScroll:()=>{const[o,e]=g;g=[o-h,o],e>d()-S(2,U)?N(o,e):(k.stop(),A())}}),T=z({element:v,onScroll:()=>{const[o,e]=i;i=[e,e+h],e<d()+S(2,U)?N(o,e):(T.stop(),A())}});return{events:m,cleanup:()=>{c.abort(),T.stop(),k.stop(),M.forEach(E)}}},ne=Object.freeze(Object.defineProperty({__proto__:null,makeCalendarFeed:$,makeFeed:X},Symbol.toStringTag,{value:"Module"}));export{ne as a,X as b,$ as m};
//# sourceMappingURL=CKTDATTP.js.map
