import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
var Countdown = function (props) {
    var _a = props.millisec, millisec = _a === void 0 ? false : _a, customisedCls = props.prefixCls, value = props.value, onFinish = props.onFinish, className = props.className, style = props.style, children = props.children;
    var configContext = useContext(ConfigContext);
    var prefixCls = getPrefixCls('countdown', configContext.prefixCls, customisedCls);
    var cls = classNames(prefixCls, className);
    var _b = useState({ day: 0, hour: 0, min: 0, sec: 0, millisec: 0 }), date = _b[0], setDate = _b[1];
    var timerRef = useRef();
    var startInterval = useCallback(function () {
        timerRef.current = window.setInterval(function () {
            var distance = value.getTime() - Date.now();
            if (distance < 0) {
                window.clearInterval(timerRef.current);
                setDate({ day: 0, hour: 0, min: 0, sec: 0, millisec: 0 });
                onFinish && onFinish();
                return;
            }
            var day = Math.floor(distance / 86400000);
            var hour = Math.floor((distance % 86400000) / 3600000);
            var min = Math.floor((distance % 3600000) / 60000);
            var sec = Math.floor((distance % 60000) / 1000);
            var millisecond = Math.floor((distance % 60000) % 1000);
            setDate({ day: day, hour: hour, min: min, sec: sec, millisec: millisecond });
        }, millisec ? 1 : 1000);
    }, [millisec, onFinish, value]);
    useEffect(function () {
        value && startInterval();
        return function () {
            window.clearInterval(timerRef.current);
        };
    }, [value, startInterval]);
    return (React.createElement("div", { className: cls, style: style }, typeof children === 'function' && children(date)));
};
Countdown.displayName = 'Countdown';
export default Countdown;
//# sourceMappingURL=index.js.map