'use strict';

var asyncToGenerator = require('./asyncToGenerator-65df6bf8.js');
var slicedToArray = require('./slicedToArray-067dd3bc.js');
var _extends = require('./extends-7c86182f.js');
var React = require('react');
var QueueDeckExpanded = require('./QueueDeckExpanded.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('./_object-pie-083f2dd6.js');
require('./toConsumableArray-d7797c2b.js');
require('react-bootstrap');
require('./urlFor.js');
require('./index-d48b231c.js');
require('./index.es-1ab8dda7.js');
require('./moment-aca40de1.js');
require('./visibility-sensor-56020df6.js');
require('react-dom');
require('./index-7435b964.js');
require('prop-types');
require('next/router');
require('./index-65621b6c.js');
require('./main-ae8472f3.js');
require('./entities-df1aed86.js');
require('./ADInfeed-9aae73be.js');
require('react-bootstrap/Card');
require('./AdSlot.js');
require('./debounce-d4a2369b.js');
require('./isSymbol-03abbdf0.js');
require('./toNumber-5e1fac58.js');
require('./Beam.js');
require('./stringify-f7a37a8d.js');
require('./Segment.js');
require('./AuthorComponent-7d00f6d5.js');
require('./lodash-f0783643.js');
require('./ADlgInfeed-0e40c762.js');
require('./getContentCategory-f38a4c00.js');
require('./timeDifferenceCalc.js');

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

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

var _this = undefined;

var PubSection = function PubSection(props) {
  var pubSection = props.pubSection,
      tocUrl = props.tocUrl;

  return React__default["default"].createElement(
    'div',
    { id: 'pubSection' },
    pubSection.publication && pubSection.publication.name && React__default["default"].createElement(
      'h2',
      null,
      pubSection.publication.name
    ),
    React__default["default"].createElement(
      'p',
      null,
      React__default["default"].createElement(
        'a',
        { id: 'back', href: tocUrl ? tocUrl : 'toc' },
        'Back to Table of Contents'
      )
    ),
    React__default["default"].createElement(QueueDeckExpanded, _extends._extends({ dataRecord: pubSection.articles }, props))
  );
};

PubSection.getInitialProps = function () {
  var _ref = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee(ctx, client) {
    var _ctx$query, publication, issue, section, res, params, query, _ref2, _ref3, pubSection;

    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _ctx$query = ctx.query, publication = _ctx$query.publication, issue = _ctx$query.issue, section = _ctx$query.section, res = ctx.res;
            params = {
              publication: publication,
              section: section
            };
            query = '*[_type == \'publicationSection\'\n    && defined(parent)\n    && parent->identifier == $publication\n    && identifier == $section] {\n      ...,\n      parent->,\n      \'articles\': *[_type == \'article\'\n      \t&& !(_id in path("drafts.**")) \n\t\t    && defined(title) \n\t\t    && defined(url) \n\t\t    && is_visible \n\t\t    && defined(published) \n        && dateTime(published) <= dateTime(now())\n        && passwordLock != true\n        && references(^._id)] {\n          ...,\n          contentCategory->,\n          authorMapping[]->,\n        },\n      \'publication\': *[_type == \'publication\'\n        && identifier.current == $publication] [0]\n    }';
            _context.next = 5;
            return client.fetch(query, params);

          case 5:
            _ref2 = _context.sent;
            _ref3 = slicedToArray._slicedToArray(_ref2, 1);
            pubSection = _ref3[0];

            if (pubSection) {
              _context.next = 11;
              break;
            }

            res.statusCode = 404;
            return _context.abrupt('return', { show404: true });

          case 11:
            return _context.abrupt('return', { publication: publication, issue: issue, section: section, pubSection: pubSection });

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

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

module.exports = PubSection;
