'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 _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;
