"use strict";

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

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

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

  _bluebird = function () {
    return data;
  };

  return data;
}

function _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = function () {
    return data;
  };

  return data;
}

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

  _consumer = function () {
    return data;
  };

  return data;
}

function _component() {
  const data = _interopRequireDefault(require("../../../consumer/component"));

  _component = function () {
    return data;
  };

  return data;
}

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

  _constants = function () {
    return data;
  };

  return data;
}

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

  _loader = function () {
    return data;
  };

  return data;
}

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

  _loaderMessages = function () {
    return data;
  };

  return data;
}

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

  _generalError = function () {
    return data;
  };

  return data;
}

function build(_x, _x2, _x3) {
  return _build.apply(this, arguments);
}

function _build() {
  _build = (0, _bluebird().coroutine)(function* (id, noCache, verbose) {
    const consumer = yield (0, _consumer().loadConsumer)();
    const bitId = consumer.getParsedId(id);
    const component = yield consumer.loadComponent(bitId);
    const result = yield component.build({
      scope: consumer.scope,
      noCache,
      consumer,
      verbose
    });
    if (result === null) return null;
    const distFilePaths = yield component.dists.writeDists(component, consumer);
    yield consumer.onDestroy();
    return distFilePaths;
  });
  return _build.apply(this, arguments);
}

function buildAll(_x4, _x5) {
  return _buildAll.apply(this, arguments);
}

function _buildAll() {
  _buildAll = (0, _bluebird().coroutine)(function* (noCache, verbose) {
    const consumer = yield (0, _consumer().loadConsumer)();
    const authoredAndImportedIds = consumer.bitMap.getAllBitIds([_constants().COMPONENT_ORIGINS.IMPORTED, _constants().COMPONENT_ORIGINS.AUTHORED]);
    if (_ramda().default.isEmpty(authoredAndImportedIds)) throw new (_generalError().default)('nothing to build');

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

    const {
      components
    } = yield consumer.loadComponents(authoredAndImportedIds);

    _loader().default.stop();

    const allComponents = yield consumer.scope.buildMultiple(components, consumer, noCache, verbose);
    const componentsObj = {};
    allComponents.forEach(component => {
      componentsObj[component.component] = component.buildResults;
    });
    yield consumer.onDestroy();
    return componentsObj;
  });
  return _buildAll.apply(this, arguments);
}