"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.addListener = addListener;exports.emit = emit;var _eventemitter = _interopRequireDefault(require("eventemitter3"));
var _reactDom = require("react-dom");
var _globalObject = require("@skbkontur/global-object");

var emitterCache;
function getEmitter() {
  if (!emitterCache) {
    emitterCache = new _eventemitter.default();
  }
  return emitterCache;
}

function listenBrowserEvents() {
  _globalObject.globalObject.addEventListener == null || _globalObject.globalObject.addEventListener('scroll', emit, { capture: true });
  _globalObject.globalObject.addEventListener == null || _globalObject.globalObject.addEventListener('resize', emit, { capture: true });
}

function unlistenBrowserEvents() {
  _globalObject.globalObject.removeEventListener == null || _globalObject.globalObject.removeEventListener('scroll', emit, { capture: true });
  _globalObject.globalObject.removeEventListener == null || _globalObject.globalObject.removeEventListener('resize', emit, { capture: true });
}

function addListener(callback) {
  var emitter = getEmitter();
  if (emitter.listeners('layout').length === 0) {
    listenBrowserEvents();
  }
  emitter.addListener('layout', callback);
  return {
    remove: function remove() {
      emitter.removeListener('layout', callback);
      if (emitter.listeners('layout').length === 0) {
        unlistenBrowserEvents();
      }
    }
  };
}

function emit() {
  (0, _reactDom.unstable_batchedUpdates)(function () {
    getEmitter().emit('layout');
  });
}