'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _scope;

function _load_scope() {
  return _scope = require('../../../scope');
}

var _bitId;

function _load_bitId() {
  return _bitId = require('../../../bit-id');
}

var _version2;

function _load_version() {
  return _version2 = _interopRequireDefault(require('../../../scope/models/version'));
}

var _constants;

function _load_constants() {
  return _constants = require('../../../constants');
}

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../../../error/general-error'));
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(id) {
    var scope, bitId, component, version, _version;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return (0, (_scope || _load_scope()).loadScope)();

          case 2:
            scope = _context.sent;
            _context.next = 5;
            return scope.getParsedId(id);

          case 5:
            bitId = _context.sent;
            _context.next = 8;
            return scope.sources.get(bitId);

          case 8:
            component = _context.sent;

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

            throw new (_generalError || _load_generalError()).default('component was not found');

          case 11:
            if (!bitId.hasVersion()) {
              _context.next = 16;
              break;
            }

            _context.next = 14;
            return component.loadVersion(bitId.version, scope.objects);

          case 14:
            version = _context.sent;
            return _context.abrupt('return', version.toObject());

          case 16:
            if (!(bitId.version === (_constants || _load_constants()).LATEST_BIT_VERSION && id.includes((_constants || _load_constants()).VERSION_DELIMITER))) {
              _context.next = 21;
              break;
            }

            _context.next = 19;
            return component.loadVersion(component.latest(), scope.objects);

          case 19:
            _version = _context.sent;
            return _context.abrupt('return', _version.toObject());

          case 21:
            return _context.abrupt('return', component.toObject());

          case 22:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

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

  return catComponent;
}();