"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.addResponsiveLayoutListener = addResponsiveLayoutListener;exports.checkMatches = checkMatches;exports.eventListenersMap = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _globalObject = require("@skbkontur/global-object");










var eventListenersMap = exports.eventListenersMap = new Map();

function addResponsiveLayoutListener(
mediaQuery,
callback)
{
  if (eventListenersMap.has(mediaQuery)) {
    addCallbackToMQListener(mediaQuery, callback);
  } else {
    createMQListener(mediaQuery, callback);
  }

  return {
    remove: function remove() {
      removeCallbackFromMQListener(mediaQuery, callback);
    }
  };
}

function addCallbackToMQListener(mediaQuery, callback) {
  var eventListener = eventListenersMap.get(mediaQuery);

  if (eventListener) {
    eventListenersMap.set(mediaQuery, (0, _extends2.default)({},
    eventListener, {
      listeners: [].concat(eventListener.listeners, [callback]) })
    );
  }
}

function createMQListener(mediaQuery, callback) {
  var mql = _globalObject.globalObject.matchMedia == null ? void 0 : _globalObject.globalObject.matchMedia(mediaQuery);
  if (mql) {
    var newMediaQueryInfo = { mql: mql, listeners: [callback] };

    eventListenersMap.set(mediaQuery, newMediaQueryInfo);
    if (mql.addEventListener) {
      mql.addEventListener('change', changeCallback);
    } else {
      mql.addListener(changeCallback);
    }
  }
}

function removeCallbackFromMQListener(mediaQuery, callback) {
  if (eventListenersMap.has(mediaQuery)) {
    var eventListener = eventListenersMap.get(mediaQuery);

    if (eventListener) {
      var newListeners = eventListener.listeners.filter(function (listener) {return listener !== callback;});

      if (newListeners.length === 0) {
        if (eventListener.mql.removeEventListener) {
          eventListener.mql.removeEventListener('change', changeCallback);
        } else {
          eventListener.mql.removeListener(changeCallback);
        }
        eventListenersMap.delete(mediaQuery);
        return;
      }

      eventListenersMap.set(mediaQuery, (0, _extends2.default)({},
      eventListener, {
        listeners: [].concat(newListeners) })
      );
    }
  }
}

function checkMatches(mediaQuery) {var _eventListener$mql$ma;
  if (!_globalObject.globalObject.matchMedia) {
    return false;
  }

  if (!eventListenersMap.has(mediaQuery)) {var _globalObject$matchMe;
    return !!((_globalObject$matchMe = _globalObject.globalObject.matchMedia(mediaQuery)) != null && _globalObject$matchMe.matches);
  }

  var eventListener = eventListenersMap.get(mediaQuery);
  return (_eventListener$mql$ma = eventListener == null ? void 0 : eventListener.mql.matches) != null ? _eventListener$mql$ma : false;
}

function changeCallback(e) {
  var eventListener = eventListenersMap.get(e.media);

  if (eventListener) {
    eventListener.listeners.forEach(function (listener) {
      listener(e);
    });
  }
}