"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

  _bluebird = function () {
    return data;
  };

  return data;
}

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

  _consumer = 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 _bitId() {
  const data = require("../../../bit-id");

  _bitId = function () {
    return data;
  };

  return data;
}

function _hasWildcard() {
  const data = _interopRequireDefault(require("../../../utils/string/has-wildcard"));

  _hasWildcard = function () {
    return data;
  };

  return data;
}

function _componentsList() {
  const data = _interopRequireDefault(require("../../../consumer/component/components-list"));

  _componentsList = function () {
    return data;
  };

  return data;
}

function _noIdMatchWildcard() {
  const data = _interopRequireDefault(require("./exceptions/no-id-match-wildcard"));

  _noIdMatchWildcard = function () {
    return data;
  };

  return data;
}

var _default =
/*#__PURE__*/
function () {
  var _remove = (0, _bluebird().coroutine)(function* ({
    ids,
    force,
    remote,
    track,
    deleteFiles
  }) {
    _loader().default.start(_loaderMessages().BEFORE_REMOVE);

    const consumer = yield (0, _consumer().loadConsumer)();
    const bitIds = getBitIdsToRemove(consumer, ids, remote);
    const removeResults = yield consumer.remove({
      ids: _bitId().BitIds.fromArray(bitIds),
      force,
      remote,
      track,
      deleteFiles
    });
    yield consumer.onDestroy();
    return removeResults;
  });

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

  return remove;
}();

exports.default = _default;

function getBitIdsToRemove(consumer, ids, remote) {
  if ((0, _hasWildcard().default)(ids)) {
    const allIds = consumer.bitMap.getAllBitIds();

    const bitIds = _componentsList().default.filterComponentsByWildcard(allIds, ids);

    if (!bitIds.length) throw new (_noIdMatchWildcard().default)(ids);
    return bitIds;
  }

  return ids.map(id => {
    return remote ? _bitId().BitId.parse(id, true) : consumer.getParsedId(id);
  });
}