'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mediaQueries = exports.withMediaQueries = undefined;

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _mediaQueries = require('kitten/helpers/utils/media-queries');

var _screenConfig = require('kitten/constants/screen-config');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var viewPortTable = {
  viewportIsMobile: _screenConfig.SCREEN_SIZE_XS,
  viewportIsSOrLess: _screenConfig.SCREEN_SIZE_S,
  viewportIsTabletOrLess: _screenConfig.SCREEN_SIZE_M
};

var withMediaQueries = exports.withMediaQueries = function withMediaQueries(hocProps) {
  return function (WrapperComponent) {
    return mediaQueries(WrapperComponent, hocProps);
  };
};

var mediaQueries = exports.mediaQueries = function mediaQueries(WrappedComponent) {
  var hocProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  return function (_Component) {
    _inherits(_class2, _Component);

    function _class2(props) {
      _classCallCheck(this, _class2);

      var _this = _possibleConstructorReturn(this, (_class2.__proto__ || Object.getPrototypeOf(_class2)).call(this, props));

      _this.setExposedMethods = function (wrappedComponentInstance) {
        if (!wrappedComponentInstance) return;
        if (!hocProps.exposedMethods) return;

        hocProps.exposedMethods.forEach(function (method) {
          _this[method] = wrappedComponentInstance[method];
        });
      };

      _this.viewports = {};
      _this.state = Object.keys(hocProps).reduce(function (result, prop) {
        return _this.isInvalidProp(prop) ? result : _extends({}, result, _defineProperty({}, prop, false));
      }, {});
      return _this;
    }

    _createClass(_class2, [{
      key: 'isInvalidProp',
      value: function isInvalidProp(prop) {
        return typeof hocProps[prop] === 'boolean' && !viewPortTable[prop] || !['boolean', 'string'].includes(_typeof(hocProps[prop]));
      }
    }, {
      key: 'componentDidMount',
      value: function componentDidMount() {
        var _this2 = this;

        var _loop = function _loop(prop) {
          var propValue = hocProps[prop];
          if (_this2.isInvalidProp(prop)) {
            return 'break';
          }
          _this2.viewports[prop] = typeof propValue === 'boolean' ? (0, _mediaQueries.createMatchMediaMax)(viewPortTable[prop]) : (0, _mediaQueries.createMatchMedia)(propValue);
          _this2.viewports[prop].cb = function (event) {
            return _this2.setState(_defineProperty({}, prop, event.matches));
          };
          _this2.viewports[prop].addListener(_this2.viewports[prop].cb);
          _this2.viewports[prop].cb(_this2.viewports[prop]);
        };

        for (var prop in hocProps) {
          var _ret = _loop(prop);

          if (_ret === 'break') break;
        }
      }
    }, {
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        var _this3 = this;

        Object.keys(this.viewports).forEach(function (prop) {
          return _this3.viewports[prop].removeListener(_this3.viewports[prop].cb);
        });
      }
    }, {
      key: 'render',
      value: function render() {
        return _react2.default.createElement(WrappedComponent, _extends({
          ref: this.setExposedMethods
        }, this.props, this.state));
      }
    }]);

    return _class2;
  }(_react.Component);
};