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

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

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 _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

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

var _reactRouter = require('react-router');

var _stringProcessor = require('../../lib/string-processor');

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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 _reactLazyLoad = require('react-lazy-load');

var _reactLazyLoad2 = _interopRequireDefault(_reactLazyLoad);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _BottomRelateds = {
  "bottomRelatedsWrapper": "BottomRelateds__bottomRelatedsWrapper___1pxho",
  "topicWrapper": "BottomRelateds__topicWrapper___2zRkr",
  "relatedItem": "BottomRelateds__relatedItem___3hw7u",
  "fade-in-down": "BottomRelateds__fade-in-down___3XJsZ",
  "relatedAnchor": "BottomRelateds__relatedAnchor___3Vbor",
  "relatedContent": "BottomRelateds__relatedContent___2sH2L",
  "relatedTitle": "BottomRelateds__relatedTitle___C3PAq",
  "relatedDescription": "BottomRelateds__relatedDescription___3Ix3c",
  "loadMore": "BottomRelateds__loadMore___1e4zv",
  "relatedImgWrapper": "BottomRelateds__relatedImgWrapper___fHvlh",
  "relatedImg": "BottomRelateds__relatedImg___33bvO",
  "crop": "BottomRelateds__crop___25OsH",
  "fade-in": "BottomRelateds__fade-in___2dJa4",
  "fade-out": "BottomRelateds__fade-out___3A2ya",
  "fade-in-up": "BottomRelateds__fade-in-up___1gcef",
  "fade-out-down": "BottomRelateds__fade-out-down___3C_ZV",
  "fade-in-left": "BottomRelateds__fade-in-left___2St5y",
  "fade-out-left": "BottomRelateds__fade-out-left___3cw0R",
  "fade-in-right": "BottomRelateds__fade-in-right___1T-Pa",
  "fade-out-right": "BottomRelateds__fade-out-right___1Bu8w"
};

var _BottomRelateds2 = _interopRequireDefault(_BottomRelateds);

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 BottomRelateds = exports.BottomRelateds = function (_Component) {
  _inherits(BottomRelateds, _Component);

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

    var itemWidth = 420;
    var relateds = _lodash2.default.get(props, 'relateds', []);
    var count = relateds.length || 0;

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

    _this.totalWidth = Math.max(count * itemWidth, 0);
    _this.state = {
      width: 'auto',
      isCollapse: false
    };
    return _this;
  }

  _createClass(BottomRelateds, [{
    key: '_setHtml',
    value: function _setHtml(html) {
      return { __html: html };
    }
  }, {
    key: '_handleResize',
    value: function _handleResize() {
      var totalWidth = this.totalWidth || 0;
      var wrapperWidth = this.state.width;
      var winWidth = window.innerWidth;

      if (winWidth > 767 && winWidth < totalWidth && wrapperWidth === 'auto') {
        this.setState({ width: totalWidth });
      } else if (wrapperWidth !== 'auto') {
        this.setState({ width: 'auto' });
      }
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      this._handleResize();
      window.addEventListener('resize', this._handleResize.bind(this));
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      window.removeEventListener('resize', this._handleResize.bind(this));
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props;
      var relateds = _props.relateds;
      var topicName = _props.topicName;
      var topicArr = _props.topicArr;
      var currentId = _props.currentId;
      var isCollapse = this.state.isCollapse;


      var titleText = topicArr && topicName ? topicName : _index.RELATED_ARTICLES;
      var listItems = topicArr && topicName ? topicArr : relateds;

      if (!_lodash2.default.get(relateds, '0') && (!topicArr || topicArr.length === 0)) {
        return null;
      }

      // find unique items and filter out the current article in display
      listItems = _lodash2.default.uniq(listItems);
      listItems = _lodash2.default.filter(listItems, function (related) {
        return related.id !== currentId;
      });

      var relatedRows = _lodash2.default.map(listItems, function (related, index) {
        var imageUrl = _lodash2.default.get(related, 'heroImage.image.resizedTargets.mobile.url', '/asset/review.png');
        var description = _lodash2.default.get(related, 'ogDescription', '');
        var itemDisplayClass = index >= _index.ITEMS_LIMIT.ARTICLE_RELATED && !isCollapse ? _Common2.default['hide'] : null;

        return _react2.default.createElement(
          'li',
          { className: (0, _classnames2.default)(_BottomRelateds2.default.relatedItem, itemDisplayClass), key: 'related-' + index++ },
          _react2.default.createElement(
            _reactRouter.Link,
            { className: _BottomRelateds2.default.relatedAnchor, to: '/a/' + related.slug },
            _react2.default.createElement(
              'div',
              { className: _BottomRelateds2.default.relatedImgWrapper },
              _react2.default.createElement(
                'div',
                { className: _BottomRelateds2.default.relatedImg },
                _react2.default.createElement(
                  _reactLazyLoad2.default,
                  null,
                  _react2.default.createElement('img', { className: _BottomRelateds2.default['crop'], src: imageUrl })
                )
              )
            ),
            _react2.default.createElement(
              'div',
              { className: _BottomRelateds2.default.relatedContent },
              _react2.default.createElement('p', { className: _BottomRelateds2.default.relatedTitle, dangerouslySetInnerHTML: _this2._setHtml(related.title) }),
              _react2.default.createElement(
                'p',
                { className: _BottomRelateds2.default.relatedDescription },
                (0, _stringProcessor.shortenString)(description, _index.CHARACTERS_LIMIT.BOTTOM_RELATED_DESC)
              )
            )
          )
        );
      });

      var loadMoreBtn = isCollapse || listItems.length <= _index.ITEMS_LIMIT.ARTICLE_RELATED ? null : _react2.default.createElement(
        'div',
        { className: (0, _classnames2.default)(_BottomRelateds2.default.loadMore, 'text-center'), onClick: function onClick() {
            _this2.setState({ isCollapse: true });
          } },
        _index.LOAD_MORE_ARTICLES
      );

      return _react2.default.createElement(
        'div',
        { className: (0, _classnames2.default)(_Common2.default['component'], 'center-block') },
        _react2.default.createElement(
          'div',
          { className: (0, _classnames2.default)(_BottomRelateds2.default.bottomRelatedsWrapper, _Common2.default['inner-block']) },
          _react2.default.createElement(
            'div',
            { className: (0, _classnames2.default)(_BottomRelateds2.default['topicWrapper'], 'text-center') },
            _react2.default.createElement(
              'h3',
              { className: _Common2.default['topic-box'] },
              ' ',
              titleText,
              ' '
            )
          ),
          _react2.default.createElement(
            'ul',
            null,
            relatedRows
          ),
          loadMoreBtn
        )
      );
    }
  }]);

  return BottomRelateds;
}(_react.Component);