import{c as P}from"./chunk-7TPOLMSO.js";import{d as B}from"./chunk-4GMKSIW4.js";import{B as n,Ff as U,Fh as O,Kh as g,Q as R,S,Y as x,aa as h,c as E,dd as b,e as f,hd as y,j as A,n as p,qf as l,uh as L,v,x as d}from"./chunk-R4Z6ZI3L.js";import{a as m,g as _,i as u}from"./chunk-P6NJDXIL.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:138e9aba5923606215625ab5641fbdc103d8a34cfe13ae910386f40f27aea675*/
//# sourceMappingURL=chunk-MFRCVAQN.js.map
