'use strict';

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

var _regenerator;

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

var _consumer;

function _load_consumer() {
  return _consumer = require('../../../consumer');
}

var _nothingToCompareTo;

function _load_nothingToCompareTo() {
  return _nothingToCompareTo = _interopRequireDefault(require('./exceptions/nothing-to-compare-to'));
}

var _constants;

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

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 _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(_ref) {
    var id = _ref.id,
        compare = _ref.compare,
        allVersions = _ref.allVersions,
        showRemoteVersions = _ref.showRemoteVersions;
    var consumer, bitId, component, componentModel;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 2:
            consumer = _context.sent;
            bitId = consumer.getParsedId(id);

            if (!allVersions) {
              _context.next = 6;
              break;
            }

            return _context.abrupt('return', consumer.scope.loadAllVersions(bitId));

          case 6:
            _context.next = 8;
            return consumer.loadComponent(bitId);

          case 8:
            component = _context.sent;

            if (!showRemoteVersions) {
              _context.next = 12;
              break;
            }

            _context.next = 12;
            return consumer.addRemoteAndLocalVersionsToDependencies(component, true);

          case 12:
            if (!compare) {
              _context.next = 24;
              break;
            }

            _context.prev = 13;
            _context.next = 16;
            return consumer.scope.loadRemoteComponent(component.id);

          case 16:
            componentModel = _context.sent;

            if (component.componentMap.origin === (_constants || _load_constants()).COMPONENT_ORIGINS.IMPORTED) {
              componentModel.stripOriginallySharedDir(consumer.bitMap);
            }
            return _context.abrupt('return', { component, componentModel });

          case 21:
            _context.prev = 21;
            _context.t0 = _context['catch'](13);
            throw new (_nothingToCompareTo || _load_nothingToCompareTo()).default(id);

          case 24:
            _context.next = 26;
            return consumer.onDestroy();

          case 26:
            return _context.abrupt('return', { component });

          case 27:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this, [[13, 21]]);
  }));

  function getConsumerBit(_x) {
    return _ref2.apply(this, arguments);
  }

  return getConsumerBit;
}();