var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  useCountDown: () => useCountDown
});
module.exports = __toCommonJS(stdin_exports);
var import_react = require("react");
var import_utils = require("../utils");
var import_raf = require("../utils/raf");
const SECOND = 1e3;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
function parseTime(time) {
  const days = Math.floor(time / DAY);
  const hours = Math.floor(time % DAY / HOUR);
  const minutes = Math.floor(time % HOUR / MINUTE);
  const seconds = Math.floor(time % MINUTE / SECOND);
  const milliseconds = Math.floor(time % SECOND);
  return {
    total: time,
    days,
    hours,
    minutes,
    seconds,
    milliseconds
  };
}
function isSameSecond(time1, time2) {
  return Math.floor(time1 / 1e3) === Math.floor(time2 / 1e3);
}
function useCountDown(options) {
  const [remain, setRemain] = (0, import_react.useState)(options.time);
  const remainRef = (0, import_react.useRef)(remain);
  const rafId = (0, import_react.useRef)();
  const endTime = (0, import_react.useRef)();
  const counting = (0, import_react.useRef)();
  const current = (0, import_react.useMemo)(() => parseTime(remain), [remain]);
  const pause = () => {
    counting.current = false;
    (0, import_raf.cancelRaf)(rafId.current);
  };
  const getCurrentRemain = () => Math.max(endTime.current - Date.now(), 0);
  const handleRemain = (value) => {
    var _a, _b;
    setRemain(value);
    remainRef.current = value;
    (_a = options.onChange) == null ? void 0 : _a.call(options, parseTime(value));
    if (value === 0) {
      pause();
      (_b = options.onFinish) == null ? void 0 : _b.call(options);
    }
  };
  const microTick = () => {
    rafId.current = (0, import_raf.raf)(() => {
      if (counting.current) {
        handleRemain(getCurrentRemain());
        if (remainRef.current > 0) {
          microTick();
        }
      }
    });
  };
  const macroTick = () => {
    rafId.current = (0, import_raf.raf)(() => {
      if (counting.current) {
        const remainRemain = getCurrentRemain();
        if (!isSameSecond(remainRemain, remainRef.current) || remainRemain === 0) {
          handleRemain(remainRemain);
        }
        if (remainRef.current > 0) {
          macroTick();
        }
      }
    });
  };
  const tick = () => {
    if (!import_utils.inBrowser) {
      return;
    }
    if (options.millisecond) {
      microTick();
    } else {
      macroTick();
    }
  };
  const start = () => {
    if (!counting.current) {
      endTime.current = Date.now() + remainRef.current;
      counting.current = true;
      tick();
    }
  };
  const reset = (totalTime = options.time) => {
    pause();
    setRemain(totalTime);
    remainRef.current = totalTime;
  };
  (0, import_react.useEffect)(() => {
    return () => {
      pause();
    };
  }, []);
  return {
    start,
    pause,
    reset,
    current
  };
}
