import{c as P}from"./chunk-JDO6XDLS.js";import{b as B}from"./chunk-HY74C762.js";import{Ah as g,B as n,Kg as U,Q as R,S,Y as x,Zc as b,aa as h,bd as y,c as E,e as f,j as A,jh as L,n as p,v,vh as O,wg as l,x as d}from"./chunk-EWB56PWF.js";import{a as m,g as _,i as u}from"./chunk-2T7OYDCL.js";var I=_(L());var C=(0,I.default)("peertube:subscriptions:UserSubscriptionService"),s=class s{authHttp=h(y);restExtractor=h(O);videoService=h(P);restService=h(g);existsSubject=new f(1);existsObservable;myAccountSubscriptionCache={};myAccountSubscriptionCacheObservable={};myAccountSubscriptionCacheSubject=new E;constructor(){this.existsObservable=v(U({time:200,notifierObservable:this.existsSubject,bulkGet:this.doSubscriptionsExist.bind(this)}).pipe(p(t=>t.response)),this.myAccountSubscriptionCacheSubject)}listFollowers(t){let{pagination:e,nameWithHost:r,search:c}=t,a=`${s.BASE_ACCOUNTS_URL}/${r}/followers`,i=new b;if(i=this.restService.addRestGetParams(i,this.restService.componentToRestPagination(e),"-createdAt"),c){let o=this.restService.parseQueryStringFilter(c,{channel:{prefix:"channel:"}});o.channel&&(a=`${s.BASE_VIDEO_CHANNELS_URL}/${o.channel}/followers`),i=this.restService.addObjectParams(i,{search:o.search})}return this.authHttp.get(a,{params:i}).pipe(n(o=>this.restExtractor.handleError(o)))}getUserSubscriptionVideos(t){let{videoPagination:e,sort:r,skipCount:c}=t,a=this.restService.componentToRestPagination(e),i=new b;return i=this.restService.addRestGetParams(i,a,r),c&&(i=i.set("skipCount",c+"")),this.authHttp.get(s.BASE_USER_SUBSCRIPTIONS_URL+"/videos",{params:i}).pipe(R(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(r=>this.restExtractor.handleError(r)))}addSubscription(t){let e=s.BASE_USER_SUBSCRIPTIONS_URL,r={uri:t};return this.authHttp.post(e,r).pipe(S(()=>{this.myAccountSubscriptionCache[t]=!0,this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)}),n(c=>this.restExtractor.handleError(c)))}listSubscriptions(t){let{pagination:e,search:r}=t,c=s.BASE_USER_SUBSCRIPTIONS_URL,a=this.restService.componentToRestPagination(e),i=new b;return i=this.restService.addRestGetParams(i,a),r&&(i=i.append("search",r)),this.authHttp.get(c,{params:i}).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(r=>r[t]!==void 0),p(r=>r[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),A(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",r=new b;return r=this.restService.addObjectParams(r,{uris:t}),this.authHttp.get(e,{params:r}).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 j=s;export{j as a};
/**i18n:85d030b7fdd36d163c6d41dcef9178681a6baa573eb199e8b12fe27dbb057a98*/
//# sourceMappingURL=chunk-Y2V4AH2W.js.map
