'use strict';

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

var _regenerator;

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

var build = exports.build = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(id, verbose) {
    var bitId, consumer, component, result, distFilePaths;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            bitId = (_bitId || _load_bitId()).BitId.parse(id);
            _context.next = 3;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 3:
            consumer = _context.sent;
            _context.next = 6;
            return consumer.loadComponent(bitId);

          case 6:
            component = _context.sent;
            _context.next = 9;
            return component.build({ scope: consumer.scope, consumer, verbose });

          case 9:
            result = _context.sent;

            if (!(result === null)) {
              _context.next = 12;
              break;
            }

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

          case 12:
            _context.next = 14;
            return component.dists.writeDists(component, consumer);

          case 14:
            distFilePaths = _context.sent;

            consumer.bitMap.addMainDistFileToComponent(component.id, distFilePaths);
            _context.next = 18;
            return consumer.onDestroy();

          case 18:
            return _context.abrupt('return', distFilePaths);

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

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

var buildAll = exports.buildAll = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(verbose) {
    var consumer, authoredAndImported, authoredAndImportedIds, _ref3, components, buildAllP, allComponents, componentsObj;

    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;
            authoredAndImported = consumer.bitMap.getAllComponents([(_constants || _load_constants()).COMPONENT_ORIGINS.IMPORTED, (_constants || _load_constants()).COMPONENT_ORIGINS.AUTHORED]);
            // eslint-disable-next-line prefer-promise-reject-errors

            if (!(_ramda || _load_ramda()).default.isEmpty(authoredAndImported)) {
              _context2.next = 6;
              break;
            }

            return _context2.abrupt('return', Promise.reject('nothing to build'));

          case 6:
            authoredAndImportedIds = Object.keys(authoredAndImported).map(function (id) {
              return (_bitId || _load_bitId()).BitId.parse(id);
            });

            (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_LOADING_COMPONENTS);
            _context2.next = 10;
            return consumer.loadComponents(authoredAndImportedIds);

          case 10:
            _ref3 = _context2.sent;
            components = _ref3.components;
            _context2.next = 14;
            return consumer.scope.buildMultiple(components, consumer, verbose);

          case 14:
            buildAllP = _context2.sent;
            _context2.next = 17;
            return Promise.all(buildAllP);

          case 17:
            allComponents = _context2.sent;
            componentsObj = {};

            allComponents.forEach(function (component) {
              componentsObj[component.component] = component.buildResults;
            });
            _context2.next = 22;
            return consumer.onDestroy();

          case 22:
            return _context2.abrupt('return', componentsObj);

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

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

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _consumer;

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

var _component;

function _load_component() {
  return _component = _interopRequireDefault(require('../../../consumer/component'));
}

var _bitId;

function _load_bitId() {
  return _bitId = require('../../../bit-id');
}

var _constants;

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

var _loader;

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

var _loaderMessages;

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

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