'use strict';

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

var _regenerator;

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

exports.default = testInScope;

var _consumer;

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

var _scope2;

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

var _exceptions;

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

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../../../logger/logger'));
}

var _specsResults;

function _load_specsResults() {
  return _specsResults = _interopRequireDefault(require('../../../consumer/specs-results'));
}

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 testInScope(_ref) {
  var loadFromScope = function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(initialError) {
      var _this = this;

      var getScope, scope, bitId;
      return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              getScope = function () {
                var _ref3 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee() {
                  var _scope;

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

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

                        case 7:
                          _context.prev = 7;
                          _context.t0 = _context['catch'](0);
                          throw new Error(initialError || _context.t0);

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

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

              _context2.next = 3;
              return getScope();

            case 3:
              scope = _context2.sent;
              _context2.next = 6;
              return scope.getParsedId(id);

            case 6:
              bitId = _context2.sent;
              return _context2.abrupt('return', scope.runComponentSpecs({
                bitId,
                save,
                verbose,
                isolated: true,
                directory,
                keep
              }));

            case 8:
            case 'end':
              return _context2.stop();
          }
        }
      }, _callee2, this);
    }));

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

  var id = _ref.id,
      save = _ref.save,
      verbose = _ref.verbose,
      scopePath = _ref.scopePath,
      directory = _ref.directory,
      keep = _ref.keep;

  (_logger || _load_logger()).default.debug(`testInScope, id: ${id}, scopePath: ${scopePath}`);


  function loadFromConsumer() {
    return (0, (_consumer || _load_consumer()).loadConsumer)().then(function (consumer) {
      var bitId = consumer.getParsedId(id);
      return consumer.scope.runComponentSpecs({
        consumer,
        bitId,
        save,
        verbose,
        isolated: true
      });
    });
  }

  if (scopePath) return loadFromScope();

  return loadFromConsumer().catch(function (err) {
    if (!(err instanceof (_exceptions || _load_exceptions()).ConsumerNotFound)) throw err;
    return loadFromScope(err);
  });
}