'use strict';

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

var _regenerator;

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

exports.default = list;

var _consumer;

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

var _scope;

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

var _exceptions;

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

var _loader;

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

var _loaderMessages;

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

var _remotes;

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

var _remote;

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

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

function list(_ref) {
  var _this = this;

  var scopeName = _ref.scopeName,
      showAll = _ref.showAll,
      cache = _ref.cache,
      showRemoteVersion = _ref.showRemoteVersion;

  var remoteList = function remoteList(remote) {
    (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_REMOTE_LIST);
    return remote.list();
  };
  var scopeList = function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(scope, consumer) {
      var components;
      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_LOCAL_LIST);

              if (!cache) {
                _context.next = 7;
                break;
              }

              _context.next = 4;
              return scope.list(showRemoteVersion);

            case 4:
              _context.t0 = _context.sent;
              _context.next = 10;
              break;

            case 7:
              _context.next = 9;
              return scope.listStage();

            case 9:
              _context.t0 = _context.sent;

            case 10:
              components = _context.t0;

              if (consumer) {
                components.forEach(function (component) {
                  var existingBitMapId = consumer.bitMap.getExistingComponentId(component.id.toStringWithoutVersion(), [(_constants || _load_constants()).COMPONENT_ORIGINS.AUTHORED, (_constants || _load_constants()).COMPONENT_ORIGINS.IMPORTED]);
                  if (existingBitMapId) {
                    component.currentlyUsedVersion = existingBitMapId;
                    component.componentMap = consumer.bitMap.getComponent(existingBitMapId);
                  }
                });
              }

              if (!showAll) {
                _context.next = 14;
                break;
              }

              return _context.abrupt('return', components);

            case 14:
              return _context.abrupt('return', components.filter(function (component) {
                return component.componentMap && component.componentMap.origin !== (_constants || _load_constants()).COMPONENT_ORIGINS.NESTED;
              }));

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

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

  return (0, (_consumer || _load_consumer()).loadConsumer)().then(function (consumer) {
    var scope = consumer.scope;

    if (scopeName) {
      return scope.remotes().then(function (remotes) {
        return remotes.resolve(scopeName, scope.name).then(remoteList);
      });
    }

    return scopeList(scope, consumer);
  }).catch(function (err) {
    if (!(err instanceof (_exceptions || _load_exceptions()).ConsumerNotFound)) throw err;

    if (scopeName) {
      return (_remotes || _load_remotes()).default.getScopeRemote(scopeName).then(remoteList);
    }

    return (0, (_scope || _load_scope()).loadScope)(process.cwd()).catch(function () {
      return Promise.reject(err);
    }).then(scopeList).catch(function (e) {
      return Promise.reject(e);
    });
  });
}