'use strict';

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

var _regenerator;

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

var _consumer;

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

var _loader;

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

var _componentsList;

function _load_componentsList() {
  return _componentsList = _interopRequireDefault(require('../../../consumer/component/components-list'));
}

var _loaderMessages;

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

var _constants;

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

var _specsRunner;

function _load_specsRunner() {
  return _specsRunner = _interopRequireDefault(require('../../../specs-runner/specs-runner'));
}

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../../../error/general-error'));
}

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

/**
 * Run tests for all modified components or for specific component
 * @param {string} id
 * @param {'NONE' | 'ONE' | 'COMPONENT'} forkLevel - run the tests in the current process
 * or in child process, or in child process for each component
 * @param {boolean} verbose
 */
exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(id) {
    var forkLevel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (_constants || _load_constants()).TESTS_FORK_LEVEL.ONE;
    var includeUnmodified = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    var verbose = arguments[3];

    var ids, consumer, components, _ids, results;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!(forkLevel === (_constants || _load_constants()).TESTS_FORK_LEVEL.NONE)) {
              _context.next = 2;
              break;
            }

            return _context.abrupt('return', testInProcess(id, includeUnmodified, verbose));

          case 2:
            if (!(forkLevel === (_constants || _load_constants()).TESTS_FORK_LEVEL.ONE)) {
              _context.next = 5;
              break;
            }

            ids = id ? [id] : undefined;
            // $FlowFixMe

            return _context.abrupt('return', (0, (_specsRunner || _load_specsRunner()).default)({ ids, forkLevel, includeUnmodified, verbose }));

          case 5:
            if (!(forkLevel === (_constants || _load_constants()).TESTS_FORK_LEVEL.COMPONENT)) {
              _context.next = 17;
              break;
            }

            _context.next = 8;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 8:
            consumer = _context.sent;
            _context.next = 11;
            return _getComponents(consumer, id, includeUnmodified, verbose);

          case 11:
            components = _context.sent;
            _ids = components.map(function (component) {
              return component.id.toString();
            });
            // $FlowFixMe

            _context.next = 15;
            return (0, (_specsRunner || _load_specsRunner()).default)({ ids: _ids, forkLevel, verbose });

          case 15:
            results = _context.sent;
            return _context.abrupt('return', results);

          case 17:
            throw new (_generalError || _load_generalError()).default('unknown fork level, fork level must be one of: NONE, ONE, COMPONENT');

          case 18:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  function test(_x3) {
    return _ref.apply(this, arguments);
  }

  return test;
}();

var testInProcess = exports.testInProcess = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(id) {
    var includeUnmodified = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    var verbose = arguments[2];
    var consumer, components, testsResults;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 2:
            consumer = _context2.sent;
            _context2.next = 5;
            return _getComponents(consumer, id, includeUnmodified, verbose);

          case 5:
            components = _context2.sent;
            _context2.next = 8;
            return consumer.scope.testMultiple({ components, consumer, verbose });

          case 8:
            testsResults = _context2.sent;

            (_loader || _load_loader()).default.stop();
            _context2.next = 12;
            return consumer.onDestroy();

          case 12:
            return _context2.abrupt('return', testsResults);

          case 13:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, undefined);
  }));

  return function testInProcess(_x5) {
    return _ref2.apply(this, arguments);
  };
}();

var _getComponents = function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(consumer, id) {
    var includeUnmodified = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    var verbose = arguments[3];
    var idParsed, component, componentsList, components;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            if (!id) {
              _context3.next = 6;
              break;
            }

            idParsed = consumer.getParsedId(id);
            _context3.next = 4;
            return consumer.loadComponent(idParsed);

          case 4:
            component = _context3.sent;
            return _context3.abrupt('return', [component]);

          case 6:
            componentsList = new (_componentsList || _load_componentsList()).default(consumer);

            (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_LOADING_COMPONENTS);
            components = void 0;

            if (!includeUnmodified) {
              _context3.next = 15;
              break;
            }

            _context3.next = 12;
            return componentsList.authoredAndImportedComponents();

          case 12:
            components = _context3.sent;
            _context3.next = 18;
            break;

          case 15:
            _context3.next = 17;
            return componentsList.newModifiedAndAutoTaggedComponents();

          case 17:
            components = _context3.sent;

          case 18:
            (_loader || _load_loader()).default.stop();
            _context3.next = 21;
            return consumer.scope.buildMultiple(components, consumer, false, verbose);

          case 21:
            return _context3.abrupt('return', components);

          case 22:
          case 'end':
            return _context3.stop();
        }
      }
    }, _callee3, undefined);
  }));

  return function _getComponents(_x7, _x8) {
    return _ref3.apply(this, arguments);
  };
}();