'use strict';

var asyncToGenerator = require('./asyncToGenerator-65df6bf8.js');
var _extends = require('./extends-be0d85bc.js');
var slicedToArray = require('./slicedToArray-067dd3bc.js');
var React = require('react');
var Link = require('next/link');
var MasterDeck = require('./MasterDeck.js');
var MediaSeriesCard = require('./MediaSeriesCard-ca024762.js');
var BlockContent = require('./BlockContent-96b44364.js');
var getSerializers = require('./index-7b7f4959.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('./toConsumableArray-d7797c2b.js');
require('./inherits-e47b5342.js');
require('./_object-sap-d1978572.js');
require('./define-property-e8404b64.js');
require('./typeof-220bbf73.js');
require('./_is-array-54228b29.js');
require('./index-d2a6ef75.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-1ab8dda7.js');
require('./visibility-sensor-56020df6.js');
require('react-dom');
require('./index-d48b231c.js');
require('./ADlgInfeed-a536168e.js');
require('./AdSlot.js');
require('./Beam.js');
require('./stringify-f7a37a8d.js');
require('./Segment.js');
require('./main-ae8472f3.js');
require('./ADInfeed-89f63b7d.js');
require('./index-65621b6c.js');
require('./urlFor.js');
require('./style-inject.es-dcee06b6.js');
require('react-bootstrap');
require('./index-187c967e.js');
require('./util-93a37fd0.js');
require('./urlForFile-5084fc48.js');
require('./brightcove-react-player-loader.es-4c7825a1.js');
require('./_object-to-array-9337ba83.js');
require('./keys-e982b3fa.js');
require('react-bootstrap/Modal');
require('react-bootstrap/Button');
require('react-bootstrap/Form');
require('react-ga');
require('./GroupDeck.js');
require('./smoothscroll-99df7f93.js');
require('./SocialShare.js');
require('next/head');
require('react-share');
require('react-bootstrap/Pagination');
require('./entities-df1aed86.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');

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

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

var _this = undefined;

var PodcastsLanding = function PodcastsLanding(props) {
  var client = props.client,
      pageview = props.pageview,
      currentPage = props.currentPage;
      props.taxonomyId;
      props.parentUrl;
      var nextPagePostsCount = props.nextPagePostsCount;

  var _useState = React.useState(props.seriesData),
      _useState2 = slicedToArray._slicedToArray(_useState, 2),
      seriesData = _useState2[0];
      _useState2[1];

  var _useState3 = React.useState(props.params),
      _useState4 = slicedToArray._slicedToArray(_useState3, 2),
      params = _useState4[0];
      _useState4[1];

  var _useState5 = React.useState(props.query),
      _useState6 = slicedToArray._slicedToArray(_useState5, 2),
      query = _useState6[0];
      _useState6[1];

  if (props.statusCode === 404) {
    return React__default["default"].createElement(
      'div',
      { className: 'd-flex flex-column align-items-center mt-3' },
      React__default["default"].createElement(
        'h3',
        null,
        '404: Resource not found'
      ),
      React__default["default"].createElement(
        'p',
        null,
        'We seem to have misplaced the page you\'re looking for. Sorry! Please try checking the URL for errors.'
      ),
      React__default["default"].createElement(
        'p',
        null,
        'Click here to',
        React__default["default"].createElement(
          Link__default["default"],
          { href: '/' },
          React__default["default"].createElement(
            'a',
            { style: { color: 'blue', textDecoration: 'underline' } },
            ' return to homepage.'
          )
        )
      )
    );
  }

  return React__default["default"].createElement(
    'div',
    { className: 'mb-5' },
    props.taxonomy && props.taxonomy.summary && React__default["default"].createElement(BlockContent.BlockContent, { style: { marginTop: '1rem !important' }, blocks: props.taxonomy.summary, serializers: getSerializers.getSerializers(client) }),
    React__default["default"].createElement('hr', { style: { marginTop: '2rem', marginBottom: '2rem' } }),
    seriesData && React__default["default"].createElement(MasterDeck, {
      deck: MediaSeriesCard.MediaSeriesCard,
      variant: 'left',
      defaultImage: '/placeholder.jpg',
      client: client,
      dataRecord: seriesData,
      query: query && query,
      params: params && params,
      currentPage: currentPage,
      pageview: pageview,
      autoScroll: true,
      seoPaginate: true,
      nextPagePostsCount: nextPagePostsCount
    })
  );
};

PodcastsLanding.getInitialData = function () {
  var _ref = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee(ctx, client, cache) {
    var urlQuery, pathname, identifier, itemsPerPage, currentPage, start, statusCode, params, taxonomyId, seriesQuery, seriesData, nextPagePostsCount;
    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            urlQuery = ctx.query, pathname = ctx.pathname;
            identifier = pathname.replace('/', '');
            itemsPerPage = 10;
            currentPage = urlQuery.page ? parseInt(urlQuery.page) : 1;
            start = (currentPage - 1) * itemsPerPage;
            statusCode = 200;
            params = {
              count: itemsPerPage,
              from: start,
              to: start + itemsPerPage,
              url: pathname,
              itemsPerPage: itemsPerPage
            };
            _context.next = 9;
            return client.fetch('*[_type == \'taxonomy\' && identifier == \'' + identifier + '\'][0]{\n    _id, \n    name,\n    summary,\n    pixelTrackingCode->\n  }');

          case 9:
            taxonomyId = _context.sent;

            //  const taxonomyId = await client.fetch(`*[_type == 'taxonomy' && _id == '6d748e0d-7946-4a14-968e-ca8182f06c1e']`)
            //   console.log('taxonomyId', taxonomyId)
            seriesQuery = '*[_type == \'taxonomy\' && isMainTopic == true && references(\'' + taxonomyId._id + '\')] | order(published desc) [$from...$to] {\n        _id,\n        name,\n        identifier,\n        thumbnail{ ..., asset-> },\n        summary,\n        \'pageNumber\': ' + currentPage + ',\n        \'parentUrl\': \'' + identifier + '\'\n    }';
            _context.next = 13;
            return client.fetch(seriesQuery, params);

          case 13:
            seriesData = _context.sent;

            seriesData.forEach(function (seriesObj) {
              seriesObj.description = seriesObj.summary;
              seriesObj.identifier = { _type: 'slug', current: seriesObj.identifier };
            });
            _context.next = 17;
            return client.fetch('count(' + seriesQuery + ')', _extends._extends({}, params, { from: params.to, to: params.to + itemsPerPage }));

          case 17:
            nextPagePostsCount = _context.sent;


            if (currentPage > 1 && seriesData.length === 0) {
              statusCode = 404;
            }

            return _context.abrupt('return', {
              seriesData: seriesData,
              query: seriesQuery,
              params: params,
              currentPage: currentPage,
              identifier: identifier,
              taxonomyId: taxonomyId,
              parentUrl: identifier,
              statusCode: statusCode,
              taxonomy: taxonomyId,
              nextPagePostsCount: nextPagePostsCount
            });

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

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

module.exports = PodcastsLanding;
