import React, { forwardRef, useContext, useEffect, useImperativeHandle, useMemo } from 'react';
import cls from 'clsx';
import { noop } from '../utils';
import useCountDown from '../hooks/use-count-down';
import { parseFormat } from './utils';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
var CountDown = /*#__PURE__*/forwardRef(function (props, ref) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('count-down', prefixCls),
      bem = _createNamespace[0];

  var _useCountDown = useCountDown({
    time: +props.time,
    millisecond: props.millisecond,
    onChange: props.onChange,
    onFinish: props.onFinish
  }),
      start = _useCountDown.start,
      pause = _useCountDown.pause,
      reset = _useCountDown.reset,
      current = _useCountDown.current;

  var timeText = useMemo(function () {
    return parseFormat(props.format, current);
  }, [current]);

  var resetTime = function resetTime() {
    reset(+props.time);

    if (props.autoStart) {
      start();
    }
  };

  useEffect(function () {
    resetTime();
    return function () {
      pause();
    };
  }, [props.time]);
  useImperativeHandle(ref, function () {
    return {
      start: start,
      pause: pause,
      reset: resetTime
    };
  });
  return /*#__PURE__*/React.createElement("div", {
    className: cls(props.className, bem()),
    style: props.style
  }, props.children ? props.children(current) : timeText);
});
CountDown.defaultProps = {
  autoStart: true,
  time: 0,
  format: 'HH:mm:ss',
  onChange: noop,
  onFinish: noop
};
export default CountDown;