"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 _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = 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;
}

function _removeComponents() {
  const data = _interopRequireDefault(require("../../../consumer/component-ops/remove-components"));

  _removeComponents = function () {
    return data;
  };

  return data;
}

function _listScope() {
  const data = require("./list-scope");

  _listScope = 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 = remote ? yield (0, _consumer().loadConsumerIfExist)() : yield (0, _consumer().loadConsumer)();
    const bitIds = remote ? yield getRemoteBitIdsToRemove(ids) : yield getLocalBitIdsToRemove(consumer, ids);
    const removeResults = yield (0, _removeComponents().default)({
      consumer,
      ids: _bitId().BitIds.fromArray(bitIds),
      force,
      remote,
      track,
      deleteFiles
    });
    if (consumer) yield consumer.onDestroy();
    return removeResults;
  });

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

  return remove;
}();

exports.default = _default;

function getLocalBitIdsToRemove(_x2, _x3) {
  return _getLocalBitIdsToRemove.apply(this, arguments);
}

function _getLocalBitIdsToRemove() {
  _getLocalBitIdsToRemove = (0, _bluebird().coroutine)(function* (consumer, ids) {
    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 => consumer.getParsedId(id));
  });
  return _getLocalBitIdsToRemove.apply(this, arguments);
}

function getRemoteBitIdsToRemove(_x4) {
  return _getRemoteBitIdsToRemove.apply(this, arguments);
}

function _getRemoteBitIdsToRemove() {
  _getRemoteBitIdsToRemove = (0, _bluebird().coroutine)(function* (ids) {
    if ((0, _hasWildcard().default)(ids)) {
      return getIdsFromRemoteByWildcards(ids);
    }

    return ids.map(id => _bitId().BitId.parse(id, true));
  });
  return _getRemoteBitIdsToRemove.apply(this, arguments);
}

function getIdsFromRemoteByWildcards(_x5) {
  return _getIdsFromRemoteByWildcards.apply(this, arguments);
}

function _getIdsFromRemoteByWildcards() {
  _getIdsFromRemoteByWildcards = (0, _bluebird().coroutine)(function* (ids) {
    const remoteIds = yield Promise.all(ids.map(id => {
      if ((0, _hasWildcard().default)(id)) {
        return (0, _listScope().getRemoteBitIdsByWildcards)(id);
      }

      return _bitId().BitId.parse(id, true);
    }));

    _loader().default.start(_loaderMessages().BEFORE_REMOVE);

    return _ramda().default.flatten(remoteIds);
  });
  return _getIdsFromRemoteByWildcards.apply(this, arguments);
}