'use strict';

var asyncToGenerator = require('./asyncToGenerator-65df6bf8.js');
var slicedToArray = require('./slicedToArray-067dd3bc.js');
var React = require('react');
var Router = require('next/router');
var reactBootstrap = require('react-bootstrap');
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');

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

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

var _this = undefined;

var VideoSeriesFilter = function VideoSeriesFilter(props) {
  var filterData = props.filterData,
      queryProps = props.queryProps,
      filteredList = props.filteredList,
      loadingStatus = props.loadingStatus,
      _props$exclude = props.exclude,
      exclude = _props$exclude === undefined ? null : _props$exclude,
      router = props.router;
  var taxonomyID = queryProps.taxonomyID,
      currentPage = queryProps.currentPage,
      client = queryProps.client,
      params = queryProps.params;

  var _useState = React.useState({ name: 'All Topics', id: '' }),
      _useState2 = slicedToArray._slicedToArray(_useState, 2),
      filter = _useState2[0],
      setFilter = _useState2[1];

  var filterList = function () {
    var _ref = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee() {
      var query;
      return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              loadingStatus && loadingStatus(true);
              filteredList(null);
              query = '*[_type == \'article\'\n                    && !(_id in path("drafts.**"))\n                    && defined(title)\n                    && defined(url) ' + (filter.id ? ' && references(\'' + filter.id + '\')' : '') + '\n                    && references(\'' + taxonomyID + '\')\n                    && is_visible == true\n                    && published <= now()\n                    && passwordLock != true\n                  ] | order(published desc) [$from...$to]{\n                  ...,\n                  title,\n                  summary,\n                  published,\n                  thumbnail{ ..., asset->},\n                  url,\n                  taxonomy,\n                  contentCategory->,\n                  body[_type == "youtubeGallery"]{\n                    ...,\n                    videos[]{\n                      ...,\n                      thumbnail{...,asset->}\n                    }\n                  },\n                  \'pageNumber\': ' + currentPage + '\n                }';
              _context.next = 5;
              return client.fetch(query, params).then(function (res) {
                filteredList({ posts: res, query: query });
              });

            case 5:
              loadingStatus && loadingStatus(false);

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

    return function filterList() {
      return _ref.apply(this, arguments);
    };
  }();
  React.useEffect(function () {
    if (router && router.query && router.query.filter && filterData && filterData.length > 0) {
      filterData.map(function (filterItem) {
        if (filterItem.name === router.query.filter) {
          setFilter({ name: filterItem.name, id: filterItem._id });
        }
      });
    }
  }, []);
  React.useEffect(function () {
    filterList();
    var qString = '';
    router.asPath.includes('?') && router.asPath.slice(router.asPath.indexOf('?') + 1).split('&').forEach(function (item) {
      if (item.includes('=') && item.split('=')[0] !== 'filter') {
        if (qString === '') {
          qString += item.split('=')[0] + '=' + item.split('=')[1];
        } else {
          qString += '&' + item.split('=')[0] + '=' + item.split('=')[1];
        }
      }
    });
    if (filter.id !== '') {
      router.push(router.pathname, router.pathname + '?filter=' + filter.name + (qString !== '' ? '&' + qString : ''), {
        shallow: true
      });
    } else {
      router.push(router.pathname, '' + router.pathname + (qString !== '' ? '?' + qString : ''), {
        shallow: true
      });
    }
  }, [filter]);
  return React__default["default"].createElement(
    React__default["default"].Fragment,
    null,
    React__default["default"].createElement(
      reactBootstrap.DropdownButton,
      { className: 'filter_dropdown', variant: 'primary', title: filter.name },
      React__default["default"].createElement(
        reactBootstrap.Dropdown.Item,
        { key: 'all', onClick: function onClick() {
            return setFilter({ name: 'All Topics', id: '' });
          } },
        'All Topics'
      ),
      filterData && filterData.map(function (filterItem) {
        if (filterItem.identifier !== exclude) {
          return React__default["default"].createElement(
            reactBootstrap.Dropdown.Item,
            { key: filterItem._id, onClick: function onClick() {
                return setFilter({ name: filterItem.name, id: filterItem._id });
              }, value: filterItem._id, selected: true },
            filterItem.name
          );
        }
      })
    )
  );
};
var VideoSeriesFilter$1 = Router.withRouter(VideoSeriesFilter);

module.exports = VideoSeriesFilter$1;
