'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

require('./_commonjsHelpers-06173234.js');
require('./_to-object-7a3ce269.js');
require('./es6.string.iterator-ab94a594.js');
require('./_library-dd23b178.js');
require('./_iter-detect-ef35661e.js');
require('./toConsumableArray-4fdb72e2.js');
require('./core.get-iterator-method-6cd7682d.js');
require('./_object-pie-68c5bcbc.js');
var _extends = require('./extends-acc1318d.js');
require('./_object-sap-b231f928.js');
require('./inherits-bfdb22af.js');
require('./define-property-fc002cec.js');
require('./web.dom.iterable-68e3aaf2.js');
require('./typeof-e83d4a26.js');
require('./_is-array-39ff55dd.js');
var React = require('react');
var React__default = _interopDefault(React);
require('./events-1b438fe7.js');
require('./index-98e01cb9.js');
require('prop-types');
require('react-bootstrap/Row');
require('react-bootstrap/Col');
require('./moment-ca3bd9fb.js');
var Router = require('next/router');
var Router__default = _interopDefault(Router);
require('./isSymbol-04666465.js');
require('./debounce-6191c8ee.js');
require('./toNumber-f2f8744b.js');
require('react-dom');
require('./index-ff69979f.js');
require('./urlFor.js');
require('./index-96b3db50.js');
require('./main-bc7d4bf8.js');
require('./entities-310b46ee.js');
require('./slicedToArray-41819145.js');
require('./stringify-7ccc8524.js');
var asyncToGenerator = require('./asyncToGenerator-8ae2191c.js');
require('./Segment.js');
require('./Beam.js');
require('./AdSlot.js');
require('react-bootstrap/Button');
var BlockContent = require('./BlockContent-bac780ac.js');
require('./index-187c967e.js');
require('./smoothscroll-5054361f.js');
require('./GroupDeck.js');
require('react-bootstrap');
require('./iconBase-68959171.js');
require('./index.esm-974f47d9.js');
var getSerializers = require('./index-80802a98.js');
require('./util-f2c1b65b.js');
require('./brightcove-react-player-loader.es-05029198.js');
var Head = _interopDefault(require('next/head'));
require('react-share');
require('./SocialShare.js');
require('./_object-to-array-c3aeb51d.js');
require('react-bootstrap/Pagination');
require('react-bootstrap/Figure');

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.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 = Router.useRouter();
  return data && data.length > 0 && React__default.createElement(
    'section',
    { className: 'taxonomy-description' },
    setMetaDescription(data, router),
    React__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;
