'use strict';

var asyncToGenerator = require('./asyncToGenerator-58c289d9.js');
var _typeof = require('./typeof-88a668c5.js');
var React = require('react');
var moment = require('./moment-aca40de1.js');
var index = require('./index-187c967e.js');
var Breadcrumbs = require('./Breadcrumbs.js');
var MasterDeck = require('./MasterDeck.js');
var VideoSeriesCard = require('./VideoSeriesCard.js');
require('./_commonjsHelpers-06173234.js');
require('./es6.string.iterator-c2573ffd.js');
require('./_to-object-6de10e57.js');
require('./web.dom.iterable-46657b5c.js');
require('./_library-dd23b178.js');
require('./core.get-iterator-method-f474eacc.js');
require('./_iter-detect-55a5657a.js');
require('./_is-array-6128bd56.js');
require('./_object-pie-083f2dd6.js');
require('./slicedToArray-5d1bc3b3.js');
require('react-bootstrap/Breadcrumb');
require('./toConsumableArray-ddb70795.js');
require('./extends-29bdbc6d.js');
require('./inherits-c2065b95.js');
require('./_object-sap-a175b4a9.js');
require('./define-property-c88be878.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('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('./index-efca863c.js');
require('./ADlgInfeed-aedd5e18.js');
require('./AdSlot.js');
require('./Beam.js');
require('./stringify-c9ac1214.js');
require('./Segment.js');
require('./main-025fdc78.js');
require('./ADInfeed-8b336c67.js');
require('./index-eadd5035.js');
require('./urlFor.js');
require('./style-inject.es-dcee06b6.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 VideoSeriesLandingPage = function VideoSeriesLandingPage(props) {
  var summary = props.summary,
      client = props.client,
      posts = props.posts,
      _props$variant = props.variant,
      variant = _props$variant === undefined ? 'left' : _props$variant,
      _props$defaultImage = props.defaultImage,
      defaultImage = _props$defaultImage === undefined ? '/placeholder.jpg' : _props$defaultImage,
      mapping = props.mapping,
      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,
      rightItems = props.rightItems,
      brandInsightAd = props.brandInsightAd,
      _props$showBI = props.showBI,
      showBI = _props$showBI === undefined ? false : _props$showBI,
      _props$lgInfeedAd = props.lgInfeedAd,
      lgInfeedAd = _props$lgInfeedAd === undefined ? false : _props$lgInfeedAd;

  return React__default['default'].createElement(
    'div',
    null,
    summary && React__default['default'].createElement(
      'div',
      { style: { fontSize: '1rem' }, className: 'series-summary px-1 my-4' },
      summary
    ),
    React__default['default'].createElement(Breadcrumbs, null),
    posts && React__default['default'].createElement(MasterDeck, {
      deck: VideoSeriesCard,
      variant: variant,
      client: client,
      defaultImage: defaultImage,
      mapping: mapping,
      dataRecord: posts,
      query: query,
      params: params,
      currentPage: currentPage,
      pageview: pageview,
      autoScroll: autoScroll,
      rightItems: rightItems,
      brandInsightAd: brandInsightAd,
      showBI: showBI,
      lgInfeedAd: lgInfeedAd
    })
  );
};

VideoSeriesLandingPage.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 today, urlQuery, seriesName, currentPage, start, params, query, posts, seriesTitle, summary, url;
    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            today = moment.moment().utc().format();
            urlQuery = context.query;
            seriesName = urlQuery.series;
            currentPage = urlQuery.page ? parseInt(urlQuery.page) : 1;
            start = (currentPage - 1) * itemsPerPage;
            params = {
              from: start,
              to: start + itemsPerPage,
              itemsPerPage: itemsPerPage
            };
            query = '*[\n    _type == \'article\' \n    && !(_id in path("drafts.**")) \n    && defined(title) \n    && defined(url) \n    && dateTime(published) <= dateTime(now()) \n    && references(\'' + taxonomyId + '\') \n    && is_visible == true \n    && url.current == \'' + seriesName + '\'\n  ][$from...$to] | order(published desc) {\n\t\ttitle,\n\t\tsummary,\n\t\tpublished,\n\t\tthumbnail,\n\t\turl,\n\t\ttaxonomy,\n\t\tbody[_type == "youtubeGallery"]\n\t}';
            _context.next = 9;
            return client.fetch(query, params);

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


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

              seriesTitle = posts[0].title;
              summary = posts[0].summary;
              posts = posts[0].body[0].videos.filter(function (video) {
                if ((video.is_visible === true || !video.hasOwnProperty('is_visible')) && (today >= video.published || !video.hasOwnProperty('published'))) {
                  return video;
                }
              });
              //start youtube thumbnail fix
              posts.map(function (post, index$1) {
                if (post.thumbnail && _typeof._typeof(post.thumbnail) !== 'object' || !post.thumbnail) {
                  var id = index.getYoutubeId(post.url);
                  var imageURL = 'https://img.youtube.com/vi/' + id + '/hqdefault.jpg';
                  return post['thumbnail'] = imageURL, post['url'] = {
                    current: url + (index$1 + 1)
                  };
                }
                return post['url'] = {
                  current: url + (index$1 + 1)
                };
              });
              //end
            }
            return _context.abrupt('return', { posts: posts, query: query, params: params, seriesTitle: seriesTitle, summary: summary, seriesName: seriesName });

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

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

module.exports = VideoSeriesLandingPage;
