"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.ModalStack = void 0;
var _eventemitter = _interopRequireDefault(require("eventemitter3"));
var _globalObject = require("@skbkontur/global-object");var

















ModalStack = exports.ModalStack = /*#__PURE__*/function () {function ModalStack() {}ModalStack.
  add = function add(
  component,
  onChange)
  {
    var _ModalStack$getStackI = ModalStack.getStackInfo(),emitter = _ModalStack$getStackI.emitter,mounted = _ModalStack$getStackI.mounted;
    mounted.unshift(component);
    var changeHandler = function changeHandler() {return onChange([].concat(mounted));};
    var _token = emitter.addListener('change', changeHandler);
    emitter.emit('change');
    return {
      remove: function remove() {
        // 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 remove(component) {
    var _ModalStack$getStackI2 = ModalStack.getStackInfo(),emitter = _ModalStack$getStackI2.emitter,mounted = _ModalStack$getStackI2.mounted;
    var index = mounted.indexOf(component);
    if (index !== -1) {
      mounted.splice(index, 1);
    }
    emitter.emit('change');
  };ModalStack.
  rerender = function rerender() {
    var _ModalStack$getStackI3 = ModalStack.getStackInfo(),emitter = _ModalStack$getStackI3.emitter;
    emitter.emit('change');
  }

  /**
   * Determines if stack component is allowed to block background
   */;ModalStack.
  isBlocking = function isBlocking(component) {
    var _ModalStack$getStackI4 = ModalStack.getStackInfo(),mounted = _ModalStack$getStackI4.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 getStackInfo() {
    var globalWithStack = _globalObject.globalObject;
    return (
      globalWithStack.__ReactUIStackInfo || (
      globalWithStack.__ReactUIStackInfo = {
        emitter: new _eventemitter.default(),
        mounted: []
      }));

  };ModalStack.

  wantsToBlock = function wantsToBlock(component) {
    if (isModal(component)) {
      return true;
    }

    if (isSidePage(component)) {
      return !!component.props.blockBackground;
    }

    return false;
  };return ModalStack;}();


/**
 * Specific check for component type by its instance
 */
var isReactUIInstance = function isReactUIInstance(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');