"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var React=require("react"),core=require("@gem-sdk/core"),helpers=require("../common/helpers.js");const useTimeLeft=e=>{let r=core.useEditorMode(),{timerDaily:t,behaviour:s,timerEverygreen:n,loopAfterFinish:a,redirectUrl:i,timeZone:u,dateStandard:c,standardType:l}=e,o=React.useMemo(()=>{switch(s){case"daily":return helpers.daily(t.hours,t.mins,t.format);case"evergreen":return helpers.evergreen(n);case"standard":return helpers.standard(c,u,l);default:return 0}},[s,c,u,l,t.format,t.hours,t.mins,n.endTime]),[h,f]=React.useState(o),d=React.useRef(),v=React.useCallback(()=>f(e=>e<=0?(i.link&&"#"!==i.link&&"edit"!==r&&window.open(i.link,i.target),isLoopAfterFinish(a,s))?evergreenRefreshAfterFinish(n):(clearInterval(d.current),0):e-1),[s,a,r,i.link,i.target,n]);return React.useEffect(()=>{let e=isLoopAfterFinish(a,s);return e&&(clearInterval(d.current),f(evergreenRefreshAfterFinish(n)),d.current=setInterval(v,1e3)),o<=0&&!e&&(clearInterval(d.current),f(0)),o>0&&(clearInterval(d.current),f(o),d.current=setInterval(v,1e3)),()=>{clearInterval(d.current)}},[s,v,o,a,n]),h},isLoopAfterFinish=(e,r)=>e&&"evergreen"===r,evergreenRefreshAfterFinish=e=>helpers.refreshAfterFinish(e.endTime,{days:e.days,hours:e.hours,mins:e.mins});exports.default=useTimeLeft;
