'use strict';

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

var _regenerator;

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

var _consumer;

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

var _generalError;

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

var _attachEnvs;

function _load_attachEnvs() {
  return _attachEnvs = _interopRequireDefault(require('../../../consumer/component-ops/attach-envs'));
}

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

exports.default = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(ids, _ref) {
    var compiler = _ref.compiler,
        tester = _ref.tester;
    var consumer, bitIds, attachResults;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!(!ids || !ids.length)) {
              _context.next = 2;
              break;
            }

            throw new (_generalError || _load_generalError()).default('please specify at least one component id');

          case 2:
            if (!(!compiler && !tester)) {
              _context.next = 4;
              break;
            }

            throw new (_generalError || _load_generalError()).default('please specify at least one env type (--compiler / --tester)');

          case 4:
            _context.next = 6;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 6:
            consumer = _context.sent;
            bitIds = ids.map(function (id) {
              return consumer.getParsedId(id);
            });
            attachResults = (0, (_attachEnvs || _load_attachEnvs()).default)(consumer, bitIds, { compiler, tester });
            _context.next = 11;
            return consumer.onDestroy();

          case 11:
            return _context.abrupt('return', attachResults);

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

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

  return attachEnvs;
}();