'use strict';

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

var _dateTransformer = require('../lib/date-transformer');

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

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _Category = require('./Category');

var _Category2 = _interopRequireDefault(_Category);

var _reactFlexCarousel = require('react-flex-carousel');

var _reactFlexCarousel2 = _interopRequireDefault(_reactFlexCarousel);

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

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

    return _possibleConstructorReturn(this, (TopNews.__proto__ || Object.getPrototypeOf(TopNews)).call(this, props, context));
  }

  _createClass(TopNews, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      // this.handleResize()
      // window.addEventListener('resize', this.handleResize);
    }
  }, {
    key: 'render',
    value: function render() {
      var topnews = this.props.topnews;
      var device = this.context.device;

      return Array.isArray(topnews) && topnews.length > 0 ? _react2.default.createElement(
        _reactFlexCarousel2.default,
        { className: 'topnews', autoplayInteval: 4500, indicator: true, switcher: true },
        topnews.map(function (a) {
          var pubDate = (0, _dateTransformer.date2yyyymmdd)(a.publishedDate * 1000, '.');
          var cats = _lodash2.default.get(a, 'categories', []);
          var catDisplay = _lodash2.default.get(cats, [0, 'name'], '專題');
          var imageSet = (0, _index.imageComposer)(a);
          var image = device === 'desktop' ? imageSet.desktopImage : imageSet.mobileImage;
          return _react2.default.createElement(
            _reactRouter.Link,
            { key: a.id, to: '/a/' + a.slug },
            _react2.default.createElement('img', { src: image, alt: a.slug }),
            _react2.default.createElement(
              'div',
              { className: 'topnews_categorycontainer' },
              _react2.default.createElement(
                _Category2.default,
                null,
                catDisplay
              )
            ),
            _react2.default.createElement(
              'div',
              { className: 'carousel-item' },
              _react2.default.createElement(
                'div',
                { className: 'carousel-itemsubtitle' },
                a.subtitle
              ),
              _react2.default.createElement(
                'div',
                { className: 'carousel-itemtitle' },
                a.title
              ),
              _react2.default.createElement(
                'div',
                { className: 'carousel-excerpt' },
                a.excerpt
              ),
              _react2.default.createElement(
                'time',
                { className: 'carousel-published' },
                pubDate
              )
            )
          );
        })
      ) : null;
    }
  }]);

  return TopNews;
}(_react.Component);

exports.default = TopNews;


TopNews.contextTypes = {
  //  store: React.PropTypes.object,
  device: _react2.default.PropTypes.string
};
exports.TopNews = TopNews;