'use strict';

var asyncToGenerator = require('./asyncToGenerator-58c289d9.js');
var _extends = require('./extends-29bdbc6d.js');
var React = require('react');
var Router = require('next/router');
var Head = require('next/head');
var BlockContent = require('./BlockContent-36e54d77.js');
var getSerializers = require('./index-70cfabd2.js');
require('./_commonjsHelpers-06173234.js');
require('./es6.string.iterator-c2573ffd.js');
require('./_to-object-6de10e57.js');
require('./web.dom.iterable-46657b5c.js');
require('./_library-dd23b178.js');
require('./core.get-iterator-method-f474eacc.js');
require('./_iter-detect-55a5657a.js');
require('./_object-pie-083f2dd6.js');
require('prop-types');
require('./index-efca863c.js');
require('./slicedToArray-5d1bc3b3.js');
require('react-bootstrap');
require('./index-0e4427c8.js');
require('./index-187c967e.js');
require('./util-6f784d85.js');
require('./Beam.js');
require('./stringify-c9ac1214.js');
require('./Segment.js');
require('./main-025fdc78.js');
require('./index-eadd5035.js');
require('react-dom');
require('./typeof-88a668c5.js');
require('./_is-array-6128bd56.js');
require('./brightcove-react-player-loader.es-a195642e.js');
require('./GroupDeck.js');
require('./smoothscroll-99df7f93.js');
require('./moment-aca40de1.js');
require('./SocialShare.js');
require('react-share');
require('./_object-to-array-8fe5a54f.js');
require('./toConsumableArray-ddb70795.js');
require('react-bootstrap/Pagination');
require('react-bootstrap/Button');
require('./entities-3f9d0f2f.js');
require('./AdSlot.js');
require('./debounce-d4a2369b.js');
require('./isSymbol-03abbdf0.js');
require('./toNumber-5e1fac58.js');
require('react-bootstrap/Row');
require('react-bootstrap/Col');
require('./inherits-c2065b95.js');
require('./_object-sap-a175b4a9.js');
require('./define-property-c88be878.js');
require('./index.esm-974f47d9.js');
require('./iconBase-68959171.js');
require('react-bootstrap/Figure');

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

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

var _this = undefined;

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

  var defaults = { nonTextBehavior: 'remove' };
  var options = _extends._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['default'].createElement(
    Head__default['default'],
    null,
    React__default['default'].createElement('meta', { name: 'description', content: metaDescription, key: 'description' })
  );
};

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

  var router = Router.useRouter();
  return data && data.length > 0 && React__default['default'].createElement(
    'section',
    { className: 'taxonomy-description' },
    setMetaDescription(data, router),
    React__default['default'].createElement(BlockContent.BlockContent, _extends._extends({ serializers: getSerializers.getSerializers(client), blocks: data, imageOptions: { w: 320, h: 240, fit: 'max' } }, client.config()))
  );
};

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

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

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

            console.error(_context.t0);

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

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

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

module.exports = TaxonomyDescription;
