'use strict';

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

var _classnames2 = _interopRequireDefault(_classnames);

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

var _reactGa2 = _interopRequireDefault(_reactGa);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDfp = require('react-dfp');

var _humps = require('humps');

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

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

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

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

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

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

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

      var _props = this.props;
      var entities = _props.entities;
      var sectionList = _props.sectionList;
      var sections = _props.sections;

      var sortedList = _lodash2.default.sortBy(sectionList.sections, function (o) {
        return o.sortOrder;
      });

      var styles = ['ui', 'centered', { 'three column': false }, 'grid'];

      return _react2.default.createElement(
        'div',
        { className: 'container' },
        _react2.default.createElement(
          'div',
          { className: (0, _classnames2.default)(styles) },
          _lodash2.default.map(_lodash2.default.take(sortedList, 2), function (s) {
            var featuredList = _lodash2.default.get(sections, ['items', (0, _humps.camelize)(s.name)], []);
            var sectionTop = _lodash2.default.get(entities.articles, _lodash2.default.first(featuredList), {}); //fetch first one
            var topicList = _lodash2.default.slice(featuredList, 1, 3); //fetch rest

            // let articles = _.filter(entities.articles, function (a) { return _.indexOf(_.get(sections, [ 'items', camelize(s.name) ], []), a.id) > -1 })
            // sectionTop = _.take(articles, 1) //fetch first one
            // topicList = articles.splice(1, 2) //fetch rest

            var image = (0, _index2.imageComposer)(sectionTop).mobileImage;
            var linkStyle = _lodash2.default.get(sectionTop, 'style', '') == 'projects' ? '/projects/' : '/story/';
            return _react2.default.createElement(
              'div',
              { className: 'ui column', key: 'section-' + s.id },
              _react2.default.createElement(
                'a',
                { href: linkStyle + _lodash2.default.get(sectionTop, 'slug', '') + '/', onClick: _this2._handleClick },
                _react2.default.createElement(
                  'div',
                  { className: 'sectionBlock' },
                  _react2.default.createElement(
                    'div',
                    { className: 'section-cat' },
                    _lodash2.default.get(entities.categories, [_lodash2.default.first(_lodash2.default.get(sectionTop, 'categories', [])), 'title'], '　　')
                  ),
                  _react2.default.createElement(
                    'div',
                    { className: 'gradient labelBlock' },
                    s.title
                  ),
                  _react2.default.createElement('div', { className: 'sectionImg', style: { background: 'url(' + image + ') no-repeat center center', backgroundSize: 'cover', width: '300px', height: '250px' } }),
                  _react2.default.createElement(
                    'div',
                    { className: 'sectionTopic' },
                    _lodash2.default.get(sectionTop, 'title', '　')
                  )
                )
              ),
              _react2.default.createElement(
                'ul',
                { className: 'sectionList' },
                _lodash2.default.map(topicList, function (t, idx) {
                  var a = _lodash2.default.get(entities.articles, t, {});
                  var linkStyle = _lodash2.default.get(a, 'style', '') == 'projects' ? '/projects/' : '/story/';
                  return _react2.default.createElement(
                    'li',
                    { key: a.id || idx },
                    _react2.default.createElement(
                      'a',
                      { href: linkStyle + a.slug + '/', onClick: _this2._handleClick },
                      a.title
                    )
                  );
                })
              )
            );
          }),
          _react2.default.createElement(
            'div',
            { className: 'ui column', style: { backgroundColor: 'rgba(0, 77, 162, 0.1)', marginTop: '-10px', marginBottom: '20px', height: '270px' } },
            _react2.default.createElement(
              'a',
              { href: 'https://goo.gl/yqJaVY', target: '_blank' },
              _react2.default.createElement(
                'div',
                { className: 'sectionBlock', style: { marginTop: '10px', marginBottom: '10px' } },
                _react2.default.createElement(_reactDfp.AdSlot, { sizes: [[300, 250]],
                  dfpNetworkId: _index.DFPID,
                  slotId: 'mm_pc_hp_300x250_1st',
                  adUnit: 'mm_pc_hp_300x250_1st',
                  sizeMapping: [{ viewport: [0, 0], sizes: [] }, { viewport: [970, 200], sizes: [[970, 90], [970, 250], [300, 250]] }]
                }),
                _react2.default.createElement(_reactDfp.AdSlot, { sizes: [[300, 250]],
                  dfpNetworkId: _index.DFPID,
                  slotId: 'mm_mobile_hp_300x250_1st',
                  adUnit: 'mm_mobile_hp_300x250_1st',
                  sizeMapping: [{ viewport: [1, 1], sizes: [[300, 250]] }, { viewport: [970, 200], sizes: [] }]
                })
              )
            )
          ),
          _lodash2.default.map(_lodash2.default.slice(sortedList, 2), function (s) {
            var featuredList = _lodash2.default.get(sections, ['items', (0, _humps.camelize)(s.name)], []);
            var sectionTop = _lodash2.default.get(entities.articles, _lodash2.default.first(featuredList), {}); //fetch first one
            var topicList = _lodash2.default.slice(featuredList, 1, 3); //fetch rest

            // let articles = _.filter(entities.articles, function (a) { return _.indexOf(_.get(sections, [ 'items', camelize(s.name) ], []), a.id) > -1 })
            // sectionTop = _.take(articles, 1) //fetch first one
            // topicList = articles.splice(1, 2) //fetch rest

            var image = (0, _index2.imageComposer)(sectionTop).mobileImage;
            var linkStyle = _lodash2.default.get(sectionTop, 'style', '') == 'projects' ? '/projects/' : '/story/';
            return _react2.default.createElement(
              'div',
              { className: 'ui column', key: 'section-' + s.id },
              _react2.default.createElement(
                'a',
                { href: linkStyle + _lodash2.default.get(sectionTop, 'slug', '') + '/', onClick: _this2._handleClick },
                _react2.default.createElement(
                  'div',
                  { className: 'sectionBlock' },
                  _react2.default.createElement(
                    'div',
                    { className: 'section-cat' },
                    _lodash2.default.get(entities.categories, [_lodash2.default.first(_lodash2.default.get(sectionTop, 'categories', [])), 'title'], '　　')
                  ),
                  _react2.default.createElement(
                    'div',
                    { className: 'gradient labelBlock' },
                    s.title
                  ),
                  _react2.default.createElement('div', { className: 'sectionImg', style: { background: 'url(' + image + ') no-repeat center center', backgroundSize: 'cover', width: '300px', height: '250px' } }),
                  _react2.default.createElement(
                    'div',
                    { className: 'sectionTopic' },
                    _lodash2.default.get(sectionTop, 'title', '　')
                  )
                )
              ),
              _react2.default.createElement(
                'ul',
                { className: 'sectionList' },
                _lodash2.default.map(topicList, function (t, idx) {
                  var a = _lodash2.default.get(entities.articles, t, {});
                  var linkStyle = _lodash2.default.get(a, 'style', '') == 'projects' ? '/projects/' : '/story/';
                  return _react2.default.createElement(
                    'li',
                    { key: a.id || idx },
                    _react2.default.createElement(
                      'a',
                      { href: linkStyle + a.slug + '/', onClick: _this2._handleClick },
                      a.title
                    )
                  );
                })
              )
            );
          })
        )
      );
    }
  }]);

  return LatestSections;
}(_react.Component);

exports.default = LatestSections;
exports.LatestSections = LatestSections;