'use strict';

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

var _regenerator;

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

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var _consumer;

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

var _generalError;

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

var _ejectComponents;

function _load_ejectComponents() {
  return _ejectComponents = _interopRequireDefault(require('../../../consumer/component-ops/eject-components'));
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.default = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(ids, force) {
    var consumer, bitIds, ejectComponents, ejectResults;
    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 component ids to eject');

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

          case 4:
            consumer = _context.sent;
            bitIds = ids.map(function (id) {
              return consumer.bitMap.getExistingBitId(id);
            }); // this also assure that the ID is in .bitmap

            ejectComponents = new (_ejectComponents || _load_ejectComponents()).default(consumer, bitIds, force);
            _context.next = 9;
            return ejectComponents.eject();

          case 9:
            ejectResults = _context.sent;
            _context.next = 12;
            return consumer.onDestroy();

          case 12:
            return _context.abrupt('return', ejectResults);

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

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

  return eject;
}();