"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../../../consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _loader() {
  const data = _interopRequireDefault(require("../../../cli/loader"));

  _loader = function () {
    return data;
  };

  return data;
}

function _componentsList() {
  const data = _interopRequireDefault(require("../../../consumer/component/components-list"));

  _componentsList = function () {
    return data;
  };

  return data;
}

function _loaderMessages() {
  const data = require("../../../cli/loader/loader-messages");

  _loaderMessages = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

function _specsRunner() {
  const data = _interopRequireDefault(require("../../../specs-runner/specs-runner"));

  _specsRunner = function () {
    return data;
  };

  return data;
}

function _generalError() {
  const data = _interopRequireDefault(require("../../../error/general-error"));

  _generalError = function () {
    return data;
  };

  return data;
}

/**
 * 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
 */
var _default =
/*#__PURE__*/
function () {
  var _test = (0, _bluebird().coroutine)(function* (id, forkLevel = _constants().TESTS_FORK_LEVEL.ONE, includeUnmodified = false, verbose) {
    _loader().default.start(_loaderMessages().BEFORE_RUNNING_SPECS);

    if (forkLevel === _constants().TESTS_FORK_LEVEL.NONE) {
      return testInProcess(id, includeUnmodified, verbose);
    }

    if (forkLevel === _constants().TESTS_FORK_LEVEL.ONE) {
      const ids = id ? [id] : undefined; // $FlowFixMe

      return (0, _specsRunner().default)({
        ids,
        forkLevel,
        includeUnmodified,
        verbose
      });
    }

    if (forkLevel === _constants().TESTS_FORK_LEVEL.COMPONENT) {
      const consumer = yield (0, _consumer().loadConsumer)();
      const components = yield _getComponentsAfterBuild(consumer, id, includeUnmodified, verbose);
      const ids = components.map(component => component.id.toString()); // $FlowFixMe

      const results = yield (0, _specsRunner().default)({
        ids,
        forkLevel,
        verbose
      });
      return results;
    }

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

  function test(_x) {
    return _test.apply(this, arguments);
  }

  return test;
}();

exports.default = _default;

const testInProcess =
/*#__PURE__*/
function () {
  var _ref = (0, _bluebird().coroutine)(function* (id, includeUnmodified = false, verbose, dontPrintEnvMsg) {
    const consumer = yield (0, _consumer().loadConsumer)();
    const components = yield _getComponentsAfterBuild(consumer, id, includeUnmodified, verbose, dontPrintEnvMsg);
    const testsResults = yield consumer.scope.testMultiple({
      components,
      consumer,
      verbose,
      dontPrintEnvMsg
    });

    _loader().default.stop();

    yield consumer.onDestroy();
    return {
      type: 'results',
      results: testsResults
    };
  });

  return function testInProcess(_x2) {
    return _ref.apply(this, arguments);
  };
}();

exports.testInProcess = testInProcess;

const _getComponentsAfterBuild =
/*#__PURE__*/
function () {
  var _ref2 = (0, _bluebird().coroutine)(function* (consumer, id, includeUnmodified = false, verbose, dontPrintEnvMsg) {
    let components;

    if (id) {
      const idParsed = consumer.getParsedId(id);
      const component = yield consumer.loadComponent(idParsed);
      components = [component];
    } else {
      const componentsList = new (_componentsList().default)(consumer);

      _loader().default.start(_loaderMessages().BEFORE_LOADING_COMPONENTS);

      if (includeUnmodified) {
        components = yield componentsList.authoredAndImportedComponents();
      } else {
        components = yield componentsList.newModifiedAndAutoTaggedComponents();
      }

      _loader().default.stop();
    }

    yield consumer.scope.buildMultiple(components, consumer, false, verbose, dontPrintEnvMsg);
    return components;
  });

  return function _getComponentsAfterBuild(_x3, _x4) {
    return _ref2.apply(this, arguments);
  };
}();