"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAsyncMount = void 0;
var react_1 = require("react");
var AsyncMountContext_1 = require("../../contexts/AsyncMountContext");
var queueSize = 0;
var useAsyncMount = function () {
    var isFirstRunRef = (0, react_1.useRef)(true);
    var _a = (0, react_1.useContext)(AsyncMountContext_1.AsyncMountContext), enabled = _a.enabled, _b = _a.queueSizeThreshold, queueSizeThreshold = _b === void 0 ? 0 : _b;
    var _c = (0, react_1.useState)(!enabled || (enabled && queueSize < queueSizeThreshold)), shouldMount = _c[0], setShouldMount = _c[1];
    if (isFirstRunRef.current) {
        queueSize++;
    }
    var timerRef = (0, react_1.useRef)(null);
    (0, react_1.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;
};
exports.useAsyncMount = useAsyncMount;
