'use strict';

var asyncToGenerator = require('./asyncToGenerator-65df6bf8.js');
var slicedToArray = require('./slicedToArray-067dd3bc.js');
var _extends = require('./extends-be0d85bc.js');
var React = require('react');
var BlockContent = require('./BlockContent-9ebd251f.js');
var getSerializers = require('./index-ee8ef012.js');
var ArticleSeriesListing = require('./ArticleSeriesListing.js');
require('./_commonjsHelpers-06173234.js');
require('./core.get-iterator-method-5643aa10.js');
require('./web.dom.iterable-d98303e0.js');
require('./_library-dd23b178.js');
require('./_set-species-0c7864a2.js');
require('./_iter-detect-b1df62f0.js');
require('prop-types');
require('./index-d48b231c.js');
require('react-bootstrap');
require('./urlFor.js');
require('./index-fc2f1ca2.js');
require('./events-1b438fe7.js');
require('next/router');
require('./index-187c967e.js');
require('./Beam.js');
require('./stringify-f7a37a8d.js');
require('./Segment.js');
require('./main-1ec1de84.js');
require('./urlForFile-5084fc48.js');
require('./index-eadd5035.js');
require('react-dom');
require('./typeof-220bbf73.js');
require('./_is-array-54228b29.js');
require('./brightcove-react-player-loader.es-a195642e.js');
require('./_object-to-array-9337ba83.js');
require('./keys-e982b3fa.js');
require('./_object-sap-d1978572.js');
require('react-bootstrap/Modal');
require('react-bootstrap/Button');
require('react-bootstrap/Form');
require('react-ga');
require('./GroupDeck.js');
require('./smoothscroll-99df7f93.js');
require('./moment-aca40de1.js');
require('./SocialShare.js');
require('next/head');
require('react-share');
require('./toConsumableArray-d7797c2b.js');
require('react-bootstrap/Pagination');
require('./entities-df1aed86.js');
require('./AdSlot.js');
require('./debounce-d4a2369b.js');
require('./isSymbol-03abbdf0.js');
require('./toNumber-5e1fac58.js');
require('react-bootstrap/Row');
require('react-bootstrap/Col');
require('./inherits-e47b5342.js');
require('./define-property-e8404b64.js');
require('./index.esm-d81454e7.js');
require('./iconBase-be4097c0.js');
require('react-bootstrap/Figure');
require('react-bootstrap/OverlayTrigger');
require('react-bootstrap/Tooltip');
require('react-bootstrap/Popover');
require('./checkSisterSite-c3a4ac98.js');
require('react-bootstrap/Container');
require('./visibility-sensor-eb2c5b69.js');
require('./YoutubeGroup.js');
require('./index.es-f3e47207.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 ArticleProgramLandingPage = function ArticleProgramLandingPage(props) {
  var client = props.client,
      posts = props.posts,
      mapping = props.mapping,
      query = props.query,
      params = props.params,
      currentPage = props.currentPage,
      pageview = props.pageview,
      _props$columns = props.columns,
      columns = _props$columns === undefined ? 'rotate' : _props$columns,
      _props$variant = props.variant,
      variant = _props$variant === undefined ? 'top' : _props$variant,
      _props$page = props.page,
      page = _props$page === undefined ? '' : _props$page,
      _props$defaultImage = props.defaultImage,
      defaultImage = _props$defaultImage === undefined ? '/placeholder.jpg' : _props$defaultImage,
      virtualBlock = props.virtualBlock,
      pageDescription = props.pageDescription,
      _props$autoScroll = props.autoScroll,
      autoScroll = _props$autoScroll === undefined ? true : _props$autoScroll;

  return React__default['default'].createElement(
    'div',
    null,
    virtualBlock && React__default['default'].createElement(BlockContent.BlockContent, _extends._extends({ serializers: getSerializers.getSerializers(client), blocks: virtualBlock.body, imageOptions: { w: 320, h: 240, fit: 'max' } }, client.config())),
    pageDescription && React__default['default'].createElement(
      'div',
      { className: 'mb-3' },
      pageDescription.image && React__default['default'].createElement('img', { className: 'mb-3 w-100', src: pageDescription.image, alt: pageDescription.imageAlt || '' }),
      pageDescription.description && React__default['default'].createElement(
        'p',
        null,
        pageDescription.description
      )
    ),
    posts && React__default['default'].createElement(ArticleSeriesListing, {
      client: client,
      mapping: mapping,
      dataRecord: posts,
      query: query,
      params: params,
      currentPage: currentPage,
      pageview: pageview,
      columns: columns,
      variant: variant,
      page: page,
      autoScroll: autoScroll,
      defaultImage: defaultImage
    })
  );
};

ArticleProgramLandingPage.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 pathname, urlQuery, url, page, currentPage, start, params, query, postsPromise, virtualBlockPromise, posts, virtualBlock, _ref2, _ref3;

    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            pathname = context.pathname, urlQuery = context.query;
            url = pathname ? pathname : '';
            page = urlQuery.page ? parseInt(urlQuery.page) : 1;
            currentPage = page ? parseInt(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    && references(\'' + taxonomyId + '\')\n    && is_visible == true\n    && passwordLock != true \n    && defined(published)\n    && dateTime(published) <= dateTime(now())] | order(published desc)[$from...$to]{\n      title,\n      published,\n      thumbnail,\n      url,\n      taxonomy,\n      body[_type == "articleSeries"]{\n        ...\n      },\n      \'pageNumber\': ' + currentPage + '\n    }';
            postsPromise = client.fetch(query, params);
            virtualBlockPromise = client.fetch('*[_type == \'virtualBlock\' && identifier == \'' + pathname + '\'][0]');
            posts = void 0, virtualBlock = void 0;
            _context.prev = 10;
_context.next = 14;
            return asyncToGenerator._Promise.all([postsPromise, virtualBlockPromise]);

          case 14:
            _ref2 = _context.sent;
            _ref3 = slicedToArray._slicedToArray(_ref2, 2);
            posts = _ref3[0];
            virtualBlock = _ref3[1];
            _context.next = 23;
            break;

          case 20:
            _context.prev = 20;
            _context.t0 = _context['catch'](10);

            console.error(_context.t0);

          case 23:
            return _context.abrupt('return', { posts: posts, virtualBlock: virtualBlock, url: url, query: query });

          case 24:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, _this, [[10, 20]]);
  }));

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

module.exports = ArticleProgramLandingPage;
