import { _ as _extends, a as _Object$assign } from './extends-6d8e3924.js';
import { _ as _asyncToGenerator, r as regenerator } from './asyncToGenerator-958ef5fc.js';
import { _ as _slicedToArray } from './slicedToArray-6ebcbfea.js';
import React__default, { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import Head from 'next/head';
import { B as BlockContent } from './BlockContent-1e73bd2c.js';
import { g as getSerializers } from './index-cefa7325.js';
import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-ea258bb1.js';
import './_object-pie-133f504a.js';
import './web.dom.iterable-ab4dea5c.js';
import './_library-528f1934.js';
import './_iter-detect-98db3b07.js';
import 'prop-types';
import './index-3f5c03b2.js';
import 'react-bootstrap';
import './urlFor.js';
import './index-f0fc23eb.js';
import './events-6c3a7a63.js';
import './index-c7e2ac95.js';
import './util-8ed16d48.js';
import './Beam.js';
import './stringify-2b084bee.js';
import './Segment.js';
import './main-1894c974.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 'react-bootstrap/Button';
import 'react-bootstrap/Form';
import './GroupDeck.js';
import './smoothscroll-fa4e3f8c.js';
import './moment-bc12cb97.js';
import './SocialShare.js';
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 'react-bootstrap/Col';
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';

var _this = undefined;

var blocksToText = function blocksToText(blocks) {
  var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

  var defaults = { nonTextBehavior: 'remove' };
  var options = _Object$assign({}, defaults, opts);
  return blocks.map(function (block) {
    if (block._type !== 'block' || !block.children) {
      return options.nonTextBehavior === 'remove' ? '' : '[' + block._type + ' block]';
    }
    return block.children.map(function (child) {
      return child.text;
    }).join('');
  }).join('\n\n');
};

var setMetaDescription = function setMetaDescription(data, router) {
  var metaDescription = blocksToText(data);
  if (router && router.asPath) {
    var queryParms = router.asPath.split('?')[1];
    if (queryParms) {
      var pageNumber = queryParms.split('&').reduce(function (acc, item) {
        if (item.startsWith('page=')) {
          if (item.split('=')[1]) {
            acc = item.split('=')[1];
            return acc;
          }
          return acc;
        }
        return acc;
      }, '');
      if (pageNumber && pageNumber !== '') {
        metaDescription = blocksToText(data) + '| Page ' + pageNumber;
      }
    }
  }
  return React__default.createElement(
    Head,
    null,
    React__default.createElement('meta', { name: 'description', content: metaDescription, key: 'description' })
  );
};

var TaxonomyDescription = function TaxonomyDescription(_ref) {
  var data = _ref.data,
      client = _ref.client;

  var router = useRouter();

  var _useState = useState(true),
      _useState2 = _slicedToArray(_useState, 2),
      changeDescription = _useState2[0],
      setChangeDescription = _useState2[1];

  useEffect(_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
    var querySeoManager, description;
    return regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!(router && router.asPath)) {
              _context.next = 6;
              break;
            }

            querySeoManager = '*[_type == \'seoManager\' && urlSeo == \'' + router.asPath + '\'][0].metaDescription';
            _context.next = 4;
            return client.fetch(querySeoManager);

          case 4:
            description = _context.sent;

            if (description && description != null) {
              setChangeDescription(false);
            }

          case 6:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, _this);
  })), []);
  return data && data.length > 0 ? React__default.createElement(
    'section',
    { className: 'taxonomy-description' },
    changeDescription ? setMetaDescription(data, router) : null,
    React__default.createElement(BlockContent, _extends({ serializers: getSerializers(client), blocks: data, imageOptions: { w: 320, h: 240, fit: 'max' } }, client.config()))
  ) : null;
};

TaxonomyDescription.getDescription = function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() {
    var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var client = arguments[1];
    var query, summary;
    return regenerator.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            query = '*[_type == \'taxonomy\' && identifier == \'' + url + '\'][0].summary';
            summary = [];
            _context2.prev = 2;
            _context2.next = 5;
            return client.fetch(query);

          case 5:
            summary = _context2.sent;
            _context2.next = 11;
            break;

          case 8:
            _context2.prev = 8;
            _context2.t0 = _context2['catch'](2);

            console.error(_context2.t0);

          case 11:
            return _context2.abrupt('return', summary);

          case 12:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, _this, [[2, 8]]);
  }));

  return function () {
    return _ref3.apply(this, arguments);
  };
}();

export default TaxonomyDescription;
