require("../../../_virtual/_rolldown/runtime.js");
let react = require("react");
//#region src/components/PisellProcedure/hooks/useElementSize.ts
/**
* 元素尺寸计算 Hook（宽度和高度）
*
* @description 计算指定元素的宽度和高度，并监听尺寸变化自动更新
* 使用 ResizeObserver 和防抖机制优化性能，减少不必要的更新
*
* @example
* const { width, height } = useElementSize({
*   selector: '.modal-container',
*   defaultWidth: 0,
*   defaultHeight: 0
* });
*/
const useElementSize = ({ selector, enabled = true, defaultWidth = 0, defaultHeight = 0, debounceMs = 100 }) => {
	const [size, setSize] = (0, react.useState)({
		width: defaultWidth,
		height: defaultHeight
	});
	const handScale = () => {
		var _getComputedStyle;
		const bodyTransform = (_getComputedStyle = getComputedStyle(document.body)) === null || _getComputedStyle === void 0 ? void 0 : _getComputedStyle.transform;
		let scaleY = 1;
		let scaleX = 1;
		if (bodyTransform !== "none") {
			const matrix = new DOMMatrix(bodyTransform);
			scaleX = matrix.a;
			scaleY = matrix.d;
		}
		return {
			scaleX,
			scaleY
		};
	};
	(0, react.useLayoutEffect)(() => {
		if (!enabled || typeof window === "undefined" || typeof document === "undefined") return;
		if (!selector || !selector.trim()) {
			setSize({
				width: defaultWidth,
				height: defaultHeight
			});
			return;
		}
		const handleResize = () => {
			setTimeout(() => {
				let element = null;
				try {
					element = document.querySelector(selector);
					const rect = element === null || element === void 0 ? void 0 : element.getBoundingClientRect();
					if (rect) {
						const { scaleX } = handScale();
						const scaledWidth = ((element === null || element === void 0 ? void 0 : element.clientWidth) || 0) * scaleX;
						setSize({
							width: Math.round(scaledWidth * 100) / 100,
							height: Math.round((rect === null || rect === void 0 ? void 0 : rect.height) * 100) / 100
						});
					}
				} catch (error) {
					setSize({
						width: defaultWidth,
						height: defaultHeight
					});
					return;
				}
			}, debounceMs);
		};
		handleResize();
		window.addEventListener("resize", handleResize);
		return () => {
			window.removeEventListener("resize", handleResize);
		};
	}, [
		selector,
		enabled,
		defaultWidth,
		defaultHeight,
		debounceMs
	]);
	return size;
};
//#endregion
exports.useElementSize = useElementSize;
