import{r as C,s as w}from"./chunk-C6uGsfWa.js";import{Bc as zn,Gt as K,Hs as se,Wa as g,Wn as Po,Wt as Jn,f as Ae,fr as S,gt as G,in as Lje,ks as qe,nn as LF,or as Rg,pc as vn,ro as he,sn as M,y as Be,ys as pt}from"./chunk-B_pj7Jpm.js";import{i as z}from"./chunk-qQMrTuzm.js";import{t as H}from"./chunk-CagJEiiY.js";var m=(0,C(Po()).default)(`peertube:subscriptions:UserSubscriptionService`);var P=class o{authHttp=g(zn);restExtractor=g(Jn);videoService=g(H);restService=g(Rg);static BASE_USER_SUBSCRIPTIONS_URL=se.apiUrl+`/api/v1/users/me/subscriptions`;static BASE_VIDEO_CHANNELS_URL=se.apiUrl+`/api/v1/video-channels`;static BASE_ACCOUNTS_URL=se.apiUrl+`/api/v1/accounts`;existsSubject=new qe(1);existsObservable;myAccountSubscriptionCache={};myAccountSubscriptionCacheObservable={};myAccountSubscriptionCacheSubject=new K;constructor(){this.existsObservable=LF(Lje({time:200,notifierObservable:this.existsSubject,bulkGet:this.doSubscriptionsExist.bind(this)}).pipe(G(t=>t.response)),this.myAccountSubscriptionCacheSubject)}listFollowers(t){let{pagination:e,nameWithHost:i,search:s,channel:a}=t,r=new vn;r=this.restService.addRestGetParams(r,this.restService.componentToRestPagination(e),`-createdAt`),s&&(r=r.append(`search`,s));let n=a?`${o.BASE_VIDEO_CHANNELS_URL}/${a}/followers`:`${o.BASE_ACCOUNTS_URL}/${i}/followers`;return this.authHttp.get(n,{params:r}).pipe(he(B=>this.restExtractor.handleError(B)))}getUserSubscriptionVideos(t){let{videoPagination:e,sort:i,skipCount:s}=t,a=this.restService.componentToRestPagination(e),r=new vn;return r=this.restService.addRestGetParams(r,a,i),s&&(r=r.set(`skipCount`,s+``)),this.authHttp.get(o.BASE_USER_SUBSCRIPTIONS_URL+`/videos`,{params:r}).pipe(Be(n=>this.videoService.extractVideos(n)),he(n=>this.restExtractor.handleError(n)))}deleteSubscription(t){let e=o.BASE_USER_SUBSCRIPTIONS_URL+`/`+t;return this.authHttp.delete(e).pipe(pt(()=>{this.myAccountSubscriptionCache[t]=!1,this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)}),he(i=>this.restExtractor.handleError(i)))}addSubscription(t){let e=o.BASE_USER_SUBSCRIPTIONS_URL,i={uri:t};return this.authHttp.post(e,i).pipe(pt(()=>{this.myAccountSubscriptionCache[t]=!0,this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)}),he(s=>this.restExtractor.handleError(s)))}listSubscriptions(t){let{pagination:e,search:i}=t,s=o.BASE_USER_SUBSCRIPTIONS_URL,a=this.restService.componentToRestPagination(e),r=new vn;return r=this.restService.addRestGetParams(r,a),i&&(r=r.append(`search`,i)),this.authHttp.get(s,{params:r}).pipe(G(n=>z.extractVideoChannels(n)),he(n=>this.restExtractor.handleError(n)))}listenToMyAccountSubscriptionCacheSubject(){return this.myAccountSubscriptionCacheSubject.asObservable()}listenToSubscriptionCacheChange(t){if(t in this.myAccountSubscriptionCacheObservable)return this.myAccountSubscriptionCacheObservable[t];let e=this.existsObservable.pipe(Ae(i=>i[t]!==void 0),G(i=>i[t]));return this.myAccountSubscriptionCacheObservable[t]=e,e}doesSubscriptionExist(t){return m(`Running subscription check for `+t),t in this.myAccountSubscriptionCache?(m(`Found cache for `+t),M(this.myAccountSubscriptionCache[t])):(this.existsSubject.next(t),m(`Fetching from network for `+t),this.existsObservable.pipe(Ae(e=>e[t]!==void 0),G(e=>e[t]),pt(e=>this.myAccountSubscriptionCache[t]=e)))}doSubscriptionsExist(t){let e=o.BASE_USER_SUBSCRIPTIONS_URL+`/exist`,i=new vn;return i=this.restService.addObjectParams(i,{uris:t}),this.authHttp.get(e,{params:i}).pipe(pt(s=>{this.myAccountSubscriptionCache=w(w({},this.myAccountSubscriptionCache),s)}),he(s=>this.restExtractor.handleError(s)))}static ɵfac=function(e){return new(e||o)};static ɵprov=S({token:o,factory:o.ɵfac})};export{P as t};
//# debugId=e0c90316-01da-5b42-af90-dcb16bd362c6
//# sourceMappingURL=chunk-DYngkL6R2.js.map