import { useContext, useEffect, useState, useRef } from 'react';
import { AsyncMountContext } from '../../contexts/AsyncMountContext';
var queueSize = 0;
export var useAsyncMount = function () {
    var isFirstRunRef = useRef(true);
    var _a = useContext(AsyncMountContext), enabled = _a.enabled, _b = _a.queueSizeThreshold, queueSizeThreshold = _b === void 0 ? 0 : _b;
    var _c = useState(!enabled || (enabled && queueSize < queueSizeThreshold)), shouldMount = _c[0], setShouldMount = _c[1];
    if (isFirstRunRef.current) {
        queueSize++;
    }
    var timerRef = useRef(null);
    useEffect(function () {
        queueSize--;
        if (!shouldMount) {
            timerRef.current = setTimeout(function () {
                setShouldMount(true);
            }, 0);
            return function () { return clearTimeout(timerRef.current); };
        }
    }, []); // eslint-disable-line react-hooks/exhaustive-deps
    isFirstRunRef.current = false;
    return shouldMount;
};
