import {g,aS as kn,aT as no,aC as x,aU as QP,aV as oe,fO as Ue,by as X,e0 as V1,fP as EHe,aN as $,aW as ln,aX as le,a9 as Oe,br as lt,dl as Ie,bs as M,ao as D,aY as S,bL as J,bM as wo,dF as z}from'./main-DEZ5cZzu.js';var I=J(wo());var d=(0, I.default)("peertube:subscriptions:UserSubscriptionService"),B=class c{authHttp=g(kn);restExtractor=g(no);videoService=g(x);restService=g(QP);static BASE_USER_SUBSCRIPTIONS_URL=oe.apiUrl+"/api/v1/users/me/subscriptions";static BASE_VIDEO_CHANNELS_URL=oe.apiUrl+"/api/v1/video-channels";static BASE_ACCOUNTS_URL=oe.apiUrl+"/api/v1/accounts";existsSubject=new Ue(1);existsObservable;myAccountSubscriptionCache={};myAccountSubscriptionCacheObservable={};myAccountSubscriptionCacheSubject=new X;constructor(){this.existsObservable=V1(EHe({time:200,notifierObservable:this.existsSubject,bulkGet:this.doSubscriptionsExist.bind(this)}).pipe($(t=>t.response)),this.myAccountSubscriptionCacheSubject);}listFollowers(t){let{pagination:e,nameWithHost:i,search:s,channel:a}=t,r=new ln;r=this.restService.addRestGetParams(r,this.restService.componentToRestPagination(e),"-createdAt"),s&&(r=r.append("search",s));let o=a?`${c.BASE_VIDEO_CHANNELS_URL}/${a}/followers`:`${c.BASE_ACCOUNTS_URL}/${i}/followers`;return this.authHttp.get(o,{params:r}).pipe(le(P=>this.restExtractor.handleError(P)))}getUserSubscriptionVideos(t){let{videoPagination:e,sort:i,skipCount:s}=t,a=this.restService.componentToRestPagination(e),r=new ln;return r=this.restService.addRestGetParams(r,a,i),s&&(r=r.set("skipCount",s+"")),this.authHttp.get(c.BASE_USER_SUBSCRIPTIONS_URL+"/videos",{params:r}).pipe(Oe(o=>this.videoService.extractVideos(o)),le(o=>this.restExtractor.handleError(o)))}deleteSubscription(t){let e=c.BASE_USER_SUBSCRIPTIONS_URL+"/"+t;return this.authHttp.delete(e).pipe(lt(()=>{this.myAccountSubscriptionCache[t]=false,this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache);}),le(i=>this.restExtractor.handleError(i)))}addSubscription(t){let e=c.BASE_USER_SUBSCRIPTIONS_URL,i={uri:t};return this.authHttp.post(e,i).pipe(lt(()=>{this.myAccountSubscriptionCache[t]=true,this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache);}),le(s=>this.restExtractor.handleError(s)))}listSubscriptions(t){let{pagination:e,search:i}=t,s=c.BASE_USER_SUBSCRIPTIONS_URL,a=this.restService.componentToRestPagination(e),r=new ln;return r=this.restService.addRestGetParams(r,a),i&&(r=r.append("search",i)),this.authHttp.get(s,{params:r}).pipe($(o=>z.extractVideoChannels(o)),le(o=>this.restExtractor.handleError(o)))}listenToMyAccountSubscriptionCacheSubject(){return this.myAccountSubscriptionCacheSubject.asObservable()}listenToSubscriptionCacheChange(t){if(t in this.myAccountSubscriptionCacheObservable)return this.myAccountSubscriptionCacheObservable[t];let e=this.existsObservable.pipe(Ie(i=>i[t]!==void 0),$(i=>i[t]));return this.myAccountSubscriptionCacheObservable[t]=e,e}doesSubscriptionExist(t){return d("Running subscription check for "+t),t in this.myAccountSubscriptionCache?(d("Found cache for "+t),M(this.myAccountSubscriptionCache[t])):(this.existsSubject.next(t),d("Fetching from network for "+t),this.existsObservable.pipe(Ie(e=>e[t]!==void 0),$(e=>e[t]),lt(e=>this.myAccountSubscriptionCache[t]=e)))}doSubscriptionsExist(t){let e=c.BASE_USER_SUBSCRIPTIONS_URL+"/exist",i=new ln;return i=this.restService.addObjectParams(i,{uris:t}),this.authHttp.get(e,{params:i}).pipe(lt(s=>{this.myAccountSubscriptionCache=D(D({},this.myAccountSubscriptionCache),s);}),le(s=>this.restExtractor.handleError(s)))}static \u0275fac=function(e){return new(e||c)};static \u0275prov=S({token:c,factory:c.\u0275fac})};/**i18n:c8855ca17fb20171aa9dd43d012e42547cb1f0cfd9c21d4dc05679bbbdf5ccb8*/export{B};//# sourceMappingURL=chunk-C7leSDo_.js.map
