"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 _mergeVersion() {
  const data = require("../../../consumer/versions-ops/merge-version");

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

  _bitId = function () {
    return data;
  };

  return data;
}

var _default =
/*#__PURE__*/
function () {
  var _merge = (0, _bluebird().coroutine)(function* (version, ids, mergeStrategy) {
    const consumer = yield (0, _consumer().loadConsumer)();
    const bitIds = getComponentsToMerge(consumer, ids);
    const mergeResults = yield (0, _mergeVersion().mergeVersion)(consumer, version, bitIds, mergeStrategy);
    yield consumer.onDestroy();
    return mergeResults;
  });

  function merge(_x, _x2, _x3) {
    return _merge.apply(this, arguments);
  }

  return merge;
}();

exports.default = _default;

function getComponentsToMerge(consumer, ids) {
  if ((0, _hasWildcard().default)(ids)) {
    const componentsList = new (_componentsList().default)(consumer);
    return componentsList.listComponentsByIdsWithWildcard(ids);
  }

  return ids.map(id => consumer.getParsedId(id));
}