import React, { useEffect, useState } from 'react';
import classnames from 'classnames';
var defaultProps = {
    prefixCls: 'ty-countdown',
    milliSecond: false,
};
var intervalHandler = null;
var Countdown = function (props) {
    var value = props.value, onFinish = props.onFinish, milliSecond = props.milliSecond, prefixCls = props.prefixCls, className = props.className, style = props.style, children = props.children;
    var cls = classnames(prefixCls, className);
    var _a = useState({ day: 0, hour: 0, min: 0, sec: 0, milliSec: 0 }), date = _a[0], setDate = _a[1];
    var startInterval = function () {
        intervalHandler = setInterval(function () {
            var distance = value.getTime() - Date.now();
            if (distance < 0) {
                clearInterval(intervalHandler);
                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 milliSec = Math.floor(distance % 60000 % 1000);
            setDate({ day: day, hour: hour, min: min, sec: sec, milliSec: milliSec });
        }, milliSecond ? 1 : 1000);
    };
    useEffect(function () {
        value && startInterval();
        return function () {
            clearInterval(intervalHandler);
        };
    }, []);
    return (React.createElement("div", { className: cls, style: style }, (typeof children === 'function') && children(date)));
};
Countdown.defaultProps = defaultProps;
export default Countdown;
//# sourceMappingURL=index.js.map