'use strict';

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

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _ImageLoader = require('./ImageLoader');

var _ImageLoader2 = _interopRequireDefault(_ImageLoader);

var _CircularProgress = require('@material-ui/core/CircularProgress');

var _CircularProgress2 = _interopRequireDefault(_CircularProgress);

var _BrokenImage = require('@material-ui/icons/BrokenImage');

var _BrokenImage2 = _interopRequireDefault(_BrokenImage);

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

var _objectAssign = require('object-assign');

var _objectAssign2 = _interopRequireDefault(_objectAssign);

var _reactRedux = require('react-redux');

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

var styles = {
  wrapper: {
    width: '100%',
    height: '100%',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    top: 0,
    position: 'absolute',
    backgroundSize: 'cover',
    backgroundPosition: 'center',
    backgroundColor: '#cccccc'
  }
};

var pixelRatio = window.devicePixelRatio || 1;

var createDiv = _react2.default.createElement.bind(null, 'div');

var Image = function (_React$Component) {
  (0, _inherits3.default)(Image, _React$Component);

  function Image() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, Image);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Image.__proto__ || (0, _getPrototypeOf2.default)(Image)).call.apply(_ref, [this].concat(args))), _this), _this.state = {}, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(Image, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.onPropChange({}, this.props);
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      this.onPropChange(this.props, nextProps);
    }
  }, {
    key: 'onPropChange',
    value: function onPropChange(props, nextProps) {
      if (props.src === nextProps.src) return;
      var src = nextProps.src,
          resizer = nextProps.resizer;

      var mediaSrc = src;

      if (resizer) {
        var el = _reactDom2.default.findDOMNode(this);
        var size = roundUp(Math.max(el.offsetWidth, el.offsetHeight) * pixelRatio);
        mediaSrc = resizer(src, size);
      }

      this.setState({
        src: mediaSrc
      });
    }
  }, {
    key: 'render',
    value: function render() {
      // TODO: whitelist, not blacklist
      var props = (0, _omit2.default)(this.props, ['progress', 'src', 'style', 'resizer', 'dispatch']);
      var _props = this.props,
          style = _props.style,
          resizer = _props.resizer,
          src = _props.src;

      var previewUrl = resizer ? resizer(src, 200 * pixelRatio) : '';
      return _react2.default.createElement(_ImageLoader2.default, {
        imgProps: (0, _objectAssign2.default)({}, props, { style: style }),
        src: this.state.src,
        style: (0, _objectAssign2.default)({}, styles.wrapper, style, { backgroundImage: 'url(' + previewUrl + ')' }),
        preloader: function preloader() {
          return _react2.default.createElement(
            'div',
            { style: styles.wrapper },
            _react2.default.createElement(_CircularProgress2.default, null)
          );
        },
        wrapper: function wrapper(props, element) {
          if (!element) {
            return _react2.default.createElement(
              'div',
              { style: styles.wrapper },
              _react2.default.createElement(_BrokenImage2.default, { color: 'white' })
            );
          } else {
            return createDiv(props, element);
          }
        }
      });
    }
  }]);
  return Image;
}(_react2.default.Component);

Image.propTypes = {
  style: _propTypes2.default.object,
  resizer: _propTypes2.default.func,
  src: _propTypes2.default.string.isRequired
};

exports.default = (0, _reactRedux.connect)(function (state) {
  return { resizer: state.resizer };
})(Image);


function roundUp(v) {
  return Math.ceil(v / 50) * 50;
}
//# sourceMappingURL=Image.js.map