import{c as B}from"./chunk-KHRLI7AH.js";import{b as j}from"./chunk-7MB5OJWB.js";import{$ as S,B as u,Ic as b,Mc as y,Nf as L,R,T as h,Wf as O,Y as x,_f as g,af as l,c as E,e as f,j as v,m as p,of as U,v as A,x as d}from"./chunk-X3RCWQ3R.js";import{a as m,g as _,i as o}from"./chunk-SR6RTMQI.js";var I=_(L());var C=(0,I.default)("peertube:subscriptions:UserSubscriptionService"),s=class s{constructor(t,e,i,c){o(this,"authHttp");o(this,"restExtractor");o(this,"videoService");o(this,"restService");o(this,"existsSubject",new f(1));o(this,"existsObservable");o(this,"myAccountSubscriptionCache",{});o(this,"myAccountSubscriptionCacheObservable",{});o(this,"myAccountSubscriptionCacheSubject",new E);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(a=>a.response)),this.myAccountSubscriptionCacheSubject)}listFollowers(t){let{pagination:e,nameWithHost:i,search:c}=t,a=`${s.BASE_ACCOUNTS_URL}/${i}/followers`,r=new b;if(r=this.restService.addRestGetParams(r,this.restService.componentToRestPagination(e),"-createdAt"),c){let n=this.restService.parseQueryStringFilter(c,{channel:{prefix:"channel:"}});n.channel&&(a=`${s.BASE_VIDEO_CHANNELS_URL}/${n.channel}/followers`),r=this.restService.addObjectParams(r,{search:n.search})}return this.authHttp.get(a,{params:r}).pipe(u(n=>this.restExtractor.handleError(n)))}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(R(n=>this.videoService.extractVideos(n)),u(n=>this.restExtractor.handleError(n)))}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)}),u(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)}),u(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(n=>j.extractVideoChannels(n)),u(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(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)}),u(c=>this.restExtractor.handleError(c)))}};o(s,"BASE_USER_SUBSCRIPTIONS_URL",l.apiUrl+"/api/v1/users/me/subscriptions"),o(s,"BASE_VIDEO_CHANNELS_URL",l.apiUrl+"/api/v1/video-channels"),o(s,"BASE_ACCOUNTS_URL",l.apiUrl+"/api/v1/accounts"),o(s,"\u0275fac",function(e){return new(e||s)(S(y),S(O),S(B),S(g))}),o(s,"\u0275prov",x({token:s,factory:s.\u0275fac}));var P=s;export{P as a};
/**i18n:3d16453f213eb5db982e7e7dd90631843ac5d3f860726a614777c3e9e3f9b57e*/
//# sourceMappingURL=chunk-IHI2VQRA.js.map
