'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

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 _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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 FitwidthMixin = function FitwidthMixin(superclass) {
  return function (_superclass) {
    _inherits(_class, _superclass);

    function _class() {
      _classCallCheck(this, _class);

      return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
    }

    _createClass(_class, [{
      key: 'componentDidMount',
      value: function componentDidMount() {
        // set state for the width of the images and listen to window resize event
        if (_reactDom2.default.findDOMNode(this)) {
          this.fitToParentWidth();
          window.addEventListener('resize', this.fitToParentWidth);
        }
      }
    }, {
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        _reactDom2.default.findDOMNode(this) && window.removeEventListener('resize', this.fitToParentWidth);
      }
    }, {
      key: 'fitToParentWidth',
      value: function fitToParentWidth() {
        var elem = _reactDom2.default.findDOMNode(this).parentNode;
        var width = elem.clientWidth;
        if (width !== this.state.width) {
          this.setState({
            width: width
          });
        }
      }
    }, {
      key: '_renderByDevice',
      value: function _renderByDevice(screenType, imageByDevice, imgStyle) {
        switch (screenType) {
          case 'MOBILE':
            return this._renderFigure(imageByDevice.mobile, imgStyle);
          case 'TABLET':
            return this._renderFigure(imageByDevice.tablet, imgStyle);
          case 'DESKTOP':
            return this._renderFigure(imageByDevice.desktop, imgStyle);
          default:
            return this._renderFigure(imageByDevice.mobile, imgStyle);
        }
      }
    }, {
      key: '_getHeight',
      value: function _getHeight(width, original, defaultWidth, defaultHeight) {
        if (original) {
          var oriWidth = _lodash2.default.get(original, 'width', defaultWidth);
          var oriHeight = _lodash2.default.get(original, 'height', defaultHeight);
          return Math.round(width * oriHeight / oriWidth);
        }
        return defaultHeight;
      }
    }]);

    return _class;
  }(superclass);
};

exports.default = FitwidthMixin;