import { _ as _asyncToGenerator, r as regenerator } from './asyncToGenerator-958ef5fc.js';
import { _ as _extends } from './extends-6d8e3924.js';
import React__default from 'react';
import { B as BlockContent } from './BlockContent-1e73bd2c.js';
import Col from 'react-bootstrap/Col';
import Form from 'react-bootstrap/Form';
import Button from 'react-bootstrap/Button';
import { g as getSerializers } from './index-076baa11.js';
import VideoSeriesListing from './VideoSeriesListing.js';
import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-ea258bb1.js';
import './web.dom.iterable-ab4dea5c.js';
import './_library-528f1934.js';
import './_iter-detect-98db3b07.js';
import './_object-pie-133f504a.js';
import 'prop-types';
import './index-3f5c03b2.js';
import './slicedToArray-6ebcbfea.js';
import 'react-bootstrap';
import './urlFor.js';
import './index-f0fc23eb.js';
import './events-6c3a7a63.js';
import 'next/router';
import './index-c7e2ac95.js';
import './empty-a8042e3c.js';
import './Beam.js';
import './stringify-2b084bee.js';
import './Segment.js';
import './main-9f6806ea.js';
import './urlForFile-b69adffc.js';
import './index-755f2cc2.js';
import 'react-dom';
import './typeof-e9fc0c5d.js';
import './_is-array-8d21b9e3.js';
import './brightcove-react-player-loader.es-57a70a56.js';
import 'react-bootstrap/Modal';
import './index-e7075467.js';
import './GroupDeck.js';
import './smoothscroll-fa4e3f8c.js';
import './moment-bc12cb97.js';
import './SocialShare.js';
import 'next/head';
import 'react-share';
import './_object-to-array-2300e51b.js';
import './toConsumableArray-f7074d7c.js';
import 'react-bootstrap/Pagination';
import './entities-6d9154b4.js';
import './AdSlot.js';
import './debounce-533d9748.js';
import './isSymbol-a7e9aafe.js';
import './toNumber-5aeba6e7.js';
import 'react-bootstrap/Row';
import './inherits-a6ba2ec4.js';
import './_object-sap-7cee6f84.js';
import './define-property-5f1bda73.js';
import './index.esm-eb1e8e80.js';
import './iconBase-ccc56b97.js';
import 'react-bootstrap/Figure';
import 'react-bootstrap/OverlayTrigger';
import 'react-bootstrap/Tooltip';
import 'react-bootstrap/Popover';
import './checkSisterSite-baa4fb6b.js';
import 'react-bootstrap/Container';
import './visibility-sensor-72fd8f8b.js';
import './YoutubeGroup.js';
import './index.es-a6137319.js';

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.createElement(
    'div',
    null,
    virtualBlock && React__default.createElement(BlockContent, _extends({ serializers: getSerializers(client), blocks: virtualBlock.body, imageOptions: { w: 320, h: 240, fit: 'max' } }, client.config())),
    React__default.createElement(
      'div',
      { className: 'peer-exchange-filter-container' },
      React__default.createElement(
        Form,
        { className: 'ml-3', method: 'post' },
        React__default.createElement(
          Form.Row,
          null,
          React__default.createElement(
            Col,
            null,
            React__default.createElement(
              Form.Group,
              { controlId: 'topic' },
              React__default.createElement(
                Form.Control,
                { as: 'select', name: 'topic_id', placeholder: 'Filter by Topic' },
                React__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.createElement(
                      'option',
                      { value: topic._id },
                      topic.name
                    );
                  }
                })
              )
            )
          ),
          React__default.createElement(
            Col,
            null,
            React__default.createElement(
              Button,
              { type: 'submit', variant: 'secondary' },
              'Apply'
            )
          )
        )
      )
    ),
    posts && React__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( /*#__PURE__*/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 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,
              itemsPerPage: itemsPerPage
            };

            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{ ..., asset->},\n  \t\turl,\n  \t\ttaxonomy,\n  \t\tcontentCategory->,\n  \t\tbody[_type == "youtubeGallery"]{\n        ...,\n        videos[]{\n          ...,\n          thumbnail{...,asset->}\n        }\n      },\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);
  };
}();

export { VideoProgramLandingPage as default };
