"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _react = _interopRequireDefault(require("react"));

var ResponsiveMap = {
  xs: '(max-width: 575px)',
  sm: '(min-width: 576px)',
  md: '(min-width: 768px)',
  lg: '(min-width: 992px)',
  xl: '(min-width: 1200px)',
  xxl: '(min-width: 1600px)'
};
var mqlListenerMap = {};

function useResponsive() {
  var _React$useState = _react.default.useState(null),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      activeBreakpoint = _React$useState2[0],
      setActiveBreakpoint = _React$useState2[1];

  function initMediaListeners() {
    Object.keys(ResponsiveMap).forEach(function (breakpoint) {
      var mql = window.matchMedia(ResponsiveMap[breakpoint]);

      var listener = function listener(_ref) {
        var matches = _ref.matches;

        if (matches) {
          setActiveBreakpoint(breakpoint);
        }
      };

      listener(mql);

      if (!mql.addListener) {
        mql.addEventListener('change', listener);
      } else {
        mql.addListener(listener);
      }

      mqlListenerMap[breakpoint] = {
        mql: mql,
        listener: listener
      };
    });
  }

  function removeListeners() {
    Object.keys(mqlListenerMap).forEach(function (breakpoint) {
      var _mqlListenerMap$break = mqlListenerMap[breakpoint],
          mql = _mqlListenerMap$break.mql,
          listener = _mqlListenerMap$break.listener;

      if (listener) {
        if (!mql.removeListener) {
          mql.removeEventListener('change', listener);
        } else {
          mql.removeListener(listener);
        }
      }
    });
  }

  _react.default.useEffect(function () {
    initMediaListeners();
    return removeListeners;
  }, []);

  return [activeBreakpoint];
}

var _default = useResponsive;
exports.default = _default;