/* Copyright © 2024 Melbet - All Rights Reserved */ import{ref as g,computed as P,unref as t,onMounted as T,onScopeDispose as U,getCurrentInstance as h,nextTick as w}from"../../../../shared-assets/__shared_vue_deps_SRT2KB67.js";import{useLogger as A,noop as C,createSharedComposable as D,useLeavingPage as E,LeavingPage as p,equalWatch as S}from"./entry-Bl6dkD65.js";var _=(e,s,a)=>new Promise((u,r)=>{var n=o=>{try{c(a.next(o))}catch(l){r(l)}},i=o=>{try{c(a.throw(o))}catch(l){r(l)}},c=o=>o.done?u(o.value):Promise.resolve(o.value).then(n,i);c((a=a.apply(e,s)).next())});const M=(e,s=0,...a)=>{let u=!1,r;const n=()=>{u=!0,clearTimeout(r)},i=()=>{u||(r=setTimeout(()=>_(void 0,null,function*(){yield e(...a),i()}),s))};return i(),n},B=({interval:e,onUpdateData:s,onCancelIntervalRequest:a})=>{const u=A();let r=C;const n=g(!0),i=g(!1),c=g(!1),o=P(()=>t(c)),l=P(()=>t(n)),d=async()=>{try{c.value=!0,await s()}catch(v){u.error("timerUpdateData",v)}finally{c.value=!1}},m=()=>{t(n)||(r(),r=M(d,t(e)))},y=()=>{!t(n)||t(i)||(n.value=!1,m())},f=()=>{t(n)||t(i)||(n.value=!0,r())};return{timerLoading:o,timerPaused:l,timerStart:y,timerPause:f,timerForceUpdate:async()=>{t(i)||(t(n)||r(),await a(),await d(),t(n)||m())},timerDestroy:async()=>{t(i)||(f(),i.value=!0,await a())}}},G=D(()=>{const e=E(),s=g(e.isHidden),a=()=>{s.value=e.isHidden};return T(()=>{e.on(p.LEAVING,a),e.on(p.COME_BACK,a)}),U(()=>{e.remove(p.LEAVING,a),e.remove(p.COME_BACK,a)}),{hasLeftPage:s}}),O=(e,s=!0)=>{h()?T(e):s?e():w(e)},k=3e4,V=({interval:e,onUpdateData:s,onCancelIntervalRequest:a,startTimerAfterComeBack:u})=>{const{hasLeftPage:r}=G(),{timerLoading:n,timerPaused:i,timerStart:c,timerPause:o,timerDestroy:l,timerForceUpdate:d}=B({interval:e,onUpdateData:s,onCancelIntervalRequest:a});let m;const y=()=>{clearTimeout(m),m=setTimeout(o,k)},f=P(()=>{var v;return(v=t(u))!=null?v:!0}),L=async()=>{clearTimeout(m),t(i&&t(f))&&(await d(),c())},I=async()=>{c(),t(r)&&o()};return S(r,()=>{t(r)?y():L()}),O(I),U(l),{loading:n,forceUpdateData:d,timerDestroy:l,timerPause:o,timerStart:c}};export{O as tryOnMounted,B as useIntervalUpdate,V as useStartIntervalUpdate};