import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2007-2025 GoodData Corporation
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { throttle } from "lodash-es";
import { elementRegion } from "../utils/domUtilities.js";
/**
 * @internal
 */
export function AutoSize({ children }) {
    const [state, setState] = useState({
        width: 0,
        height: 0,
    });
    const wrapperRef = useRef(null);
    const updateSize = useCallback(() => {
        const { width, height } = elementRegion(wrapperRef.current);
        setState({
            width,
            height,
        });
    }, []);
    const throttledUpdateSize = useMemo(() => throttle(updateSize, 250, { leading: false }), [updateSize]);
    useEffect(() => {
        window.addEventListener("resize", throttledUpdateSize);
        throttledUpdateSize();
        return () => {
            throttledUpdateSize.cancel();
            window.removeEventListener("resize", throttledUpdateSize);
        };
    }, [throttledUpdateSize]);
    const { width, height } = state;
    return (_jsx("div", { ref: wrapperRef, style: { height: "100%", width: "100%" }, children: children({ width, height }) }));
}
//# sourceMappingURL=AutoSize.js.map