import React, { useContext, useEffect, useMemo, useImperativeHandle, forwardRef } from "react";
import classnames from "classnames";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
import { parseFormat } from "./utils";
import { useCountDown } from "../hooks/use-count-down";
const CountDown = /* @__PURE__ */ forwardRef((props, ref) => {
  const {
    time,
    format,
    autoStart,
    millisecond,
    renderChildren
  } = props;
  const {
    start,
    pause,
    reset,
    current
  } = useCountDown({
    time: +time,
    millisecond,
    onChange: (value) => {
      var _a;
      return (_a = props.onChange) == null ? void 0 : _a.call(props, value);
    },
    onFinish: props.onFinish
  });
  const timeText = useMemo(() => parseFormat(format, current), [format, current]);
  const resetTime = () => {
    reset(+time);
    if (autoStart) {
      start();
    }
  };
  useEffect(() => {
    resetTime();
  }, [time]);
  useImperativeHandle(ref, () => ({
    start,
    pause,
    reset: resetTime
  }));
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("count-down", prefixCls);
  return /* @__PURE__ */ React.createElement("div", {
    role: "timer",
    className: classnames(bem())
  }, renderChildren ? renderChildren(current) : timeText);
});
CountDown.defaultProps = {
  time: 0,
  format: "HH:mm:ss",
  autoStart: true,
  millisecond: false
};
var stdin_default = CountDown;
export {
  stdin_default as default
};
