import './_commonjsHelpers-0c4b6f40.js';
import './_to-object-a4107da3.js';
import './es6.string.iterator-c990c18c.js';
import './_library-528f1934.js';
import './_iter-detect-5d49a330.js';
import './core.get-iterator-method-e1de7503.js';
import './web.dom.iterable-4439f05a.js';
import React__default, { useState, useEffect } from 'react';
import { withRouter } from 'next/router';
import { _ as _slicedToArray } from './slicedToArray-641f241f.js';
import { a as _asyncToGenerator, r as regenerator } from './asyncToGenerator-502f13b4.js';
import './_set-species-3f8319f5.js';
import { DropdownButton, Dropdown } from 'react-bootstrap';

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 = useState({ name: 'All Topics', id: '' }),
      _useState2 = _slicedToArray(_useState, 2),
      filter = _useState2[0],
      setFilter = _useState2[1];

  var filterList = function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
      var query;
      return 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,\n                  url,\n                  taxonomy,\n                  contentCategory->,\n                  body[_type == "youtubeGallery"],\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);
    };
  }();
  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 });
        }
      });
    }
  }, []);
  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.createElement(
    React__default.Fragment,
    null,
    React__default.createElement(
      DropdownButton,
      { className: 'filter_dropdown', variant: 'primary', title: filter.name },
      React__default.createElement(
        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.createElement(
            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 = withRouter(VideoSeriesFilter);

export default VideoSeriesFilter$1;
