import { useCallback, useEffect, useLayoutEffect, useRef, useState, } from "react";
import { useIframeOverlay, } from "./IframeOverlayProvider";
export function useOverlayMeasurement(measure, deps, options) {
    const overlay = useIframeOverlay();
    const measureRef = useRef(measure);
    const equalsRef = useRef(options.equals);
    const [value, setValue] = useState(options.initialValue);
    useEffect(() => {
        measureRef.current = measure;
        equalsRef.current = options.equals;
    }, [measure, options.equals]);
    const runMeasurement = useCallback((metrics, reasons) => {
        const next = measureRef.current(metrics, reasons);
        setValue((previous) => equalsRef.current(previous, next) ? previous : next);
    }, []);
    useLayoutEffect(() => {
        if (!options.validateBeforePaint)
            return;
        runMeasurement(overlay.getMetrics(), new Set(options.interests));
    });
    useEffect(() => {
        runMeasurement(overlay.getMetrics(), new Set(options.interests));
        return overlay.subscribeMeasurement(runMeasurement, options.interests);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [overlay, runMeasurement, ...deps]);
    return value;
}
//# sourceMappingURL=useOverlayMeasurement.js.map