'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

require('./_commonjsHelpers-06173234.js');
require('./_to-object-329a650b.js');
require('./es6.string.iterator-cc0bbaa4.js');
require('./_library-dd23b178.js');
require('./_iter-detect-60b2f026.js');
require('./toConsumableArray-9a3476fe.js');
require('./core.get-iterator-method-f62321d4.js');
require('./_object-pie-68c5bcbc.js');
var _extends$1 = require('./extends-60837c34.js');
require('./web.dom.iterable-a0e279c1.js');
var React = require('react');
var React__default = _interopDefault(React);
require('prop-types');
require('./index.es-9110add6.js');
require('react-dom');
require('./index-be0c82be.js');
require('./index-da796b8c.js');
require('./main-02daeefe.js');
require('./slicedToArray-8f385299.js');
var asyncToGenerator = require('./asyncToGenerator-533d476a.js');
require('./_set-species-f92c67c5.js');
require('./index-187c967e.js');
require('./smoothscroll-5054361f.js');
var Dropdown = _interopDefault(require('react-bootstrap/Dropdown'));
require('./SeriesSlider-0a4b30a7.js');
var SeriesListingDeck = require('./SeriesListingDeck.js');

var _this = undefined;

var Filter = function Filter(_ref) {
  var filterData = _ref.filterData;

  if (!filterData.series || filterData.series.length == 0) return null;
  return React__default.createElement(
    Dropdown,
    { className: 'mb-3' },
    React__default.createElement(
      Dropdown.Toggle,
      { variant: 'secondary', id: 'dropdown-series' },
      'All Series'
    ),
    React__default.createElement(
      Dropdown.Menu,
      null,
      filterData.series && filterData.series.map(function (docGroup) {
        return React__default.createElement(
          Dropdown.Item,
          { href: '/' + filterData.identifier.current + '/' + docGroup.identifier.current },
          docGroup.name
        );
      })
    )
  );
};

var MediaSeriesLanding = function MediaSeriesLanding(props) {
  if (!props.seriesData || props.seriesData.length == 0) {
    return null;
  }
  return React__default.createElement(
    'div',
    null,
    props.filterDropDownData && props.filterDropDownData.series && React__default.createElement(Filter, { filterData: props.filterDropDownData }),
    React__default.createElement(SeriesListingDeck, _extends$1._extends({}, props, { data: props.seriesData, client: props.client, displayEpisodeNumber: props.displayEpisodeNumber }))
  );
};

MediaSeriesLanding.getInitialData = function () {
  var _ref2 = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee(ctx, client, cache) {
    var urlQuery, pathname, req, identifier, itemsPerPage, currentPage, start, params, seriesQuery, seriesData, filterDropDownData, allSeries;
    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            urlQuery = ctx.query, pathname = ctx.pathname, req = ctx.req;
            identifier = pathname.replace('/', '');
            itemsPerPage = 6;
            currentPage = urlQuery.page ? parseInt(urlQuery.page) : 1;
            start = (currentPage - 1) * itemsPerPage;
            params = {
              count: itemsPerPage,
              from: start,
              to: start + itemsPerPage,
              url: pathname
            };
            seriesQuery = '*[_type == \'documentGroup\' && identifier.current == \'' + identifier + '\'][0]{\n    _id,\n    name,\n    identifier,\n      \'series\': *[_type == \'documentGroup\' && references(^._id) && isSeries != false][$from...$to]{\n        _id,\n        name,\n        identifier,\n        \'articles\': \n            *[!(_id in path("drafts.**")) \n            && _type == "article"\n            && defined(title)\n            && is_visible\n            && defined(url)\n            && references(^._id)\n            && passwordLock != true\n            && defined(published)\n            && dateTime(published) <= dateTime(now())\n          ] | order(published desc)  {\n            ...,\n              title,\n              summary,\n              thumbnail,\n              url,\n              published,\n              authorMapping[0]->,\n              taxonomyMapping[]->,\n              contentCategory->,\n              \'pageNumber\': ' + currentPage + '\n            }\n\n    }\n}';
            _context.next = 9;
            return client.fetch(seriesQuery, params);

          case 9:
            seriesData = _context.sent;
            filterDropDownData = [];
            _context.next = 13;
            return client.fetch('*[_type == \'documentGroup\' && identifier.current == \'' + identifier + '\'][0]{\n      _id,\n      name,\n      identifier,\n        \'series\': *[_type == \'documentGroup\' && references(^._id) && isSeries != false]{\n          _id,\n          name,\n          identifier\n        \n      }\n  }');

          case 13:
            allSeries = _context.sent;


            filterDropDownData = allSeries;

            return _context.abrupt('return', { seriesData: seriesData, query: seriesQuery, params: params, page: currentPage, filterDropDownData: filterDropDownData, identifier: identifier });

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

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

module.exports = MediaSeriesLanding;
