"use strict";

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

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

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

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

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

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

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

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

var _react = require("react");

var _isObject = _interopRequireDefault(require("lodash/isObject"));

var _debounce = _interopRequireDefault(require("lodash/debounce"));

var _mobx = require("mobx");

var _mediaQueryListPolyfill = require("../_util/mediaQueryListPolyfill");

var _enum = require("./enum");

var _responsiveMap;

var enquire;

if (typeof window !== 'undefined') {
  window.matchMedia = window.matchMedia || _mediaQueryListPolyfill.matchMediaPolifill; // eslint-disable-next-line global-require

  enquire = require('enquire.js');
}

var responsiveMap = (_responsiveMap = {}, (0, _defineProperty2["default"])(_responsiveMap, _enum.Breakpoint.xxl, '(min-width: 1600px)'), (0, _defineProperty2["default"])(_responsiveMap, _enum.Breakpoint.xl, '(min-width: 1200px)'), (0, _defineProperty2["default"])(_responsiveMap, _enum.Breakpoint.lg, '(min-width: 992px)'), (0, _defineProperty2["default"])(_responsiveMap, _enum.Breakpoint.md, '(min-width: 768px)'), (0, _defineProperty2["default"])(_responsiveMap, _enum.Breakpoint.sm, '(min-width: 576px)'), (0, _defineProperty2["default"])(_responsiveMap, _enum.Breakpoint.xs, '(max-width: 575px)'), _responsiveMap);
var responsiveArray = Object.keys(responsiveMap);

function isBreakPointMap(item) {
  if ((0, _isObject["default"])(item)) {
    var keys = Object.keys(item);

    if (keys.length) {
      return keys.every(function (key) {
        return key in responsiveMap;
      });
    }
  }

  return false;
}

function hasBreakPointMap(items) {
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
  return items.some(isOrHasBreakPointMap);
}

function isOrHasBreakPointMap(item) {
  return (0, _mobx.isArrayLike)(item) ? hasBreakPointMap(item) : isBreakPointMap(item);
}

var Responsive = /*#__PURE__*/function (_PureComponent) {
  (0, _inherits2["default"])(Responsive, _PureComponent);

  var _super = (0, _createSuper2["default"])(Responsive);

  function Responsive() {
    var _this;

    (0, _classCallCheck2["default"])(this, Responsive);
    _this = _super.apply(this, arguments);
    _this.state = {
      breakpoints: {}
    };
    _this.handlers = new Map();
    _this.fireUpdate = (0, _debounce["default"])(function () {
      var onChange = _this.props.onChange;

      if (onChange) {
        onChange(_this.getValues());
      }
    }, 10);
    return _this;
  }

  (0, _createClass2["default"])(Responsive, [{
    key: "isDisabled",
    value: function isDisabled(props) {
      var disabled = props.disabled,
          items = props.items;

      if (!disabled && items) {
        return !hasBreakPointMap(items);
      }

      return true;
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      if (!this.isDisabled(this.props)) {
        this.register();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var props = this.props;

      if (!this.isDisabled(prevProps) && this.isDisabled(props)) {
        this.unregister();
      }

      if (this.isDisabled(prevProps) && !this.isDisabled(props)) {
        this.register();
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (!this.isDisabled(this.props)) {
        this.unregister();
      }
    }
  }, {
    key: "register",
    value: function register() {
      var _this2 = this;

      if (enquire) {
        var handlers = this.handlers;
        responsiveArray.forEach(function (breakpoint) {
          var query = responsiveMap[breakpoint];
          var handler = {
            match: function match() {
              _this2.setState(function (prevState) {
                return {
                  breakpoints: (0, _objectSpread4["default"])((0, _objectSpread4["default"])({}, prevState.breakpoints), {}, (0, _defineProperty2["default"])({}, breakpoint, true))
                };
              }, _this2.fireUpdate);
            },
            unmatch: function unmatch() {
              _this2.setState(function (prevState) {
                return {
                  breakpoints: (0, _objectSpread4["default"])((0, _objectSpread4["default"])({}, prevState.breakpoints), {}, (0, _defineProperty2["default"])({}, breakpoint, false))
                };
              }, _this2.fireUpdate);
            },
            // Keep a empty destory to avoid triggering unmatch when unregister
            destroy: function destroy() {}
          };
          enquire.register(query, handler);
          handlers.set(query, handler);
        });
      }
    }
  }, {
    key: "unregister",
    value: function unregister() {
      var handlers = this.handlers;
      Object.keys(responsiveMap).forEach(function (breakpoint) {
        var query = responsiveMap[breakpoint];
        var handler = handlers.get(query);

        if (handler) {
          enquire.unregister(query, handler);
        }
      });
    }
  }, {
    key: "processValue",
    value: function processValue(value) {
      var breakpoints = this.state.breakpoints;

      if ((0, _mobx.isArrayLike)(value) && hasBreakPointMap(value)) {
        return value.map(this.processValue, this);
      }

      if (isBreakPointMap(value)) {
        for (var i = 0; i < responsiveArray.length; i++) {
          var breakpoint = responsiveArray[i];

          if (breakpoints[breakpoint] && value[breakpoint] !== undefined) {
            return value[breakpoint];
          }
        }

        return undefined;
      }

      return value;
    }
  }, {
    key: "getValues",
    value: function getValues() {
      var items = this.props.items;

      if (items) {
        return items.map(this.processValue, this);
      }

      return [];
    }
  }, {
    key: "render",
    value: function render() {
      var props = this.props;
      var children = props.children;

      if (typeof children === 'function') {
        return children(this.getValues(), this.isDisabled(props));
      }

      return children;
    }
  }]);
  return Responsive;
}(_react.PureComponent);

exports["default"] = Responsive;
Responsive.displayName = 'Responsive';
//# sourceMappingURL=Responsive.js.map
