import { useEffect } from 'react';

var useFocusWhenVisible = function (_a) {
    var ref = _a.ref, _b = _a.enabled, enabled = _b === void 0 ? true : _b, _c = _a.maxWaitTime, maxWaitTime = _c === void 0 ? 1000 : _c;
    useEffect(function () {
        var el = ref.current;
        if (!enabled || !el)
            return;
        var rafId = null;
        var timeoutId = null;
        var shouldStop = false;
        var isVisible = function () {
            if (el.offsetParent === null) {
                var style = getComputedStyle(el);
                return style.position === 'fixed' && style.visibility === 'visible';
            }
            return getComputedStyle(el).visibility === 'visible';
        };
        var cleanup = function () {
            if (rafId !== null) {
                cancelAnimationFrame(rafId);
                rafId = null;
            }
            if (timeoutId !== null) {
                clearTimeout(timeoutId);
                timeoutId = null;
            }
        };
        var checkWithPolling = function () {
            if (shouldStop)
                return;
            if (isVisible()) {
                el.focus();
                if (document.activeElement === el) {
                    shouldStop = true;
                    cleanup();
                    return;
                }
            }
            rafId = requestAnimationFrame(checkWithPolling);
        };
        rafId = requestAnimationFrame(checkWithPolling);
        timeoutId = setTimeout(function () {
            shouldStop = true;
            cleanup();
        }, maxWaitTime);
        return cleanup;
    }, [ref, enabled, maxWaitTime]);
};

export { useFocusWhenVisible as default };
//# sourceMappingURL=useFocusWhenVisible.js.map
