/*eslint no-unused-vars:0*/
'use strict';

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

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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _BlockAlignmentWrapper = require('./BlockAlignmentWrapper');

var _BlockAlignmentWrapper2 = _interopRequireDefault(_BlockAlignmentWrapper);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _FitwidthMixin2 = require('./mixins/FitwidthMixin');

var _FitwidthMixin3 = _interopRequireDefault(_FitwidthMixin2);

var _index = require('../../utils/index');

var _reactResponsive = require('react-responsive');

var _reactResponsive2 = _interopRequireDefault(_reactResponsive);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _reactDom2 = _interopRequireDefault(_reactDom);

var _Common = {
  "inner-block": "Common__inner-block___2cOrF",
  "disable-inner-block": "Common__disable-inner-block___1MLd0",
  "components": "Common__components___2fQuj",
  "component": "Common__component___3bwMg",
  "hide": "Common__hide___2HyoH",
  "desc-text-color": "Common__desc-text-color___2fV3l",
  "desc-text-block": "Common__desc-text-block___1Z4b-",
  "text-link": "Common__text-link___1jaLy",
  "image": "Common__image___AKB6W",
  "imagediff": "Common__imagediff___2j7tX",
  "slideshow": "Common__slideshow___1Fz23",
  "embeddedcode": "Common__embeddedcode___3Avmp",
  "youtube": "Common__youtube___2oq1n",
  "topic-box": "Common__topic-box___2Q-kN"
};

var _Common2 = _interopRequireDefault(_Common);

var _screenSize = require('../../constants/screen-size');

var _screenSize2 = _interopRequireDefault(_screenSize);

var _ImageDiff = {
  "diffContainer": "ImageDiff__diffContainer___39E6T",
  "imgDescription": "ImageDiff__imgDescription___oR8yZ",
  "wrapper": "ImageDiff__wrapper___2nVnQ",
  "imgContainer": "ImageDiff__imgContainer___2cYSY",
  "overlayContainer": "ImageDiff__overlayContainer___1Kxlp",
  "rangeInput": "ImageDiff__rangeInput___2qI5g",
  "sliderButton": "ImageDiff__sliderButton___3NpV7",
  "hovered": "ImageDiff__hovered___5b0UR"
};

var _ImageDiff2 = _interopRequireDefault(_ImageDiff);

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 DEFAULT_WIDTH = 200;
var DEFAULT_HEIGHT = 200;

var ImageDiff = function (_FitwidthMixin) {
  _inherits(ImageDiff, _FitwidthMixin);

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

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

    _this.state = {
      isMounted: false,
      onhovered: false,
      screenType: 'MOBILE',
      width: 200,
      placeholderOpacity: 0,
      imageOpacity: 0,
      percentage: 50
    };
    _this.fitToParentWidth = _this.fitToParentWidth.bind(_this);
    return _this;
  }

  _createClass(ImageDiff, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.setState({
        isMounted: true,
        screenType: (0, _index.getScreenType)(window.innerWidth)
      });
      if (_get(ImageDiff.prototype.__proto__ || Object.getPrototypeOf(ImageDiff.prototype), 'componentDidMount', this)) _get(ImageDiff.prototype.__proto__ || Object.getPrototypeOf(ImageDiff.prototype), 'componentDidMount', this).call(this);
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      if (_reactDom2.default.findDOMNode(this)) {
        this.fitToParentWidth();
      }
    }
  }, {
    key: '_renderFigure',
    value: function _renderFigure(imageObj, imgStyle) {
      if (_lodash2.default.get(imageObj, 'url')) {
        return _react2.default.createElement('img', {
          src: imageObj.url,
          style: imgStyle,
          className: (0, _classnames2.default)('center-block', _ImageDiff2.default.imgAbsolute)
        });
      }
      return null;
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var imageByDevice0 = _lodash2.default.get(this.props, ['content', 0], {});
      var imageByDevice1 = _lodash2.default.get(this.props, ['content', 1], {});
      var desktop = imageByDevice0.desktop;
      var _state = this.state;
      var isMounted = _state.isMounted;
      var screenType = _state.screenType;
      var width = _state.width;
      var percentage = _state.percentage;
      var onhovered = _state.onhovered;
      var _props = this.props;
      var outerWidth = _props.outerWidth;
      var outerHeight = _props.outerHeight;

      var boxWidth = outerWidth || width;
      var boxHeight = outerHeight || this._getHeight(boxWidth, desktop, DEFAULT_WIDTH, DEFAULT_HEIGHT);
      var buttonClass = null;
      var renderedFigure0 = null;
      var renderedFigure1 = null;
      var imageDescription = _lodash2.default.get(imageByDevice0, 'description', null);
      var descriptionBox = void 0;

      var outerStyle = {
        width: boxWidth,
        minHeight: boxHeight
      };
      var imgStyle = _extends({}, outerStyle, {
        height: boxHeight
      });

      if (onhovered) {
        buttonClass = _ImageDiff2.default.hovered;
      }

      // if the Image is being mounted, select image to render
      // according to the device of the client
      if (isMounted) {
        renderedFigure0 = this._renderByDevice(screenType, imageByDevice0, imgStyle);
        renderedFigure1 = this._renderByDevice(screenType, imageByDevice1, imgStyle);
      }

      if (imageDescription) {
        descriptionBox = _react2.default.createElement(
          'div',
          { className: (0, _classnames2.default)(_Common2.default['desc-text-block']) },
          imageDescription
        );
      }

      return _react2.default.createElement(
        'div',
        { ref: 'imgDiff', className: _ImageDiff2.default.diffContainer },
        _react2.default.createElement(
          'div',
          { style: outerStyle },
          _react2.default.createElement(
            'figure',
            { className: _ImageDiff2.default.wrapper },
            _react2.default.createElement(
              'div',
              { className: _ImageDiff2.default.imgContainer },
              renderedFigure0
            ),
            _react2.default.createElement(
              'div',
              { className: _ImageDiff2.default.overlayContainer, style: { width: percentage + '%' } },
              renderedFigure1
            ),
            _react2.default.createElement('img', { src: '/asset/slider-button.svg',
              className: (0, _classnames2.default)(_ImageDiff2.default.sliderButton, buttonClass), style: { left: percentage + '%' } }),
            _react2.default.createElement('input', { type: 'range', min: '0', max: '100', className: _ImageDiff2.default.rangeInput, style: imgStyle,
              value: percentage, onChange: function onChange(event) {
                _this2.setState({ percentage: parseInt(event.target.value) });
              },
              onMouseOver: function onMouseOver() {
                _this2.setState({ onhovered: true });
              },
              onMouseOut: function onMouseOut() {
                _this2.setState({ onhovered: false });
              } })
          )
        ),
        descriptionBox
      );
    }
  }]);

  return ImageDiff;
}((0, _FitwidthMixin3.default)(_react.Component));

ImageDiff.propTypes = {
  content: _react2.default.PropTypes.array,
  customeStyles: _react2.default.PropTypes.array,
  id: _react2.default.PropTypes.string
};

ImageDiff.defaultProps = {
  content: [],
  customeStyles: [],
  id: ''
};

var AlignedImageDiff = (0, _BlockAlignmentWrapper2.default)(ImageDiff);

exports.AlignedImageDiff = AlignedImageDiff;
exports.ImageDiff = ImageDiff;