'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 React = require('react');
var slicedToArray = require('./slicedToArray-8f385299.js');
var asyncToGenerator = require('./asyncToGenerator-140e5f89.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 PubToc = function PubToc(props) {
  var toc = props.toc;

  return React__default['default'].createElement(
    'div',
    { id: 'pubToc' },
    React__default['default'].createElement(
      'ul',
      null,
      toc && toc.parentSection && Array.isArray(toc.parentSection.sections) && toc.parentSection.sections.map(function (s) {
        return React__default['default'].createElement(
          'li',
          { key: s.identifier },
          React__default['default'].createElement(
            'a',
            { href: s.identifier },
            s.name
          )
        );
      })
    )
  );
};

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

    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, res = ctx.res;
            params = {
              publication: publication
            };
            query = '*[_type == \'publication\'\n    && identifier.current == $publication] {\n      ...,\n      \'parentSection\': *[_type == \'publicationSection\'\n        && name == ^.name\n        && !defined(parent)] [0] {\n          ...,\n          \'sections\': *[_type == \'publicationSection\'\n            && references(^._id)]\n        }\n    }';
            _context.next = 5;
            return client.fetch(query, params);

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

            if (!(!toc || !toc.parentSection)) {
              _context.next = 11;
              break;
            }

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

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

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

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

module.exports = PubToc;
