'use strict';

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

var _regenerator;

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

var _scope2;

function _load_scope() {
  return _scope2 = 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'));
}

var _consumer;

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

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 _callee6(_ref) {
    var _this = this;

    var id = _ref.id,
        allVersions = _ref.allVersions,
        scopePath = _ref.scopePath;
    var bitId, remoteShow, getConsumer, getScope, showComponentUsingConsumer, showComponentUsingScope, consumer, scope, remote;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            bitId = (_bitId || _load_bitId()).BitId.parse(id, true); // user used --remote so we know it has a scope

            remoteShow = function () {
              var _ref3 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(remote) {
                return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_REMOTE_SHOW);
                        return _context.abrupt('return', remote.show(bitId));

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

              return function remoteShow(_x2) {
                return _ref3.apply(this, arguments);
              };
            }();

            getConsumer = function () {
              var _ref4 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2() {
                var consumer;
                return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
                  while (1) {
                    switch (_context2.prev = _context2.next) {
                      case 0:
                        _context2.prev = 0;
                        _context2.next = 3;
                        return (0, (_consumer || _load_consumer()).loadConsumer)();

                      case 3:
                        consumer = _context2.sent;
                        return _context2.abrupt('return', consumer);

                      case 7:
                        _context2.prev = 7;
                        _context2.t0 = _context2['catch'](0);
                        return _context2.abrupt('return', null);

                      case 10:
                      case 'end':
                        return _context2.stop();
                    }
                  }
                }, _callee2, _this, [[0, 7]]);
              }));

              return function getConsumer() {
                return _ref4.apply(this, arguments);
              };
            }();

            getScope = function () {
              var _ref5 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3() {
                var _scope;

                return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
                  while (1) {
                    switch (_context3.prev = _context3.next) {
                      case 0:
                        _context3.prev = 0;
                        _context3.next = 3;
                        return (0, (_scope2 || _load_scope()).loadScope)(scopePath || process.cwd());

                      case 3:
                        _scope = _context3.sent;
                        return _context3.abrupt('return', _scope);

                      case 7:
                        _context3.prev = 7;
                        _context3.t0 = _context3['catch'](0);

                        if (!(_context3.t0 instanceof (_exceptions || _load_exceptions()).ScopeNotFound)) {
                          _context3.next = 11;
                          break;
                        }

                        return _context3.abrupt('return', null);

                      case 11:
                        throw _context3.t0;

                      case 12:
                      case 'end':
                        return _context3.stop();
                    }
                  }
                }, _callee3, _this, [[0, 7]]);
              }));

              return function getScope() {
                return _ref5.apply(this, arguments);
              };
            }();

            showComponentUsingConsumer = function () {
              var _ref6 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee4(consumer) {
                var remotes, remote;
                return (_regenerator || _load_regenerator()).default.wrap(function _callee4$(_context4) {
                  while (1) {
                    switch (_context4.prev = _context4.next) {
                      case 0:
                        if (!allVersions) {
                          _context4.next = 2;
                          break;
                        }

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

                      case 2:
                        _context4.next = 4;
                        return consumer.scope.remotes();

                      case 4:
                        remotes = _context4.sent;
                        _context4.next = 7;
                        return remotes.resolve(bitId.scope, consumer.scope);

                      case 7:
                        remote = _context4.sent;
                        return _context4.abrupt('return', remoteShow(remote));

                      case 9:
                      case 'end':
                        return _context4.stop();
                    }
                  }
                }, _callee4, _this);
              }));

              return function showComponentUsingConsumer(_x3) {
                return _ref6.apply(this, arguments);
              };
            }();

            showComponentUsingScope = function () {
              var _ref7 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee5(scope) {
                return (_regenerator || _load_regenerator()).default.wrap(function _callee5$(_context5) {
                  while (1) {
                    switch (_context5.prev = _context5.next) {
                      case 0:
                        if (!allVersions) {
                          _context5.next = 2;
                          break;
                        }

                        return _context5.abrupt('return', scope.loadAllVersions(bitId));

                      case 2:
                        return _context5.abrupt('return', scope.loadRemoteComponent(bitId));

                      case 3:
                      case 'end':
                        return _context5.stop();
                    }
                  }
                }, _callee5, _this);
              }));

              return function showComponentUsingScope(_x4) {
                return _ref7.apply(this, arguments);
              };
            }();

            if (scopePath) {
              _context6.next = 12;
              break;
            }

            _context6.next = 9;
            return getConsumer();

          case 9:
            consumer = _context6.sent;

            if (!consumer) {
              _context6.next = 12;
              break;
            }

            return _context6.abrupt('return', showComponentUsingConsumer(consumer));

          case 12:
            _context6.next = 14;
            return getScope();

          case 14:
            scope = _context6.sent;

            if (!scope) {
              _context6.next = 17;
              break;
            }

            return _context6.abrupt('return', showComponentUsingScope(scope));

          case 17:
            _context6.next = 19;
            return (_remotes || _load_remotes()).default.getScopeRemote(bitId.scope);

          case 19:
            remote = _context6.sent;
            return _context6.abrupt('return', remoteShow(remote));

          case 21:
          case 'end':
            return _context6.stop();
        }
      }
    }, _callee6, this);
  }));

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

  return getScopeComponent;
}();