import{useMemo as e,useState as r,useRef as t,useCallback as n,useEffect as s}from"react";import{useEditorMode as i}from"@gem-sdk/core";import{standard as a,evergreen as o,daily as l,refreshAfterFinish as u}from"../common/helpers.js";let useTimeLeft=u=>{let c=i(),{timerDaily:m,behaviour:f,timerEverygreen:h,loopAfterFinish:d,redirectUrl:v,timeZone:p,dateStandard:g,standardType:I}=u,A=e(()=>{switch(f){case"daily":return l(m.hours,m.mins,m.format);case"evergreen":return o(h);case"standard":return a(g,p,I);default:return 0}},[f,g,p,I,m.format,m.hours,m.mins,h.endTime]),[F,k]=r(A),L=t(),T=n(()=>k(e=>e<=0?(v.link&&"#"!==v.link&&"edit"!==c&&window.open(v.link,v.target),isLoopAfterFinish(d,f))?evergreenRefreshAfterFinish(h):(clearInterval(L.current),0):e-1),[f,d,c,v.link,v.target,h]);return s(()=>{let e=isLoopAfterFinish(d,f);return e&&(clearInterval(L.current),k(evergreenRefreshAfterFinish(h)),L.current=setInterval(T,1e3)),A<=0&&!e&&(clearInterval(L.current),k(0)),A>0&&(clearInterval(L.current),k(A),L.current=setInterval(T,1e3)),()=>{clearInterval(L.current)}},[f,T,A,d,h]),F},isLoopAfterFinish=(e,r)=>e&&"evergreen"===r,evergreenRefreshAfterFinish=e=>u(e.endTime,{days:e.days,hours:e.hours,mins:e.mins});export{useTimeLeft as default};
