/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { useMemo } from "react";

//#region src/hooks/useForkRef.ts
const setRef = (ref, value) => {
	if (typeof ref === "function") ref(value);
	else if (ref) ref.current = value;
};
const useForkRef = (...refs) => {
	/**
	* This will create a new function if the refs passed to this hook change and are all defined.
	* This means react will call the old forkRef with `null` and the new forkRef
	* with the ref. Cleanup naturally emerges from this behavior.
	*/
	return useMemo(() => {
		if (refs.every((ref) => ref == null)) return null;
		return (instance) => {
			refs.forEach((ref) => {
				setRef(ref, instance);
			});
		};
	}, [refs]);
};

//#endregion
export { useForkRef };