'use strict';

var asyncToGenerator = require('./asyncToGenerator-58c289d9.js');
var _extends = require('./extends-29bdbc6d.js');
var React = require('react');
var BlockContent = require('./BlockContent-36e54d77.js');
var Col = require('react-bootstrap/Col');
var Form = require('react-bootstrap/Form');
var Button = require('react-bootstrap/Button');
var getSerializers = require('./index-3cfbe927.js');
var VideoSeriesListing = require('./VideoSeriesListing.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('./_object-pie-083f2dd6.js');
require('prop-types');
require('./index-efca863c.js');
require('./slicedToArray-5d1bc3b3.js');
require('react-bootstrap');
require('./urlFor.js');
require('./index-fc2f1ca2.js');
require('./events-1b438fe7.js');
require('next/router');
require('./index-187c967e.js');
require('./util-6f784d85.js');
require('./Beam.js');
require('./stringify-c9ac1214.js');
require('./Segment.js');
require('./main-025fdc78.js');
require('./index-eadd5035.js');
require('react-dom');
require('./typeof-88a668c5.js');
require('./_is-array-6128bd56.js');
require('./brightcove-react-player-loader.es-a195642e.js');
require('./GroupDeck.js');
require('./smoothscroll-99df7f93.js');
require('./moment-aca40de1.js');
require('./SocialShare.js');
require('next/head');
require('react-share');
require('./_object-to-array-8fe5a54f.js');
require('./toConsumableArray-ddb70795.js');
require('react-bootstrap/Pagination');
require('./entities-3f9d0f2f.js');
require('./AdSlot.js');
require('./debounce-d4a2369b.js');
require('./isSymbol-03abbdf0.js');
require('./toNumber-5e1fac58.js');
require('react-bootstrap/Row');
require('./inherits-c2065b95.js');
require('./_object-sap-a175b4a9.js');
require('./define-property-c88be878.js');
require('./index.esm-974f47d9.js');
require('./iconBase-68959171.js');
require('react-bootstrap/Figure');
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 Col__default = /*#__PURE__*/_interopDefaultLegacy(Col);
var Form__default = /*#__PURE__*/_interopDefaultLegacy(Form);
var Button__default = /*#__PURE__*/_interopDefaultLegacy(Button);

var _this = undefined;

var cache = { topic_ids: [] };

var VideoProgramLandingPage = function VideoProgramLandingPage(props) {
  var client = props.client,
      virtualBlock = props.virtualBlock,
      topics = props.topics,
      posts = props.posts,
      query = props.query,
      params = props.params,
      _props$currentPage = props.currentPage,
      currentPage = _props$currentPage === undefined ? 1 : _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$autoScroll = props.autoScroll,
      autoScroll = _props$autoScroll === undefined ? true : _props$autoScroll,
      defaultImage = props.defaultImage,
      mapping = props.mapping;

  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())),
    React__default['default'].createElement(
      'div',
      { className: 'peer-exchange-filter-container' },
      React__default['default'].createElement(
        Form__default['default'],
        { className: 'ml-3', method: 'post' },
        React__default['default'].createElement(
          Form__default['default'].Row,
          null,
          React__default['default'].createElement(
            Col__default['default'],
            null,
            React__default['default'].createElement(
              Form__default['default'].Group,
              { controlId: 'topic' },
              React__default['default'].createElement(
                Form__default['default'].Control,
                { as: 'select', name: 'topic_id', placeholder: 'Filter by Topic' },
                React__default['default'].createElement(
                  'option',
                  { value: '' },
                  'All Topics'
                ),
                topics && topics.map(function (topic) {
                  if (props.topic_id === topic._id || topic.name == 'Peer Exchange') {
                    return null;
                  } else {
                    return React__default['default'].createElement(
                      'option',
                      { value: topic._id },
                      topic.name
                    );
                  }
                })
              )
            )
          ),
          React__default['default'].createElement(
            Col__default['default'],
            null,
            React__default['default'].createElement(
              Button__default['default'],
              { type: 'submit', variant: 'secondary' },
              'Apply'
            )
          )
        )
      )
    ),
    posts && React__default['default'].createElement(VideoSeriesListing, {
      client: client,
      mapping: mapping,
      dataRecord: posts,
      query: query,
      params: params,
      currentPage: currentPage,
      pageview: pageview,
      columns: columns,
      variant: variant,
      page: page,
      autoScroll: autoScroll,
      defaultImage: defaultImage
    })
  );
};

VideoProgramLandingPage.getInitialData = function () {
  var _ref = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee(context, client) {
    var itemsPerPage = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 6;
    var topicId = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
    var taxonomyId = arguments[4];
    var pathname, virtualBlock, query, taxonomy_ids_res, taxonomy_ids, i, res, j, topics, urlQuery, currentPage, start, params, posts;
    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            // Virtual Block
            pathname = context.pathname;
            _context.next = 3;
            return client.fetch('*[_type == \'virtualBlock\' && identifier == \'' + pathname + '\'][0]');

          case 3:
            virtualBlock = _context.sent;


            // Topics for filtering
            query = '';

            if (!(cache && cache.topic_ids && cache.topic_ids.length > 0)) {
              _context.next = 8;
              break;
            }

            _context.next = 15;
            break;

          case 8:
            query = '*[\n        _type == \'article\' \n        && !(_id in path("drafts.**")) \n        && defined(title) \n        && defined(url)\n        && references(\'' + taxonomyId + '\') \n        && is_visible == true \n        && dateTime(published) <= dateTime(now())\n    ]{\n        \'taxonomy_id\': taxonomyMapping[]->_id\n    }';
            _context.next = 11;
            return client.fetch(query);

          case 11:
            taxonomy_ids_res = _context.sent;
            taxonomy_ids = [];

            for (i = 0; i < taxonomy_ids_res.length; i++) {
              res = taxonomy_ids_res[i];

              for (j = 0; j < res.taxonomy_id.length; j++) {
                if (!taxonomy_ids.includes(res.taxonomy_id[j])) {
                  taxonomy_ids.push(res.taxonomy_id[j]);
                }
              }
            }
            cache.topic_ids = taxonomy_ids;

          case 15:
            query = '*[_type == \'taxonomy\' && _id in $taxonomy_ids] | order(name asc)';
            _context.next = 18;
            return client.fetch(query, { taxonomy_ids: cache.topic_ids });

          case 18:
            topics = _context.sent;


            // Posts
            urlQuery = context.query;
            currentPage = urlQuery.page ? parseInt(urlQuery.page) : 1;
            start = (currentPage - 1) * itemsPerPage;
            params = {
              from: start,
              to: start + itemsPerPage,
              url: pathname
            };

            query = '*[\n        _type == \'article\'\n        && !(_id in path("drafts.**"))\n        && defined(title)\n        && defined(url)\n        && is_visible == true\n        && dateTime(published) <= dateTime(now())\n        && references(\'' + taxonomyId + '\')\n        ' + (topicId ? '&& references(\'' + topicId + '\')' : '') + '\n      ] | order(published desc) [$from...$to]{\n  \t\t...,\n  \t\ttitle,\n  \t\tsummary,\n  \t\tpublished,\n  \t\tthumbnail,\n  \t\turl,\n  \t\ttaxonomy,\n  \t\tcontentCategory->,\n  \t\tbody[_type == "youtubeGallery"],\n  \t\t\'pageNumber\': ' + currentPage + '\n  \t}';
            _context.next = 26;
            return client.fetch(query, params);

          case 26:
            posts = _context.sent;
            return _context.abrupt('return', { virtualBlock: virtualBlock, topics: topics, posts: posts, query: query, params: params, currentPage: currentPage });

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

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

module.exports = VideoProgramLandingPage;
