/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

require("../../../_virtual/_rolldown/runtime.js");
const require_useIsomorphicEffect = require("./useIsomorphicEffect.js");
let react = require("react");
//#region src/global/js/hooks/useResizeObserver.ts
/**
* Copyright IBM Corp. 2023, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const useResizeObserver = (ref, onResize) => {
	const [width, setWidth] = (0, react.useState)(-1);
	const [height, setHeight] = (0, react.useState)(-1);
	const entriesToHandle = (0, react.useRef)(null);
	const cb = (0, react.useRef)(onResize);
	(0, react.useEffect)(() => {
		cb.current = onResize;
	}, [onResize]);
	(0, react.useEffect)(() => {
		const getInitialSize = () => {
			if (ref.current) {
				const refComputedStyle = window.getComputedStyle(ref.current);
				const initialWidth = (ref.current?.offsetWidth ?? 0) - (typeof refComputedStyle?.paddingLeft === "string" && refComputedStyle?.paddingLeft.length ? parseFloat(refComputedStyle?.paddingLeft) : 0) - (typeof refComputedStyle?.paddingRight === "string" && refComputedStyle?.paddingRight.length ? parseFloat(refComputedStyle?.paddingRight) : 0);
				const initialHeight = (ref.current?.offsetHeight ?? 0) - (typeof refComputedStyle?.paddingTop === "string" && refComputedStyle?.paddingTop.length ? parseFloat(refComputedStyle?.paddingTop) : 0) - (typeof refComputedStyle?.paddingBottom === "string" && refComputedStyle?.paddingBottom.length ? parseFloat(refComputedStyle?.paddingBottom) : 0);
				setWidth(initialWidth);
				setHeight(initialHeight);
			}
		};
		if (!ref?.current || width >= 0 && height >= 0) return;
		getInitialSize();
	}, [width, height]);
	require_useIsomorphicEffect.useIsomorphicEffect(() => {
		if (!ref?.current) return;
		const doCallbacks = () => {
			if (!ref?.current || !Array.isArray(entriesToHandle?.current)) return;
			const entry = entriesToHandle.current[0];
			setWidth(entry.contentRect.width);
			setHeight(entry.contentRect.height);
			cb.current && cb.current(entry.contentRect);
		};
		const observer = new ResizeObserver((entries) => {
			entriesToHandle.current = entries;
			window.requestAnimationFrame(() => {
				doCallbacks();
			});
		});
		observer.observe(ref.current);
		return () => {
			observer.disconnect();
		};
	}, [ref.current]);
	return {
		width,
		height
	};
};
//#endregion
exports.useResizeObserver = useResizeObserver;
