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}=u,I=e(()=>{switch(f){case"daily":return l(m.hours,m.mins,m.format);case"evergreen":return o(h);case"standard":return a(g,p);default:return 0}},[f,g,p,m.format,m.hours,m.mins,h.endTime]),[A,F]=r(I),k=t(),L=n(()=>F(e=>e<=0?(v.link&&"#"!==v.link&&"edit"!==c&&window.open(v.link,v.target),isLoopAfterFinish(d,f))?evergreenRefreshAfterFinish(h):(clearInterval(k.current),0):e-1),[f,d,c,v.link,v.target,h]);return s(()=>{let e=isLoopAfterFinish(d,f);return e&&(clearInterval(k.current),F(evergreenRefreshAfterFinish(h)),k.current=setInterval(L,1e3)),I<=0&&!e&&(clearInterval(k.current),F(0)),I>0&&(clearInterval(k.current),F(I),k.current=setInterval(L,1e3)),()=>{clearInterval(k.current)}},[f,L,I,d,h]),A},isLoopAfterFinish=(e,r)=>e&&"evergreen"===r,evergreenRefreshAfterFinish=e=>u(e.endTime,{days:e.days,hours:e.hours,mins:e.mins});export{useTimeLeft as default};
