'use strict';

var _extends = require('./extends-be0d85bc.js');
var asyncToGenerator = require('./asyncToGenerator-65df6bf8.js');
var slicedToArray = require('./slicedToArray-067dd3bc.js');
var React = require('react');
var Router = require('next/router');
var Head = require('next/head');
var BlockContent = require('./BlockContent-96b44364.js');
var getSerializers = require('./index-e412e727.js');
require('./_commonjsHelpers-06173234.js');
require('./core.get-iterator-method-5643aa10.js');
require('./web.dom.iterable-d98303e0.js');
require('./_library-dd23b178.js');
require('./_set-species-0c7864a2.js');
require('./_iter-detect-b1df62f0.js');
require('prop-types');
require('./index-d48b231c.js');
require('react-bootstrap');
require('./urlFor.js');
require('./index-7435b964.js');
require('./index-187c967e.js');
require('./Beam.js');
require('./stringify-f7a37a8d.js');
require('./Segment.js');
require('./main-812888f1.js');
require('./urlForFile-5084fc48.js');
require('./index-65621b6c.js');
require('react-dom');
require('./typeof-220bbf73.js');
require('./_is-array-54228b29.js');
require('./brightcove-react-player-loader.es-35faab67.js');
require('./_object-to-array-9337ba83.js');
require('./keys-e982b3fa.js');
require('./_object-sap-d1978572.js');
require('react-bootstrap/Modal');
require('react-bootstrap/Button');
require('react-bootstrap/Form');
require('react-ga');
require('./GroupDeck.js');
require('./smoothscroll-99df7f93.js');
require('./moment-f730814d.js');
require('./SocialShare.js');
require('react-share');
require('./toConsumableArray-d7797c2b.js');
require('react-bootstrap/Pagination');
require('./entities-df1aed86.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-e47b5342.js');
require('./define-property-e8404b64.js');
require('./index.esm-d81454e7.js');
require('./iconBase-be4097c0.js');
require('react-bootstrap/Figure');
require('react-bootstrap/OverlayTrigger');
require('react-bootstrap/Tooltip');
require('react-bootstrap/Popover');
require('./checkSisterSite-c3a4ac98.js');

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();

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

  React.useEffect(asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee() {
    var querySeoManager, description;
    return asyncToGenerator.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["default"].createElement(
    'section',
    { className: 'taxonomy-description' },
    changeDescription ? setMetaDescription(data, router) : null,
    React__default["default"].createElement(BlockContent.BlockContent, _extends._extends({ serializers: getSerializers.getSerializers(client), blocks: data, imageOptions: { w: 320, h: 240, fit: 'max' } }, client.config()))
  ) : null;
};

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

          case 5:
            taxonomyData = _context2.sent;

            if (taxonomyData.summary) {
              summary = taxonomyData.summary;
            }
            _context2.next = 12;
            break;

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

            console.error(_context2.t0);

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

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

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

module.exports = TaxonomyDescription;
