'use strict';

var asyncToGenerator = require('./asyncToGenerator-58c289d9.js');
require('./_commonjsHelpers-06173234.js');
require('./es6.string.iterator-c2573ffd.js');
require('./_to-object-6de10e57.js');
require('./web.dom.iterable-46657b5c.js');
require('./_library-dd23b178.js');
require('./core.get-iterator-method-f474eacc.js');
require('./_iter-detect-55a5657a.js');

var _this = undefined;

var getRootDocGroup = function () {
  var _ref = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee(id, client) {
    var query, result, checkPropExist;
    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            query = "*[_type == 'documentGroup' && _id == '" + id + "'][0]";
            _context.next = 3;
            return client.fetch(query);

          case 3:
            result = _context.sent;
            checkPropExist = result && result.parent && (result.parent._id ? result.parent._id : result.parent._ref);

            if (!(result && result.parent)) {
              _context.next = 11;
              break;
            }

            _context.next = 8;
            return getRootDocGroup(checkPropExist, client);

          case 8:
            return _context.abrupt("return", _context.sent);

          case 11:
            if (!(result && result.identifier && result.identifier.current)) {
              _context.next = 15;
              break;
            }

            return _context.abrupt("return", result.identifier.current);

          case 15:
            return _context.abrupt("return");

          case 16:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, _this);
  }));

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

module.exports = getRootDocGroup;
