/** @license MIT | @cube-dev/ui-kit v0.123.0 | Cube Dev Team */
import { useEvent } from "../use-event.js";
import { Timer } from "./timer.js";
import { useEffect, useState } from "react";

//#region src/_internal/hooks/use-timer/use-timer.ts
function useTimer(props = {}) {
	const { callback, delay, isDisabled, timer: propsTimer = null } = props;
	const callbackEvent = useEvent(() => callback?.());
	const [timer, setTimer] = useState(() => {
		if (propsTimer) return propsTimer;
		if (typeof delay === "number") return new Timer(callbackEvent, delay);
		return null;
	});
	if (isDisabled) timer?.reset();
	else timer?.resume();
	useEffect(() => {
		setTimer(propsTimer);
		return () => timer?.reset();
	}, [propsTimer]);
	useEffect(() => {
		if (propsTimer) return;
		if (typeof delay === "number") setTimer(new Timer(callbackEvent, delay));
		return () => timer?.reset();
	}, [delay, propsTimer]);
	useEffect(() => () => timer?.reset(), [timer]);
	return { timer };
}

//#endregion
export { useTimer };
//# sourceMappingURL=use-timer.js.map