import { _ as _extends } from './extends-60e0e1c6.js';
import { _ as _inherits, a as _classCallCheck, b as _possibleConstructorReturn, c as _Object$getPrototypeOf, d as _createClass } from './inherits-a41fe93f.js';
import React from 'react';
import { l as lib_3 } from './index-2446af28.js';
import Container from 'react-bootstrap/Container';
import { V as VisibilitySensor } from './visibility-sensor-f40c2564.js';
import { I as IoIosArrowForward } from './index.esm-3fdda599.js';
import { m as moment } from './moment-bc12cb97.js';
import { withRouter } from 'next/router';
import YoutubeGroup from './YoutubeGroup.js';
import { d as debounce_1 } from './debounce-533d9748.js';
import { I as InfiniteScroll } from './index.es-c6222053.js';
import './_commonjsHelpers-0c4b6f40.js';
import './_to-object-b50e61c3.js';
import './_object-pie-133f504a.js';
import './es6.string.iterator-bc33758b.js';
import './_object-sap-f353c2a9.js';
import './define-property-fe54dc9f.js';
import './typeof-a286cf0c.js';
import './web.dom.iterable-4109ff68.js';
import './_is-array-3e99e06e.js';
import './_library-528f1934.js';
import './events-6c3a7a63.js';
import 'prop-types';
import 'react-dom';
import './iconBase-602d52fe.js';
import './slicedToArray-be2f7a22.js';
import './core.get-iterator-method-133dfacd.js';
import './index-c7e2ac95.js';
import './index-35bdcc81.js';
import './smoothscroll-fa4e3f8c.js';
import './urlFor.js';
import './index-0dbff1d1.js';
import './main-b5c5aac1.js';
import './isSymbol-a7e9aafe.js';
import './toNumber-5aeba6e7.js';

var ArticleSeriesListing = function (_React$Component) {
  _inherits(ArticleSeriesListing, _React$Component);

  function ArticleSeriesListing() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, ArticleSeriesListing);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ArticleSeriesListing.__proto__ || _Object$getPrototypeOf(ArticleSeriesListing)).call.apply(_ref, [this].concat(args))), _this), _this.page = _this.props.page, _this.mapping = _this.props.mapping, _this.data = _this.props.dataRecord, _this.query = _this.props.query, _this.params = _this.props.params, _this.autoScroll = _this.props.autoScroll ? _this.props.autoScroll : false, _this.today = moment().utc().format(), _this.dataArr = [_this.data], _this.defaultImage = _this.props.defaultImage ? _this.props.defaultImage : '/placeholder.jpg', _this.state = {
      data: _this.dataArr.map(function (item) {
        var tempObj = item;
        if (tempObj.body) {
          var filteredBody = tempObj.body.map(function (item) {
            if (item._type === 'articleSeries') {
              var innerObj = item;
              var filteredArticles = innerObj.articles.filter(function (article) {
                if ((article.is_visible === true || !article.hasOwnProperty('is_visible')) && _this.today >= article.published) {
                  return article;
                }
              });
              innerObj.body = filteredArticles;
              return innerObj;
            } else {
              return item;
            }
          });

          tempObj.body = filteredBody;
        }
        return tempObj;
      }),
      per: _this.params ? _this.params.to : 2,
      page: _this.props.currentPage || 1,
      from: _this.params ? _this.params.from : 0,
      to: _this.params ? _this.params.to : 2,
      total_pages: null,
      scrolling: _this.autoScroll,
      query: _this.query,
      currentPage: _this.props.currentPage || 1
    }, _this.loadMore = debounce_1(function () {
      _this.setState(function (state) {
        var page = state.page,
            from = state.from,
            per = state.per,
            to = state.to;

        return {
          page: page + 1,
          from: from + per,
          to: to + per
        };
      }, _this.loadData);
    }, 0), _this.loadData = function () {
      var _this$state = _this.state,
          from = _this$state.from,
          to = _this$state.to;
          _this$state.data;
          var query = _this$state.query,
          page = _this$state.page;
      var client = _this.props.client;
      //this.changePageNumber(page)

      var params = _extends({}, _this.params, { from: from, to: to
        // const queryUpdated = query.replace('$from', params.from).replace('$to', params.to)
        // const query = '*[_type == "article" && defined(title) && defined(thumbnail)][$from...$to] { title, summary, thumbnail { asset-> }, url }'
      });client.fetch(query, params).then(function (dataArr) {
        if (_this.pointer && _this.pointerArray) {
          var pointer = _this.pointer;
          dataArr = dataArr[_this.pointerArray][pointer];
        }
        dataArr = dataArr.map(function (item) {
          return _extends({}, item, {
            pageNumber: page
          });
        });

        _this.setState(function () {
          if (dataArr.length > 0) {
            var newDataArr = _this.state.data;
            newDataArr.push(dataArr);
            return {
              data: newDataArr,
              scrolling: true
            };
          } else {
            return {
              scrolling: false
            };
          }
        });
      });
    }, _this.changePageNumber = function (pageNumber) {
      var _this$props = _this.props,
          seoPaginate = _this$props.seoPaginate,
          pageview = _this$props.pageview,
          router = _this$props.router;
      var currentPage = _this.state.currentPage;

      if (seoPaginate) {
        var path = router.asPath;
        var qrIndex = path.indexOf('?');
        var pathname = router.pathname;
        var queryString = '';

        if (qrIndex > 0) {
          path = path.substring(1, qrIndex);

          var partialQS = router.asPath.substring(qrIndex + 1);
          var partialQSArr = partialQS.split('&');

          // exclude page=xxx from query string
          partialQSArr.map(function (item) {
            var itemArr = item.split('=');
            var key = itemArr[0];
            var val = itemArr[1];

            if (key !== 'page') {
              queryString += (queryString.length === 0 ? '' : '&') + key + '=' + val;
            }
          });
        }

        if (queryString.length > 0) {
          path += '?' + queryString;
        }

        pageNumber = parseInt(pageNumber);

        if (currentPage !== pageNumber) {
          lib_3.refresh();

          _this.setState({
            currentPage: pageNumber
          }, function () {
            if (path[0] !== '/') {
              path = '/' + path;
            }

            var newPath = pageNumber === 1 ? '' + path : '' + path + (path.includes('?') ? '&page=' + pageNumber : '?page=' + pageNumber);

            if (pageview) {
              pageview(newPath);
            }

            router.push(pathname, newPath, {
              shallow: true
            });
          });
        }
      }
    }, _this.renderManualPagination = function () {
      var currentPage = _this.state.currentPage;

      return React.createElement(
        'div',
        { className: 'd-flex justify-content-between' },
        currentPage && currentPage > 1 && React.createElement(
          'a',
          { href: '?page=' + (currentPage - 1) },
          '<< Previous'
        ),
        React.createElement(
          'a',
          { href: '?page=' + (currentPage + 1) },
          'Next >>'
        )
      );
    }, _this.renderSeries = function (seriesData) {
      var itemCounter = 0;

      return React.createElement(
        'div',
        null,
        seriesData && seriesData.map(function (docSeries, index) {
          if (docSeries.body && docSeries.body.length > 0 && docSeries.body[0].articles) {
            docSeries.body[0].articles.map(function (singleArticle, indexInner) {
              singleArticle.link = '/view/' + docSeries.url.current + '?seriesArticle=' + singleArticle.param.current;
            });
          }

          return React.createElement(
            'div',
            { key: index, counter: itemCounter++ },
            React.createElement(
              'h4',
              { className: 'article-title-banner', style: { fontWeight: 'bold', color: 'var(--primary)', padding: '.25rem 0', borderBottom: '2px solid #d4a64d' } },
              React.createElement(
                'a',
                { href: '/' + _this.props.page + '/' + docSeries.url.current },
                docSeries.title,
                React.createElement(
                  'span',
                  { style: { fontSize: '.9rem', color: 'grey', marginLeft: '1rem', transform: 'translateY(-2px)', display: 'inline-block' } },
                  React.createElement(IoIosArrowForward, null)
                )
              )
            ),
            docSeries.body && docSeries.body.length > 0 && docSeries.body[0].articles ? React.createElement(YoutubeGroup, _extends({ key: index, dataset: docSeries.body[0].articles }, _this.props)) : React.createElement(
              'p',
              null,
              'No Article Found.'
            )
          );
        })
      );
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(ArticleSeriesListing, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      return React.createElement(
        React.Fragment,
        null,
        this.state.data.length >= 1 && this.state.data[0].length > 0 ? //Check if any Video Groups available
        React.createElement(
          InfiniteScroll,
          { dataLength: this.state.data.length, next: this.loadMore, hasMore: this.state.scrolling, scrollThreshold: 0.8 },
          React.createElement(
            Container,
            null,
            this.state.data && this.state.data.map(function (series, index) {
              var page = series && series.length > 0 && series[0].pageNumber !== 1 ? series[0].pageNumber : 1;

              return React.createElement(
                React.Fragment,
                { key: index },
                React.createElement(
                  VisibilitySensor,
                  {
                    onChange: function onChange(isVisible) {
                      isVisible && _this2.changePageNumber(page);
                    } },
                  React.createElement('div', { className: 'visibility-sensor', style: { minHeight: '.25rem' } })
                ),
                React.createElement(
                  'div',
                  null,
                  _this2.renderSeries(series)
                )
              );
            })
          )
        ) : React.createElement(
          'div',
          { style: { marginTop: '10px' } },
          React.createElement(
            'h5',
            null,
            React.createElement(
              'em',
              null,
              'New content coming soon, please check back later.'
            )
          )
        )
      );
    }
  }]);

  return ArticleSeriesListing;
}(React.Component);

var ArticleSeriesListing$1 = withRouter(ArticleSeriesListing);

export default ArticleSeriesListing$1;
