"use strict";
/*
 * Copyright 2024 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.useOverlayStack = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importDefault(require("react"));
var common_1 = require("../../common");
var errors_1 = require("../../common/errors");
var utils_1 = require("../../common/utils");
var overlaysProvider_1 = require("../../context/overlays/overlaysProvider");
var useLegacyOverlayStack_1 = require("./useLegacyOverlayStack");
/**
 * React hook to interact with the global overlay stack.
 *
 * @see https://blueprintjs.com/docs/#core/hooks/use-overlay-stack
 */
function useOverlayStack() {
    // get the overlay stack from application-wide React context
    var _a = react_1.default.useContext(overlaysProvider_1.OverlaysContext), stack = _a.stack, hasProvider = _a.hasProvider;
    var legacyOverlayStack = (0, useLegacyOverlayStack_1.useLegacyOverlayStack)();
    var getLastOpened = react_1.default.useCallback(function () {
        return stack.current[stack.current.length - 1];
    }, [stack]);
    var getThisOverlayAndDescendants = react_1.default.useCallback(function (id) {
        var index = stack.current.findIndex(function (o) { return o.id === id; });
        if (index === -1) {
            return [];
        }
        return stack.current.slice(index);
    }, [stack]);
    var resetStack = react_1.default.useCallback(function () {
        stack.current = [];
    }, [stack]);
    var openOverlay = react_1.default.useCallback(function (overlay) {
        stack.current.push(overlay);
        if (overlay.props.usePortal && overlay.props.hasBackdrop) {
            // add a class to the body to prevent scrolling of content below the overlay
            document.body.classList.add(common_1.Classes.OVERLAY_OPEN);
        }
    }, [stack]);
    var closeOverlay = react_1.default.useCallback(function (id) {
        var otherOverlaysWithBackdrop = stack.current.filter(function (o) { return o.props.usePortal && o.props.hasBackdrop && o.id !== id; });
        var index = stack.current.findIndex(function (o) { return o.id === id; });
        if (index > -1) {
            stack.current.splice(index, 1);
        }
        if (otherOverlaysWithBackdrop.length === 0) {
            // remove body class which prevents scrolling of content below overlay
            document.body.classList.remove(common_1.Classes.OVERLAY_OPEN);
        }
    }, [stack]);
    if (!hasProvider) {
        if ((0, utils_1.isNodeEnv)("development")) {
            console.error(errors_1.OVERLAY2_REQUIRES_OVERLAY_PROVDER);
        }
        return legacyOverlayStack;
    }
    return {
        closeOverlay: closeOverlay,
        getLastOpened: getLastOpened,
        getThisOverlayAndDescendants: getThisOverlayAndDescendants,
        openOverlay: openOverlay,
        resetStack: resetStack,
    };
}
exports.useOverlayStack = useOverlayStack;
//# sourceMappingURL=useOverlayStack.js.map