'use strict';

require('./_commonjsHelpers-06173234.js');
require('./_to-object-329a650b.js');
require('./es6.string.iterator-cc0bbaa4.js');
require('./_library-dd23b178.js');
require('./_iter-detect-60b2f026.js');
require('./core.get-iterator-method-f62321d4.js');
require('./web.dom.iterable-a0e279c1.js');
var asyncToGenerator = require('./asyncToGenerator-533d476a.js');
require('./_set-species-f92c67c5.js');
var nookies = require('nookies');
var getQuery = require('./getQuery.js');

var _this = undefined;

var getRelatedArticle = function () {
  var _ref = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee() {
    var ctx = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
    var client = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
    var article = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var prevUrl = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '/';
    var articleCount = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;

    var _article$url, url, documentGroupMapping, issueGroup, content_placement, references, params, articles, filters, cookies, prevSlugs, query;

    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _article$url = article.url;
            _article$url = _article$url === undefined ? {
              current: null
            } : _article$url;
            url = _article$url.current, documentGroupMapping = article.documentGroupMapping, issueGroup = article.issueGroup;
            content_placement = article.content_placement;
            references = [];


            content_placement = content_placement && content_placement.length > 0 && content_placement.filter(function (_ref2) {
              var identifier = _ref2.identifier;
              return identifier !== 'news';
            });
            if (content_placement && content_placement.length > 0) {
              content_placement.forEach(function (_ref3) {
                var path = _ref3.path,
                    _id = _ref3._id;

                if (!_id || typeof _id == 'undefined') {
                  return null;
                }
                var prev_url_to_check = prevUrl;
                if (prev_url_to_check.includes('?')) {
                  prev_url_to_check = prev_url_to_check.split('?')[0];
                }
                if (prev_url_to_check.endsWith(path) && _id) references.push(_id);
              });
              if (references.length === 0) {
                content_placement.forEach(function (_ref4) {
                  var _id = _ref4._id;

                  if (_id) references.push(_id);
                });
              }
            }

            if (documentGroupMapping && documentGroupMapping.length > 0) {
              documentGroupMapping.forEach(function (_ref5) {
                var _id = _ref5._id;

                if (_id) references.push(_id);
              });
            }

            if (issueGroup && issueGroup._id) {
              references.push(issueGroup._id);
            }

            params = { url: url, index: articleCount };
            articles = null;

            if (!(references && references.length > 0)) {
              _context.next = 24;
              break;
            }

            filters = '&& references("' + references.join('","') + '") ';


            if (ctx && url) {
              cookies = nookies.parseCookies(ctx);
              prevSlugs = cookies['prevSlugs'];

              if (!!prevSlugs) {
                if (!prevSlugs.includes(url)) nookies.setCookie(ctx, 'prevSlugs', prevSlugs + ',"' + url + '"', {});
                filters = filters + ' && !(url.current in [' + prevSlugs + '])';
              } else nookies.setCookie(ctx, 'prevSlugs', '"' + url + '"', {});
            }

            query = getQuery('related', filters, '', articleCount).replace('&& taxonomyMapping[]._ref in $taxonomy', '');
            _context.prev = 15;
            _context.next = 18;
            return client.fetch(query, params);

          case 18:
            articles = _context.sent;
            _context.next = 24;
            break;

          case 21:
            _context.prev = 21;
            _context.t0 = _context['catch'](15);

            console.error(_context.t0);

          case 24:
            return _context.abrupt('return', articles);

          case 25:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, _this, [[15, 21]]);
  }));

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

module.exports = getRelatedArticle;
