'use strict';

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

var _regenerator;

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

exports.default = getScopeComponent;

var _consumer;

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

var _scope;

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

var _bitId;

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

var _loader;

function _load_loader() {
  return _loader = _interopRequireDefault(require('../../../cli/loader'));
}

var _loaderMessages;

function _load_loaderMessages() {
  return _loaderMessages = require('../../../cli/loader/loader-messages');
}

var _exceptions;

function _load_exceptions() {
  return _exceptions = require('../../../scope/exceptions');
}

var _remotes;

function _load_remotes() {
  return _remotes = _interopRequireDefault(require('../../../remotes/remotes'));
}

var _remote;

function _load_remote() {
  return _remote = _interopRequireDefault(require('../../../remotes/remote'));
}

var _component;

function _load_component() {
  return _component = _interopRequireDefault(require('../../../consumer/component'));
}

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"); }); }; }

function getScopeComponent(_ref) {
  var _this = this;

  var id = _ref.id,
      allVersions = _ref.allVersions,
      scopePath = _ref.scopePath,
      showRemoteVersions = _ref.showRemoteVersions;

  function loadFromScope() {
    return (0, (_scope || _load_scope()).loadScope)(scopePath || process.cwd()).then(function (scope) {
      var bitId = (_bitId || _load_bitId()).BitId.parse(id, true); // user used --remote so we know it has a scope
      if (allVersions) {
        return scope.loadAllVersions(bitId);
      }
      return scope.loadRemoteComponent(bitId);
    });
  }

  var remoteShow = function remoteShow(remote, bitId) {
    (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_REMOTE_SHOW);
    return remote.show(bitId);
  };

  if (scopePath) {
    return loadFromScope();
  }

  return (0, (_consumer || _load_consumer()).loadConsumer)().then(function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(consumer) {
      var localScopeName, bitId, component;
      return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              localScopeName = consumer.scope.name;
              bitId = consumer.getParsedId(id);

              if (bitId.isLocal(localScopeName)) {
                _context.next = 12;
                break;
              }

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

              return _context.abrupt('return', Promise.reject(new Error('cant list all versions of a remote scope')));

            case 5:
              _context.next = 7;
              return consumer.scope.remotes().then(function (remotes) {
                return remotes.resolve(bitId.scope, consumer.scope).then(function (remote) {
                  return remoteShow(remote, bitId);
                });
              });

            case 7:
              component = _context.sent;

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

              _context.next = 11;
              return consumer.addRemoteAndLocalVersionsToDependencies(component, false);

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

            case 12:
              if (!allVersions) {
                _context.next = 14;
                break;
              }

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

            case 14:
              return _context.abrupt('return', consumer.scope.loadComponent(bitId));

            case 15:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, _this);
    }));

    return function (_x) {
      return _ref2.apply(this, arguments);
    };
  }()).catch(function () {
    // TODO - handle relevant error error
    return loadFromScope();
  }).catch(function (err) {
    if (err instanceof (_exceptions || _load_exceptions()).ScopeNotFound) {
      var bitId = (_bitId || _load_bitId()).BitId.parse(id, true);
      // $FlowFixMe the scope must be there
      return (_remotes || _load_remotes()).default.getScopeRemote(bitId.scope).then(function (remote) {
        return remoteShow(remote, bitId);
      }).catch(function (e) {
        return Promise.reject(e);
      });
    }
    throw err;
  });
}