'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Choices = 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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _sanitizeHtml = require('sanitize-html');

var _sanitizeHtml2 = _interopRequireDefault(_sanitizeHtml);

var _truncate = require('truncate');

var _truncate2 = _interopRequireDefault(_truncate);

var _entities = require('entities');

var _entities2 = _interopRequireDefault(_entities);

var _dateformat = require('dateformat');

var _dateformat2 = _interopRequireDefault(_dateformat);

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

var _reactGa = require('react-ga');

var _reactGa2 = _interopRequireDefault(_reactGa);

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; }

if (process.env.BROWSER) {}

var Choices = function (_Component) {
  _inherits(Choices, _Component);

  function Choices(props, context) {
    _classCallCheck(this, Choices);

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

    _this._handleClick = _this._handleClick.bind(_this);
    return _this;
  }

  _createClass(Choices, [{
    key: '_handleClick',
    value: function _handleClick() {
      _reactGa2.default.event({
        category: 'home',
        action: 'click',
        label: 'choices'
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props;
      var choices = _props.choices;
      var articles = _props.articles;
      var categories = _props.categories;
      var authors = _props.authors;


      return articles ? _react2.default.createElement(
        'div',
        { className: 'container mobile-hide', style: { marginTop: '50px' } },
        _react2.default.createElement(
          'div',
          { className: 'ui text container', style: { marginBottom: '35px', paddingLeft: '1em !important', marginLeft: '0 !important' } },
          _react2.default.createElement(
            'div',
            { className: 'article-main', style: { textAlign: 'center' } },
            _react2.default.createElement(
              'h2',
              { className: 'hot-topic' },
              _react2.default.createElement('div', { className: 'colorBlock choice' }),
              _react2.default.createElement(
                'span',
                null,
                '編輯精選'
              ),
              _react2.default.createElement('div', { className: 'blue-line', style: { marginLeft: '16px', display: 'inline-block' } })
            )
          )
        ),
        _react2.default.createElement(
          'div',
          { className: 'choice-main' },
          _lodash2.default.map(_lodash2.default.take(choices, 3), function (c) {

            var a = _lodash2.default.get(articles, c, {});
            var linkStyle = _lodash2.default.get(a, 'style', '') == 'projects' ? '/projects/' : '/story/';
            var image = (0, _index.imageComposer)(a).mobileImage;
            var brief = (0, _sanitizeHtml2.default)(_lodash2.default.get(a, ['brief', 'html'], ''), { allowedTags: [] });
            var content = (0, _sanitizeHtml2.default)(_lodash2.default.get(a, ['content', 'html'], ''), { allowedTags: [] });

            var briefContent = brief.length > 0 ? brief : content;

            var writers = '文｜' + _lodash2.default.map(a.writers, 'name').join('、');
            var photographers = ' 攝影｜' + _lodash2.default.map(a.photographers, function (n) {
              return _lodash2.default.get(authors, [n, 'name'], null);
            }).join('、');
            var designers = ' 設計｜' + _lodash2.default.map(a.designers, function (n) {
              return _lodash2.default.get(authors, [n, 'name'], null);
            }).join('、');
            var engineers = ' 工程｜' + _lodash2.default.map(a.engineers, function (n) {
              return _lodash2.default.get(authors, [n, 'name'], null);
            }).join('、');

            return _react2.default.createElement(
              'div',
              { className: 'choice-block', key: 'choice' + a.id },
              _react2.default.createElement(
                'a',
                { href: linkStyle + a.slug + '/', onClick: _this2._handleClick },
                _react2.default.createElement('div', { className: 'choice-img ', style: { background: 'url(' + image + ') no-repeat center center', backgroundSize: 'cover' } })
              ),
              _react2.default.createElement(
                'div',
                { className: 'choice-cat ' },
                _lodash2.default.get(categories, [_lodash2.default.first(a.categories), 'title'])
              ),
              _react2.default.createElement(
                'div',
                { className: 'choice-content ' },
                _react2.default.createElement(
                  'a',
                  { href: linkStyle + a.slug + '/', onClick: _this2._handleClick },
                  _react2.default.createElement(
                    'h2',
                    null,
                    a.title
                  )
                ),
                _react2.default.createElement('div', { className: 'line' }),
                _react2.default.createElement(
                  'div',
                  { className: 'brief' },
                  (0, _truncate2.default)(_entities2.default.decodeHTML(briefContent), 200)
                )
              ),
              _react2.default.createElement(
                'div',
                { className: 'choice-meta ' },
                _react2.default.createElement(
                  'div',
                  { className: 'author' },
                  _react2.default.createElement(
                    'span',
                    null,
                    _lodash2.default.get(a, ['writers', 'length'], 0) > 0 ? writers + ' ' : null,
                    _lodash2.default.get(a, ['photographers', 'length'], 0) > 0 ? photographers + ' ' : null,
                    _lodash2.default.get(a, ['designers', 'length'], 0) > 0 ? designers + ' ' : null,
                    _lodash2.default.get(a, ['engineers', 'length'], 0) > 0 ? engineers + ' ' : null,
                    _lodash2.default.get(a, 'extendByline', null)
                  )
                ),
                _react2.default.createElement('div', { className: 'separator' }),
                _react2.default.createElement(
                  'div',
                  { className: 'date' },
                  (0, _dateformat2.default)(a.publishedDate, 'yyyy.mm.dd')
                )
              )
            );
          })
        ),
        _react2.default.createElement(
          'div',
          { className: 'ui three column stackable grid' },
          _lodash2.default.map(_lodash2.default.slice(choices, 3), function (c) {
            var a = _lodash2.default.get(articles, c, {});
            var linkStyle = _lodash2.default.get(a, 'style', '') == 'projects' ? '/projects/' : '/story/';
            var image = (0, _index.imageComposer)(a).mobileImage;
            var brief = (0, _sanitizeHtml2.default)(_lodash2.default.get(a, ['brief', 'html'], ''), { allowedTags: [] });
            var content = (0, _sanitizeHtml2.default)(_lodash2.default.get(a, ['content', 'html'], ''), { allowedTags: [] });

            var briefContent = brief.length > 0 ? brief : content;

            var writers = '文｜' + _lodash2.default.pluck(a.writers, 'name').join('、');

            return _react2.default.createElement(
              'div',
              { className: 'column', style: { padding: 0 }, key: 'choice' + a.id },
              _react2.default.createElement(
                'div',
                { className: 'choice-block' },
                _react2.default.createElement(
                  'a',
                  { href: linkStyle + a.slug + '/', onClick: _this2._handleClick },
                  _react2.default.createElement(
                    'div',
                    { className: 'column-choice-img', style: { background: 'url(' + image + ') no-repeat center center', backgroundSize: 'cover' } },
                    _react2.default.createElement(
                      'div',
                      { className: 'choice-cat' },
                      _lodash2.default.get(categories, [_lodash2.default.first(a.categories), 'title'])
                    )
                  )
                ),
                _react2.default.createElement(
                  'div',
                  { className: 'column-choice-content' },
                  _react2.default.createElement(
                    'a',
                    { href: linkStyle + a.slug + '/', onClick: _this2._handleClick },
                    _react2.default.createElement(
                      'h2',
                      null,
                      a.title
                    )
                  ),
                  _react2.default.createElement('div', { className: 'line' }),
                  _react2.default.createElement(
                    'div',
                    { className: 'brief' },
                    (0, _truncate2.default)(_entities2.default.decodeHTML(briefContent), 75)
                  )
                ),
                _react2.default.createElement(
                  'div',
                  { className: 'column-choice-meta' },
                  _react2.default.createElement(
                    'div',
                    { className: 'author' },
                    _lodash2.default.get(a, ['writers', 'length'], 0) > 0 ? writers + ' ' : null
                  ),
                  _react2.default.createElement('div', { className: 'separator' }),
                  _react2.default.createElement(
                    'div',
                    { className: 'date' },
                    (0, _dateformat2.default)(a.publishedDate, 'yyyy.mm.dd')
                  )
                )
              )
            );
          })
        )
      ) : null;
    }
  }]);

  return Choices;
}(_react.Component);

exports.default = Choices;
exports.Choices = Choices;