import{cj as X,r,X as B,c as d,ck as K,cl as U,cm as Y,q as z}from"./index-xnhGKCfe.js";import{u as Q}from"./useWorkerIntervalFn.hook-BuwsgSxq.js";const ae=(u,k)=>{const m=performance.now();let l;const h=E=>{E-m>=k?u():l=requestAnimationFrame(h)};return l=requestAnimationFrame(h),()=>cancelAnimationFrame(l)};var _=(u=>(u.BEFORE_PREHEAT="before_preheat",u.PREHEAT="preheat",u.COUNTDOWN="countdown",u.RAINING="raining",u.ENDED="ended",u))(_||{});const V=r({7089:"This round of red envelopes has been claimed out",7090:"Exceeded the activity claim limit",7091:"Exceeded the activity claim amount limit",7092:"Exceeded the claim amount limit for this round",7093:"Available claim amount is less than the minimum amount",7094:"Exceeded the claim limit for this round",7095:"This round of red envelopes has not started",7096:"The balance of this red envelope rain is insufficient"}),v=r(!1),i=r(!1),y=r(!1),b="ar_cashRain_limit_status",c=r(1),w=r(!1),S=r(!1),C=r(0),N=r(),t=r(null),f=r(0);function te(u){const k=B({fallSpeed:[5,6],spawnInterval:200,columns:6,canvasWidth:window.innerWidth,canvasHeight:window.innerHeight,scaleRange:[.8,1],rotationSpeedRange:[-.05,.05],imageSrc:u,boundaries:[60,60,0,0]}),m=()=>{c.value=1},l=async()=>{if(U())try{const e=await Y();if(e.code===0)if(e.data)t.value=e.data,f.value=(e==null?void 0:e.serverTime)??(e==null?void 0:e.serviceTime)??Date.now(),c.value=1;else{t.value=null,w.value=!1,v.value=!1,T();return}if(!t.value)return;const a=t.value;if(a.roundId!=null&&a.totalRoundCount!=null&&a.roundId>a.totalRoundCount){v.value=!1,i.value=!1,w.value=!0,I();return}a&&a.cashRainConfigId&&a.roundId&&(E(a.cashRainConfigId,a.roundId)&&(v.value=!1,i.value=!1,w.value=!1,S.value=!1),x())}catch{}},h=X(async()=>{var e,a;try{if(!t.value)return;const{code:n,data:s,msg:o,msgCode:D}=await K({cashRainConfigId:t.value.cashRainConfigId,roundId:t.value.roundId});return n===0?(C.value=Math.round((C.value+Number(s))*100)/100,{success:!0,amount:s}):(y.value=!0,R(((e=t.value)==null?void 0:e.cashRainConfigId)||0,((a=t.value)==null?void 0:a.roundId)||0,!0),{success:!1,message:D?V.value[D]:o,hasReachedLimit:!0})}catch{}},500),E=(e,a)=>{const n=sessionStorage.getItem(b);if(n){const{cashRainConfigId:s,roundId:o}=JSON.parse(n);if(s===e&&o===a)return!1}return y.value=!1,R(e,a,!1),!0},g=()=>{const e=sessionStorage.getItem(b);if(e){const{hasReachedLimit:a}=JSON.parse(e);return a}return!1},R=(e,a,n)=>{const s={cashRainConfigId:e,roundId:a,hasReachedLimit:n};sessionStorage.setItem(b,JSON.stringify(s))},p=()=>{if(!t.value)return"before_preheat";const e=f.value+c.value*1e3,{startPreheatTime:a=0,startCountdownTime:n=0,startTime:s=0,endTime:o=0}=t.value;return e<a?"before_preheat":e>=a&&e<n?"preheat":e>=n&&e<s?"countdown":e>=s&&e<o?"raining":"ended"},F=d(()=>{if(!t.value)return 0;const e=f.value+c.value*1e3,{endTime:a=0}=t.value,n=Math.floor((a-e)/1e3);return n>0?n:0}),P=d(()=>{if(!t.value)return"";const{startTime:e=0}=t.value;return z(e,"HH:mm")}),A=d(()=>{if(!t.value)return"00:00";const{startTime:e=0}=t.value,a=f.value+c.value*1e3,n=Math.max(0,e-a);return H(n)}),M=d(()=>{if(!t.value)return null;switch(p()){case"preheat":return`Starts ${P.value}`;case"countdown":return`In ${A.value}`;case"raining":return"In Progress";default:return"Activity Ended"}}),O=d(()=>{if(!t.value)return!1;const e=p();return e==="preheat"||e==="countdown"||e==="raining"}),H=e=>{if(e<=0)return"00:00";const a=Math.floor(e/1e3),n=Math.floor(a/60),s=a%60;return`${n.toString().padStart(2,"0")}:${s.toString().padStart(2,"0")}`},L=()=>{v.value=!1,i.value=!1,C.value=0},W=()=>{if(!t.value||g())return!0;const{endTime:e=0}=t.value;return(f.value||Date.now())>=e},$=async()=>{var e;!t.value||i.value||(await((e=N.value)==null?void 0:e.startRedPackRain()),i.value=!0,w.value=!0)},T=()=>{var e;I(),i.value=!1,c.value=1,S.value=!1,(e=N.value)==null||e.stopRedPackRain()},q=()=>{var a,n,s,o;switch(p()){case"preheat":(g()==!0||g()==null||g()==null)&&R(((a=t.value)==null?void 0:a.cashRainConfigId)||0,((n=t.value)==null?void 0:n.roundId)||0,!1);break;case"countdown":J();break;case"raining":j();break;case"ended":R(((s=t.value)==null?void 0:s.cashRainConfigId)||0,((o=t.value)==null?void 0:o.roundId)||0,!0),G();break}},J=async()=>{S.value||(S.value=!0,await l(),m())},j=()=>{i.value&&W()&&T()},G=()=>{T()},{pause:I,start:x}=Q(async()=>{if(c.value+=1,!t.value){I();return}q()},1e3,{immediate:!0});return{cashRainInfoData:t,displayText:M,startTimeText:P,countdownText:A,TimePhase:_,hasTriggered:v,isRunning:i,hasReachedCurrentRoundLimit:y,remainingDuration:F,shouldShowRedPackTag:O,rainConfig:k,canvasComponentRef:N,totalRoundAmount:C,syncServerTime:m,fetchCashRainData:l,claimRedPack:h,getCurrentTimePhase:p,resetState:L,startRedPackRain:$,stopRedPackRain:T,pauseTimer:I,startTimer:x}}const ne="/images/red_pack-CnXS5jgk.webp";export{te as a,ne as r,ae as u};