import { __read } from "tslib";
import { useCallback, useLayoutEffect, useState } from "react";
export var useResizeObserver = function (ref, callback) {
    var _a = __read(useState(), 2), width = _a[0], setWidth = _a[1];
    var _b = __read(useState(), 2), height = _b[0], setHeight = _b[1];
    var handleResize = useCallback(function (entries) {
        if (!Array.isArray(entries)) {
            return;
        }
        var entry = entries[0];
        setWidth(entry.contentRect.width);
        setHeight(entry.contentRect.height);
        if (callback) {
            callback(entry.contentRect);
        }
    }, [callback]);
    useLayoutEffect(function () {
        if (!ref.current) {
            return;
        }
        var RO = new ResizeObserver(function (entries) {
            return handleResize(entries);
        });
        RO.observe(ref.current);
        return function () {
            RO && RO.disconnect();
            RO = null;
        };
    }, [ref]);
    return [width, height];
};
