import{c as I}from"./chunk-NBZO6GJA.js";import{d as B}from"./chunk-KBCGLXAJ.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-L6W7X4TF.js";import{a as m,g as L,i as u}from"./chunk-WH5WJFMZ.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:ae7e6707fc51a9e1f6f015929aece8afe43a92a094f09f07f0463afe6c89573f*/
//# sourceMappingURL=chunk-PCPGWUNW.js.map
