import { useState, useEffect } from 'react';

function useCountdown() {
    const [expiration, setExpiration] = useState(0);
    const start = (durationInSeconds)=>{
        setExpiration(Date.now() + durationInSeconds * 1000);
    };
    const clear = ()=>{
        setExpiration(0);
    };
    return {
        expiration,
        start,
        clear
    };
}
function formatCountdown(secondsLeft) {
    const minutes = Math.floor(secondsLeft / 60);
    const seconds = secondsLeft - minutes * 60;
    return minutes + ':' + String(seconds).padStart(2, '0');
}
const getSecondsRemaining = (expiration)=>Math.max(0, Math.ceil((expiration - Date.now()) / 1000));
const useTimeRemaining = (expirationEpoch)=>{
    const [seconds, setSeconds] = useState(()=>getSecondsRemaining(expirationEpoch));
    const codeExpired = seconds <= 0;
    useEffect(()=>{
        const updateSeconds = ()=>setSeconds(getSecondsRemaining(expirationEpoch));
        updateSeconds();
        if (Date.now() >= expirationEpoch) return;
        const interval = setInterval(updateSeconds, 1000);
        return ()=>clearInterval(interval);
    }, [
        codeExpired,
        expirationEpoch
    ]);
    return [
        seconds,
        !codeExpired
    ];
};

export { formatCountdown, useCountdown, useTimeRemaining };
//# sourceMappingURL=Countdown.mjs.map
