var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import { EventEmitter } from 'eventemitter3';
var ModalStack = /** @class */ (function () {
    function ModalStack() {
    }
    ModalStack.add = function (component, onChange, globalObject) {
        var _a = ModalStack.getStackInfo(globalObject), emitter = _a.emitter, mounted = _a.mounted;
        mounted.unshift(component);
        var changeHandler = function () { return onChange(__spreadArray([], mounted, true)); };
        var _token = emitter.addListener('change', changeHandler);
        emitter.emit('change');
        return {
            remove: function () {
                // Backwards compatible with versions 0.x and 1.w which using the fbemitter package
                if ('remove' in _token) {
                    // @ts-expect-error: `remove` is method of `ModalStack`.
                    _token.remove();
                    return;
                }
                emitter.removeListener('change', changeHandler);
            },
        };
    };
    ModalStack.remove = function (component, globalObject) {
        var _a = ModalStack.getStackInfo(globalObject), emitter = _a.emitter, mounted = _a.mounted;
        var index = mounted.indexOf(component);
        if (index !== -1) {
            mounted.splice(index, 1);
        }
        emitter.emit('change');
    };
    ModalStack.rerender = function (globalObject) {
        var emitter = ModalStack.getStackInfo(globalObject).emitter;
        emitter.emit('change');
    };
    /**
     * Determines if stack component is allowed to block background
     */
    ModalStack.isBlocking = function (component, globalObject) {
        var mounted = ModalStack.getStackInfo(globalObject).mounted;
        for (var index = 0; index < mounted.length; index++) {
            if (ModalStack.wantsToBlock(mounted[index])) {
                // only the highest component in stack
                // that wants to block is allowed to do it
                return component === mounted[index];
            }
        }
        return false;
    };
    ModalStack.getStackInfo = function (globalObject) {
        var globalWithStack = globalObject;
        return (globalWithStack.__ReactUIStackInfo ||
            (globalWithStack.__ReactUIStackInfo = {
                emitter: new EventEmitter(),
                mounted: [],
            }));
    };
    ModalStack.wantsToBlock = function (component) {
        if (isModal(component)) {
            return true;
        }
        if (isSidePage(component)) {
            return !!component.props.blockBackground;
        }
        return false;
    };
    return ModalStack;
}());
export { ModalStack };
/**
 * Specific check for component type by its instance
 */
var isReactUIInstance = function (componentName) {
    return function (instance) {
        var constructor = instance.constructor;
        return (Object.prototype.hasOwnProperty.call(constructor, '__KONTUR_REACT_UI__') &&
            // @ts-expect-error: React doesn't know about existence of __KONTUR_REACT_UI__.
            constructor.__KONTUR_REACT_UI__ === componentName);
    };
};
var isSidePage = isReactUIInstance('SidePage');
var isModal = isReactUIInstance('Modal');
//# sourceMappingURL=ModalStack.js.map