'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 _loader;

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

var _loaderMessages;

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

var _bitId;

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

var _hasWildcard;

function _load_hasWildcard() {
  return _hasWildcard = _interopRequireDefault(require('../../../utils/string/has-wildcard'));
}

var _componentsList;

function _load_componentsList() {
  return _componentsList = _interopRequireDefault(require('../../../consumer/component/components-list'));
}

var _noIdMatchWildcard;

function _load_noIdMatchWildcard() {
  return _noIdMatchWildcard = _interopRequireDefault(require('./exceptions/no-id-match-wildcard'));
}

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(_ref) {
    var ids = _ref.ids,
        force = _ref.force,
        remote = _ref.remote,
        track = _ref.track,
        deleteFiles = _ref.deleteFiles;
    var consumer, bitIds, removeResults;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_REMOVE);
            _context.next = 3;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 3:
            consumer = _context.sent;
            bitIds = getBitIdsToRemove(consumer, ids, remote);
            _context.next = 7;
            return consumer.remove({ ids: (_bitId || _load_bitId()).BitIds.fromArray(bitIds), force, remote, track, deleteFiles });

          case 7:
            removeResults = _context.sent;
            _context.next = 10;
            return consumer.onDestroy();

          case 10:
            return _context.abrupt('return', removeResults);

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

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

  return remove;
}();

function getBitIdsToRemove(consumer, ids, remote) {
  if ((0, (_hasWildcard || _load_hasWildcard()).default)(ids)) {
    var allIds = consumer.bitMap.getAllBitIds();
    var bitIds = (_componentsList || _load_componentsList()).default.filterComponentsByWildcard(allIds, ids);
    if (!bitIds.length) throw new (_noIdMatchWildcard || _load_noIdMatchWildcard()).default(ids);
    return bitIds;
  }
  return ids.map(function (id) {
    return remote ? (_bitId || _load_bitId()).BitId.parse(id, true) : consumer.getParsedId(id);
  });
}