"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}=e,l=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);default:return 0}},[s,c,u,t.format,t.hours,t.mins,n.endTime]),[o,h]=React.useState(l),f=React.useRef(),d=React.useCallback(()=>h(e=>e<=0?(i.link&&"#"!==i.link&&"edit"!==r&&window.open(i.link,i.target),isLoopAfterFinish(a,s))?evergreenRefreshAfterFinish(n):(clearInterval(f.current),0):e-1),[s,a,r,i.link,i.target,n]);return React.useEffect(()=>{let e=isLoopAfterFinish(a,s);return e&&(clearInterval(f.current),h(evergreenRefreshAfterFinish(n)),f.current=setInterval(d,1e3)),l<=0&&!e&&(clearInterval(f.current),h(0)),l>0&&(clearInterval(f.current),h(l),f.current=setInterval(d,1e3)),()=>{clearInterval(f.current)}},[s,d,l,a,n]),o},isLoopAfterFinish=(e,r)=>e&&"evergreen"===r,evergreenRefreshAfterFinish=e=>helpers.refreshAfterFinish(e.endTime,{days:e.days,hours:e.hours,mins:e.mins});exports.default=useTimeLeft;
