1import{i as f}from"./chunk-JPIUVRHT.js";import{e as c,f as R,ga as m,la as w,t as d,ua as p}from"./chunk-QLDFHHJL.js";import{a,b as n}from"./chunk-PRARYYF3.js";var x=(()=>{class u{getCurrentReelId(){return this.stateSubject.value.currentReel?.reel?.id??null}constructor(){this.BATCH_SIZE=10,this.PRELOAD_THRESHOLD=5,this.MAX_QUEUE_SIZE=30,this.stateSubject=new R({currentReel:null,queue:[],isLoading:!1,hasMore:!0,totalViewed:0,currentIndex:0}),this.viewedReelIds=new Set,this.currentCycleReelIds=new Set,this.destroy$=new c,this.normalReelsBuffer=[],this.adminReelsBuffer=[],this.normalReelsOffset=0,this.adminReelsOffset=0,this.normalReelsHasMore=!0,this.adminReelsHasMore=!0,this.normalReelsInPattern=0,this.state$=this.stateSubject.asObservable(),this.injector=w(p)}shuffleArray(t){let e=[...t];for(let s=e.length-1;s>0;s--){let i=Math.floor(Math.random()*(s+1));[e[s],e[i]]=[e[i],e[s]]}return e}isAdminReel(t){return t.isAdminReel===!0}async fetchReelsBatch(t,e,s){try{let{GET_REELS:i}=await import("./chunk-B2DSIPUB.js"),r=(await d(this.injector.get(f).query({query:i,variables:{filter:{limit:t,offset:e,approvalStatus:"APPROVED",isAdminReel:s}},fetchPolicy:"network-only"})))?.data?.getReels;return{reels:r?.reels||[],hasMore:r?.hasMore||!1}}catch(i){return console.error(`Failed to fetch ${s?"admin":"normal"} reels:`,i),{reels:[],hasMore:!1}}}buildInterleavedQueue(t=this.MAX_QUEUE_SIZE){let e=[],s=new Set,i=0,l=this.normalReelsInPattern;for(;e.length<t&&(this.normalReelsBuffer.length>0||this.adminReelsBuffer.length>0);)if(l<5&&this.normalReelsBuffer.length>0){let r=this.normalReelsBuffer.shift();!this.viewedReelIds.has(r.id)&&!s.has(r.id)&&(e.push({reel:r,index:i++,isViewed:!1}),s.add(r.id),this.currentCycleReelIds.add(r.id),l++)}else if(l===5&&this.adminReelsBuffer.length>0){let r=this.adminReelsBuffer.shift();!this.viewedReelIds.has(r.id)&&!s.has(r.id)&&(e.push({reel:r,index:i++,isViewed:!1}),s.add(r.id),this.currentCycleReelIds.add(r.id)),l=0}else if(this.normalReelsBuffer.length===0&&this.adminReelsBuffer.length>0){let r=this.adminReelsBuffer.shift();!this.viewedReelIds.has(r.id)&&!s.has(r.id)&&(e.push({reel:r,index:i++,isViewed:!1}),s.add(r.id),this.currentCycleReelIds.add(r.id))}else break;return this.normalReelsInPattern=l,e}async prefetchBuffersForInitialBuild(t=8){for(let e=0;e<t&&!(!this.normalReelsHasMore&&!this.adminReelsHasMore);e++)await this.ensureBuffersHaveEnough()}syncCycleIdsToQueue(t){this.currentCycleReelIds.clear();for(let e of t)this.currentCycleReelIds.add(e.reel.id)}async ensureBuffersHaveEnough(){if(this.normalReelsBuffer.length<10&&this.normalReelsHasMore){let e=await this.fetchReelsBatch(20,this.normalReelsOffset,!1),s=this.filterDuplicateAndViewedReels(e.reels);this.normalReelsBuffer.push(...this.shuffleArray(s)),this.normalReelsOffset+=e.reels.length,this.normalReelsHasMore=e.hasMore}if(!this.normalReelsHasMore){if(this.adminReelsHasMore&&this.adminReelsBuffer.length<40){let s=await this.fetchReelsBatch(20,this.adminReelsOffset,!0),i=this.filterDuplicateAndViewedReels(s.reels);this.adminReelsBuffer.push(...this.shuffleArray(i)),this.adminReelsOffset+=s.reels.length,this.adminReelsHasMore=s.hasMore}return}let t=Math.ceil(this.normalReelsBuffer.length/5);if(this.adminReelsBuffer.length<t&&this.adminReelsHasMore){let e=await this.fetchReelsBatch(4,this.adminReelsOffset,!0),s=this.filterDuplicateAndViewedReels(e.reels);this.adminReelsBuffer.push(...this.shuffleArray(s)),this.adminReelsOffset+=e.reels.length,this.adminReelsHasMore=e.hasMore}}allReelsInQueueViewed(){let t=this.stateSubject.value.queue;return t.length>0&&t.every(e=>e.isViewed)}async reshuffleAndStartNewCycle(){let t=this.stateSubject.value;this.currentCycleReelIds.clear(),this.viewedReelIds.clear(),this.normalReelsBuffer=[],this.adminReelsBuffer=[],this.normalReelsOffset=0,this.adminReelsOffset=0,this.normalReelsInPattern=0,this.normalReelsHasMore=!0,this.adminReelsHasMore=!0,this.updateState({isLoading:!0});try{let[e,s]=await Promise.all([this.fetchReelsBatch(50,0,!1),this.fetchReelsBatch(10,0,!0)]),i=this.filterDuplicateAndViewedReels(e.reels),l=this.filterDuplicateAndViewedReels(s.reels);if(this.normalReelsBuffer=this.shuffleArray(i),this.adminReelsBuffer=this.shuffleArray(l),this.normalReelsOffset=e.reels.length,this.adminReelsOffset=s.reels.length,this.normalReelsHasMore=e.hasMore,this.adminReelsHasMore=s.hasMore,this.normalReelsBuffer.length===0&&this.adminReelsBuffer.length===0){console.warn("reshuffleAndStartNewCycle: No reels available"),this.updateState({isLoading:!1});return}await this.prefetchBuffersForInitialBuild();let r=this.buildInterleavedQueue(50);if(r.length===0){this.updateState({isLoading:!1});return}this.viewedReelIds.add(r[0].reel.id),r[0]=n(a({},r[0]),{isViewed:!0,viewedAt:new Date}),this.updateState({queue:r,currentReel:r[0],currentIndex:0,hasMore:this.normalReelsHasMore||this.adminReelsHasMore,totalViewed:t.totalViewed,isLoading:!1}),(this.normalReelsHasMore||this.adminReelsHasMore)&&await this.ensureBuffersHaveEnough()}catch(e){console.error("reshuffleAndStartNewCycle: Failed to reshuffle reels",e),this.updateState({isLoading:!1})}}async getInitialReelId(){try{this.updateState({currentReel:null,queue:[],isLoading:!0,hasMore:!0,totalViewed:0,currentIndex:0}),this.viewedReelIds.clear(),this.currentCycleReelIds.clear(),this.normalReelsBuffer=[],this.adminReelsBuffer=[],this.normalReelsOffset=0,this.adminReelsOffset=0,this.normalReelsHasMore=!0,this.adminReelsHasMore=!0,this.normalReelsInPattern=0;let[t,e]=await Promise.all([this.fetchReelsBatch(50,0,!1),this.fetchReelsBatch(10,0,!0)]),s=this.filterDuplicateAndViewedReels(t.reels),i=this.filterDuplicateAndViewedReels(e.reels);if(this.normalReelsBuffer=this.shuffleArray(s),this.adminReelsBuffer=this.shuffleArray(i),this.normalReelsOffset=t.reels.length,this.adminReelsOffset=e.reels.length,this.normalReelsHasMore=t.hasMore,this.adminReelsHasMore=e.hasMore,this.normalReelsBuffer.length===0&&this.adminReelsBuffer.length===0)return null;await this.prefetchBuffersForInitialBuild(),this.normalReelsInPattern=0;let l=this.buildInterleavedQueue(30);return l.length===0?null:(this.viewedReelIds.add(l[0].reel.id),l[0]=n(a({},l[0]),{isViewed:!0,viewedAt:new Date}),this.updateState({queue:l,currentReel:l[0],currentIndex:0,isLoading:!1,hasMore:this.normalReelsHasMore||this.adminReelsHasMore}),(this.normalReelsHasMore||this.adminReelsHasMore)&&await this.ensureBuffersHaveEnough(),l[0]?.reel?.id??null)}catch(t){return console.error("getInitialReelId: Failed to load initial reels",t),null}}async filterApprovedReels(){this.updateState({queue:this.stateSubject.value?.queue?.filter(t=>t.reel.approvalStatus==="APPROVED").map((t,e)=>n(a({},t),{index:e}))||[]})}async goToReelById(t){let e=this.stateSubject.value,s=e.queue.findIndex(i=>i.reel?.id===t);if(s>=0)return this.updateState({currentReel:e.queue[s],currentIndex:s}),!0;try{this.updateState({isLoading:!0});let{GET_REEL_BY_ID:i}=await import("./chunk-B2DSIPUB.js"),r=(await d(this.injector.get(f).query({query:i,variables:{id:t},fetchPolicy:"network-only"})))?.data?.getReelById?.reel;if(!r)return!1;let h={reel:r,index:0,isViewed:!1},o=[h,...e.queue].map((I,S)=>n(a({},I),{index:S}));return this.syncCycleIdsToQueue(o),this.updateState({queue:o,currentReel:h,currentIndex:0,isLoading:!1}),o.length<=this.PRELOAD_THRESHOLD&&this.preloadNextBatch(),!0}catch{return this.updateState({isLoading:!1}),!1}}getNormalReelsCountFromQueue(t){if(t.length===0)return 0;let e=0;for(let s=t.length-1;s>=0&&e<5&&!this.isAdminReel(t[s].reel);s--)e++;return e}async loadNextBatch(t=!0){let e=this.stateSubject.value;if(!(!this.normalReelsHasMore&&!this.adminReelsHasMore||t&&e.isLoading)){t&&this.updateState({isLoading:!0});try{this.normalReelsInPattern=this.getNormalReelsCountFromQueue(e.queue),await this.ensureBuffersHaveEnough();let s=this.buildInterleavedQueue(this.BATCH_SIZE);if(s.length>0){let i=s.map((h,o)=>n(a({},h),{index:e.queue.length+o})),l=[...e.queue,...i],r=l.length>this.MAX_QUEUE_SIZE?l.slice(-this.MAX_QUEUE_SIZE):l;this.syncCycleIdsToQueue(r),this.updateState({queue:r,isLoading:t?!1:e.isLoading,hasMore:this.normalReelsHasMore||this.adminReelsHasMore}),this.maybePreloadAhead(r)}else t&&this.updateState({isLoading:!1})}catch(s){console.error("Failed to load reel batch:",s),t&&this.updateState({isLoading:!1})}}}async preloadNextBatch(){let t=this.stateSubject.value;t.isLoading||!t.hasMore||await this.loadNextBatch(!1)}maybePreloadAhead(t){let e=this.stateSubject.value,s=t??e.queue;if(s.length===0||!(this.normalReelsHasMore||this.adminReelsHasMore))return;s.length-1-e.currentIndex<=this.PRELOAD_THRESHOLD&&this.preloadNextBatch()}moveToNextReel(){try{let t=this.stateSubject.value;if(t.queue.length===0)return console.warn("moveToNextReel: No reels in queue"),!1;if(t.currentReel){this.viewedReelIds.add(t.currentReel.reel.id);let l=t.queue.map(r=>r.index===t.currentIndex?n(a({},r),{isViewed:!0,viewedAt:new Date}):r);this.updateState({queue:l})}let e=this.stateSubject.value,s=e.currentIndex+1;if(s>=e.queue.length){if(e.hasMore)return this.loadNextBatch(),!1;if(this.allReelsInQueueViewed())return this.reshuffleAndStartNewCycle(),!0;let l=e.queue[0];return l?(this.updateState({currentReel:l,currentIndex:0,totalViewed:e.totalViewed+1}),!0):!1}let i=e.queue[s];return i?(this.updateState({currentReel:i,currentIndex:s,totalViewed:e.totalViewed+1}),this.maybePreloadAhead(),!0):(console.error(`moveToNextReel: No reel found at index ${s}`),!1)}catch(t){return console.error("moveToNextReel: Error occurred",t),!1}}moveToPreviousReel(){try{let t=this.stateSubject.value;if(t.queue.length===0)return console.warn("moveToPreviousReel: No reels in queue"),!1;let e=t.currentIndex-1;if(e<0){let i=t.queue[t.queue.length-1];return i?(this.updateState({currentReel:i,currentIndex:t.queue.length-1}),!0):(console.error("moveToPreviousReel: No last reel found for looping"),!1)}let s=t.queue[e];return s?(this.updateState({currentReel:s,currentIndex:e}),!0):(console.error(`moveToPreviousReel: No reel found at index ${e}`),!1)}catch(t){return console.error("moveToPreviousReel: Error occurred",t),!1}}updateState(t){let e=this.stateSubject.value;this.stateSubject.next(a(a({},e),t))}updateReelInCache(t,e){if(!t){console.warn("updateReelInCache: reelId is required");return}let s=this.stateSubject.value,i=s.queue.map(r=>r.reel?.id===t?n(a({},r),{reel:a(a({},r.reel),e)}):r),l=s.currentReel;s.currentReel?.reel?.id===t&&(l=n(a({},s.currentReel),{reel:a(a({},s.currentReel.reel),e)})),this.updateState({queue:i,currentReel:l})}updateReelCommentCount(t,e){if(!t){console.warn("updateReelCommentCount: reelId is required");return}let i=this.stateSubject.value.queue.find(l=>l.reel?.id===t);if(i){let l=Math.max(0,(i.reel.commentCount||0)+e);this.updateReelInCache(t,{commentCount:l})}else console.warn(`updateReelCommentCount: Reel with id ${t} not found in cache`)}updateReelLikeStatus(t,e,s){if(!t){console.warn("updateReelLikeStatus: reelId is required");return}let l=this.stateSubject.value.queue.find(r=>r.reel?.id===t);if(l){let r=Math.max(0,(l.reel.likeCount||0)+s);this.updateReelInCache(t,{likeCount:r,isLikedByCurrentUser:e})}else console.warn(`updateReelLikeStatus: Reel with id ${t} not found in cache`)}filterDuplicateAndViewedReels(t){if(!t?.length)return[];let e=new Set(this.stateSubject.value.queue.map(i=>i.reel.id)),s=new Set;return t.filter(i=>s.has(i.id)||e.has(i.id)||this.currentCycleReelIds.has(i.id)&&this.viewedReelIds.has(i.id)?!1:(s.add(i.id),!0))}ngOnDestroy(){this.destroy$.next(),this.destroy$.complete(),this.stateSubject.complete()}static{this.\u0275fac=function(e){return new(e||u)}}static{this.\u0275prov=m({token:u,factory:u.\u0275fac,providedIn:"root"})}}return u})();export{x as a};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.