import { _ as _asyncToGenerator, r as regenerator } from './asyncToGenerator-958ef5fc.js';
import { _ as _slicedToArray } from './slicedToArray-6ebcbfea.js';
import React__default from 'react';
import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-ea258bb1.js';
import './web.dom.iterable-ab4dea5c.js';
import './_library-528f1934.js';
import './_iter-detect-98db3b07.js';

var _this = undefined;

var PubToc = function PubToc(props) {
  var toc = props.toc;

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

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

    return 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(_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);
  };
}();

export { PubToc as default };
