import { t as debounce } from "./debounce.js";
import { useEffect, useRef, useState } from "react";
const useSize = (ref) => {
	const ticking = useRef(false);
	const [size, setSize] = useState();
	useEffect(() => {
		if (typeof ResizeObserver !== "undefined") {
			const resizeObserver = new ResizeObserver((entries) => {
				if (!ticking.current) window.requestAnimationFrame(() => {
					setSize(entries[0].target.getBoundingClientRect());
					ticking.current = false;
				});
				ticking.current = true;
			});
			if (ref?.current) resizeObserver.observe(ref?.current);
			return () => {
				if (ref?.current) resizeObserver.unobserve(ref.current);
				else resizeObserver.disconnect();
			};
		} else if (typeof window === "object") {
			const handleLayoutEvent = () => {
				if (ref?.current) setSize(ref.current.getBoundingClientRect());
			};
			const events = [
				"layoutchange",
				"resize",
				"orientationchange"
			];
			const [debouncedCallback, teardown] = debounce(handleLayoutEvent, 10);
			events.forEach((eventName) => window.addEventListener(eventName, debouncedCallback));
			debouncedCallback();
			return () => {
				teardown();
				events.forEach((eventName) => window.removeEventListener(eventName, debouncedCallback));
			};
		}
	}, [ref?.current]);
	return size;
};
export { useSize as t };

//# sourceMappingURL=useSize.js.map