'use strict';

var asyncToGenerator = require('./asyncToGenerator-77821af0.js');
var React = require('react');
var MasterDeck = require('./MasterDeck.js');
require('./_commonjsHelpers-06173234.js');
require('./core.get-iterator-method-5643aa10.js');
require('./web.dom.iterable-d98303e0.js');
require('./_library-dd23b178.js');
require('./_iter-detect-b1df62f0.js');
require('./toConsumableArray-d7797c2b.js');
require('./extends-7c86182f.js');
require('./_object-pie-083f2dd6.js');
require('./inherits-de66ea16.js');
require('./_object-sap-d1978572.js');
require('./define-property-e8404b64.js');
require('./typeof-567e31ff.js');
require('./_is-array-54228b29.js');
require('./index-fc2f1ca2.js');
require('./events-1b438fe7.js');
require('prop-types');
require('react-bootstrap/Container');
require('react-bootstrap/Row');
require('react-bootstrap/Col');
require('react-bootstrap/Card');
require('./moment-aca40de1.js');
require('next/router');
require('./lodash-f0783643.js');
require('./debounce-d4a2369b.js');
require('./isSymbol-03abbdf0.js');
require('./toNumber-5e1fac58.js');
require('./index.es-f3e47207.js');
require('./visibility-sensor-eb2c5b69.js');
require('react-dom');
require('./slicedToArray-067dd3bc.js');
require('./index-d48b231c.js');
require('./ADlgInfeed-950b414b.js');
require('./AdSlot.js');
require('./Beam.js');
require('./stringify-f7a37a8d.js');
require('./Segment.js');
require('./main-f1c77adb.js');
require('./ADInfeed-bce0ba07.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var _this = undefined;

var ArticleSeriesLandingPage = function ArticleSeriesLandingPage(props) {
  var client = props.client,
      posts = props.posts,
      summary = props.summary,
      seriesTitle = props.seriesTitle,
      seriesName = props.seriesName,
      query = props.query,
      params = props.params,
      _props$currentPage = props.currentPage,
      currentPage = _props$currentPage === undefined ? 1 : _props$currentPage,
      pageview = props.pageview,
      _props$autoScroll = props.autoScroll,
      autoScroll = _props$autoScroll === undefined ? true : _props$autoScroll,
      mapping = props.mapping,
      deck = props.deck,
      _props$variant = props.variant,
      variant = _props$variant === undefined ? 'left' : _props$variant,
      _props$defaultImage = props.defaultImage,
      defaultImage = _props$defaultImage === undefined ? '/placeholder.jpg' : _props$defaultImage;


  return React__default['default'].createElement(
    'div',
    null,
    summary && React__default['default'].createElement(
      'div',
      { style: { fontSize: '1rem' }, className: 'article-series-summary px-1 mt-2 mb-4' },
      summary
    ),
    seriesTitle && React__default['default'].createElement(
      'h1',
      { className: 'article-series-title' },
      React__default['default'].createElement(
        'a',
        seriesName && { href: '/view/' + seriesName },
        seriesTitle
      )
    ),
    posts && deck && React__default['default'].createElement(MasterDeck, {
      deck: deck,
      variant: variant,
      defaultImage: defaultImage,
      client: client,
      mapping: mapping,
      dataRecord: posts,
      query: query,
      params: params,
      currentPage: currentPage,
      pageview: pageview,
      autoScroll: autoScroll
    })
  );
};

ArticleSeriesLandingPage.getInitialData = function () {
  var _ref = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee(context, client) {
    var itemsPerPage = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10;
    var taxonomyId = arguments[3];
    var urlQuery, seriesName, page, currentPage, start, params, query, posts, seriesTitle, summary, url;
    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            urlQuery = context.query;
            seriesName = urlQuery.series;
            page = urlQuery.page ? parseInt(urlQuery.page) : 1;
            currentPage = page ? parseInt(page) : 1;
            start = (currentPage - 1) * itemsPerPage;
            params = {
              from: start,
              to: start + itemsPerPage
            };
            query = '*[\n    _type == \'article\' \n    && !(_id in path("drafts.**")) \n    && defined(title) \n    && defined(url) \n    && references(\'' + taxonomyId + '\')\n    && url.current == \'' + seriesName + '\'\n    && is_visible == true\n    && passwordLock != true \n    && defined(published)\n    && dateTime(published) <= dateTime(now())] | order(published desc)[$from...$to]{\n      title,\n      summary,\n      published,\n      thumbnail,\n      url,\n      taxonomy,\n      body[_type == "articleSeries"]{\n        ...\n      },\n    }';
            _context.next = 9;
            return client.fetch(query, params);

          case 9:
            posts = _context.sent;
            seriesTitle = '';
            summary = false;


            if (posts.length > 0 && posts[0].body[0].articles) {
              url = posts[0].url.current + '?seriesArticle=';

              seriesTitle = posts[0].title;
              summary = posts[0].summary;
              posts = posts[0].body[0].articles;
              //start youtube thumbnail fix
              posts.map(function (post, index) {
                return post['url'] = {
                  current: url + post.param.current
                };
              });
              //end
            }
            return _context.abrupt('return', { posts: posts, query: query, params: params, seriesTitle: seriesTitle, seriesName: seriesName, summary: summary });

          case 14:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, _this);
  }));

  return function (_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

module.exports = ArticleSeriesLandingPage;
