"use strict";

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

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

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

  _bluebird = function () {
    return data;
  };

  return data;
}

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _links() {
  const data = require("../../links");

  _links = function () {
    return data;
  };

  return data;
}

function _installPackages() {
  const data = require("../../npm-client/install-packages");

  _installPackages = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

function _() {
  const data = require("..");

  _ = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = require("../../bit-id");

  _bitId = function () {
    return data;
  };

  return data;
}

function _analytics() {
  const data = require("../../analytics/analytics");

  _analytics = function () {
    return data;
  };

  return data;
}

/**
 * does the following (the order is important):
 * 1) install npm packages of the consumer root.
 * 2) install npm packages of all imported and nested components
 * 3) link all components
 */
function install(_x, _x2) {
  return _install.apply(this, arguments);
}
/**
 * does the following (the order is important):
 * 1) install npm packages of the provided ids.
 * 2) link the provided ids.
 */


function _install() {
  _install = (0, _bluebird().coroutine)(function* (consumer, verbose) {
    const candidateComponents = consumer.bitMap.getAllComponents([_constants().COMPONENT_ORIGINS.IMPORTED, _constants().COMPONENT_ORIGINS.NESTED]);
    const dirs = Object.keys(candidateComponents).map(id => candidateComponents[id].rootDir).filter(dir => dir);
    const consumerPath = consumer.getPath();
    const dirsAbsolute = dirs.map(dir => _path().default.join(consumerPath, dir));
    yield (0, _installPackages().installPackages)(consumer, dirsAbsolute, verbose, true);
    return (0, _links().linkAllToNodeModules)(consumer);
  });
  return _install.apply(this, arguments);
}

function installIds(_x3, _x4, _x5) {
  return _installIds.apply(this, arguments);
}

function _installIds() {
  _installIds = (0, _bluebird().coroutine)(function* (consumer, ids, verbose) {
    const {
      components
    } = yield consumer.loadComponents(ids);
    const dirs = components.map(component => component.componentMap.rootDir).filter(dir => dir);
    if (dirs.length) yield (0, _installPackages().installPackages)(consumer, dirs, verbose);

    _analytics().Analytics.setExtraData('num_components', components.length);

    const nodeModuleLinker = new (_links().NodeModuleLinker)(components, consumer, consumer.bitMap);
    return nodeModuleLinker.link();
  });
  return _installIds.apply(this, arguments);
}