import{c as B}from"./chunk-NJ5QGAXC.js";import{b as j}from"./chunk-DRC3434R.js";import{C as a,Jf as L,Kc as b,Oc as y,Rf as O,S as R,U as h,Xf as g,Ye as l,Z as x,aa as S,c as E,e as f,j as v,kf as U,n as p,w as A,y as d}from"./chunk-4JM6MIZ5.js";import{a as m,g as _,i as u}from"./chunk-QJQRWPYR.js";var I=_(L());var C=(0,I.default)("peertube:subscriptions:UserSubscriptionService"),s=class s{authHttp;restExtractor;videoService;restService;existsSubject=new f(1);existsObservable;myAccountSubscriptionCache={};myAccountSubscriptionCacheObservable={};myAccountSubscriptionCacheSubject=new E;constructor(t,e,i,c){this.authHttp=t,this.restExtractor=e,this.videoService=i,this.restService=c,this.existsObservable=A(U({time:200,notifierObservable:this.existsSubject,bulkGet:this.doSubscriptionsExist.bind(this)}).pipe(p(n=>n.response)),this.myAccountSubscriptionCacheSubject)}listFollowers(t){let{pagination:e,nameWithHost:i,search:c}=t,n=`${s.BASE_ACCOUNTS_URL}/${i}/followers`,r=new b;if(r=this.restService.addRestGetParams(r,this.restService.componentToRestPagination(e),"-createdAt"),c){let o=this.restService.parseQueryStringFilter(c,{channel:{prefix:"channel:"}});o.channel&&(n=`${s.BASE_VIDEO_CHANNELS_URL}/${o.channel}/followers`),r=this.restService.addObjectParams(r,{search:o.search})}return this.authHttp.get(n,{params:r}).pipe(a(o=>this.restExtractor.handleError(o)))}getUserSubscriptionVideos(t){let{videoPagination:e,sort:i,skipCount:c}=t,n=this.restService.componentToRestPagination(e),r=new b;return r=this.restService.addRestGetParams(r,n,i),c&&(r=r.set("skipCount",c+"")),this.authHttp.get(s.BASE_USER_SUBSCRIPTIONS_URL+"/videos",{params:r}).pipe(R(o=>this.videoService.extractVideos(o)),a(o=>this.restExtractor.handleError(o)))}deleteSubscription(t){let e=s.BASE_USER_SUBSCRIPTIONS_URL+"/"+t;return this.authHttp.delete(e).pipe(h(()=>{this.myAccountSubscriptionCache[t]=!1,this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)}),a(i=>this.restExtractor.handleError(i)))}addSubscription(t){let e=s.BASE_USER_SUBSCRIPTIONS_URL,i={uri:t};return this.authHttp.post(e,i).pipe(h(()=>{this.myAccountSubscriptionCache[t]=!0,this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)}),a(c=>this.restExtractor.handleError(c)))}listSubscriptions(t){let{pagination:e,search:i}=t,c=s.BASE_USER_SUBSCRIPTIONS_URL,n=this.restService.componentToRestPagination(e),r=new b;return r=this.restService.addRestGetParams(r,n),i&&(r=r.append("search",i)),this.authHttp.get(c,{params:r}).pipe(p(o=>j.extractVideoChannels(o)),a(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),v(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]),h(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(h(c=>{this.myAccountSubscriptionCache=m(m({},this.myAccountSubscriptionCache),c)}),a(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)(S(y),S(O),S(B),S(g))}),u(s,"\u0275prov",x({token:s,factory:s.\u0275fac}));var P=s;export{P as a};
/**i18n:68865f44f2a527c44cf5136a17c76a0ff8308c2bae5c7c8d50070a5fb9847c44*/
//# sourceMappingURL=chunk-ZXCBEUCX.js.map
