import{c as I}from"./chunk-SGCHCWLM.js";import{d as B}from"./chunk-5TE7477A.js";import{Bf as l,C as n,Qf as U,R as v,T as S,Z as x,Zg as T,ba as h,d as E,f as A,ih as O,k as f,nd as b,nh as g,o as p,qd as y,w as R,y as d}from"./chunk-NY4TI4KT.js";import{a as m,g as L,i as u}from"./chunk-IQVXR75E.js";var j=L(T());var C=(0,j.default)("peertube:subscriptions:UserSubscriptionService"),s=class s{authHttp=h(y);restExtractor=h(O);videoService=h(I);restService=h(g);existsSubject=new A(1);existsObservable;myAccountSubscriptionCache={};myAccountSubscriptionCacheObservable={};myAccountSubscriptionCacheSubject=new E;constructor(){this.existsObservable=R(U({time:200,notifierObservable:this.existsSubject,bulkGet:this.doSubscriptionsExist.bind(this)}).pipe(p(t=>t.response)),this.myAccountSubscriptionCacheSubject)}listFollowers(t){let{pagination:e,nameWithHost:i,search:c,channel:a}=t,r=new b;r=this.restService.addRestGetParams(r,this.restService.componentToRestPagination(e),"-createdAt"),c&&(r=r.append("search",c));let o=a?`${s.BASE_VIDEO_CHANNELS_URL}/${a}/followers`:`${s.BASE_ACCOUNTS_URL}/${i}/followers`;return this.authHttp.get(o,{params:r}).pipe(n(_=>this.restExtractor.handleError(_)))}getUserSubscriptionVideos(t){let{videoPagination:e,sort:i,skipCount:c}=t,a=this.restService.componentToRestPagination(e),r=new b;return r=this.restService.addRestGetParams(r,a,i),c&&(r=r.set("skipCount",c+"")),this.authHttp.get(s.BASE_USER_SUBSCRIPTIONS_URL+"/videos",{params:r}).pipe(v(o=>this.videoService.extractVideos(o)),n(o=>this.restExtractor.handleError(o)))}deleteSubscription(t){let e=s.BASE_USER_SUBSCRIPTIONS_URL+"/"+t;return this.authHttp.delete(e).pipe(S(()=>{this.myAccountSubscriptionCache[t]=!1,this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)}),n(i=>this.restExtractor.handleError(i)))}addSubscription(t){let e=s.BASE_USER_SUBSCRIPTIONS_URL,i={uri:t};return this.authHttp.post(e,i).pipe(S(()=>{this.myAccountSubscriptionCache[t]=!0,this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)}),n(c=>this.restExtractor.handleError(c)))}listSubscriptions(t){let{pagination:e,search:i}=t,c=s.BASE_USER_SUBSCRIPTIONS_URL,a=this.restService.componentToRestPagination(e),r=new b;return r=this.restService.addRestGetParams(r,a),i&&(r=r.append("search",i)),this.authHttp.get(c,{params:r}).pipe(p(o=>B.extractVideoChannels(o)),n(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(d(i=>i[t]!==void 0),p(i=>i[t]));return this.myAccountSubscriptionCacheObservable[t]=e,e}doesSubscriptionExist(t){return C("Running subscription check for "+t),t in this.myAccountSubscriptionCache?(C("Found cache for "+t),f(this.myAccountSubscriptionCache[t])):(this.existsSubject.next(t),C("Fetching from network for "+t),this.existsObservable.pipe(d(e=>e[t]!==void 0),p(e=>e[t]),S(e=>this.myAccountSubscriptionCache[t]=e)))}doSubscriptionsExist(t){let e=s.BASE_USER_SUBSCRIPTIONS_URL+"/exist",i=new b;return i=this.restService.addObjectParams(i,{uris:t}),this.authHttp.get(e,{params:i}).pipe(S(c=>{this.myAccountSubscriptionCache=m(m({},this.myAccountSubscriptionCache),c)}),n(c=>this.restExtractor.handleError(c)))}};u(s,"BASE_USER_SUBSCRIPTIONS_URL",l.apiUrl+"/api/v1/users/me/subscriptions"),u(s,"BASE_VIDEO_CHANNELS_URL",l.apiUrl+"/api/v1/video-channels"),u(s,"BASE_ACCOUNTS_URL",l.apiUrl+"/api/v1/accounts"),u(s,"\u0275fac",function(e){return new(e||s)}),u(s,"\u0275prov",x({token:s,factory:s.\u0275fac}));var P=s;export{P as a};
/**i18n:347eeb695970c32bc275618edc2e751c67145d9fc3012e70543c114a99b9e016*/
//# sourceMappingURL=chunk-BQ5TNB4S.js.map
