/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-29d58ba | Cube Dev Team */
import { keyframes } from "../injector/index.js";
import { useInsertionEffect, useMemo, useRef } from "react";

//#region src/tasty/hooks/useKeyframes.ts
function useKeyframes(stepsOrFactory, depsOrOptions, options) {
	const isFactory = typeof stepsOrFactory === "function";
	const deps = isFactory && Array.isArray(depsOrOptions) ? depsOrOptions : void 0;
	const opts = isFactory ? options : depsOrOptions;
	const stepsData = useMemo(() => {
		const steps = isFactory ? stepsOrFactory() : stepsOrFactory;
		if (!steps || Object.keys(steps).length === 0) return null;
		return steps;
	}, isFactory ? deps ?? [] : [stepsOrFactory]);
	const renderResultRef = useRef(null);
	const effectResultRef = useRef(null);
	const name = useMemo(() => {
		renderResultRef.current?.dispose();
		renderResultRef.current = null;
		if (!stepsData) return "";
		const result = keyframes(stepsData, {
			name: opts?.name,
			root: opts?.root
		});
		renderResultRef.current = result;
		return result.toString();
	}, [
		stepsData,
		opts?.name,
		opts?.root
	]);
	useInsertionEffect(() => {
		effectResultRef.current?.dispose();
		effectResultRef.current = null;
		if (stepsData) effectResultRef.current = keyframes(stepsData, {
			name: opts?.name,
			root: opts?.root
		});
		return () => {
			effectResultRef.current?.dispose();
			effectResultRef.current = null;
			renderResultRef.current?.dispose();
			renderResultRef.current = null;
		};
	}, [
		stepsData,
		opts?.name,
		opts?.root
	]);
	return name;
}

//#endregion
export { useKeyframes };
//# sourceMappingURL=useKeyframes.js.map