'use strict';

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

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

/**
 * 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
 */
var install = exports.install = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(consumer, verbose) {
    var candidateComponents, dirs, consumerPath, dirsAbsolute;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            candidateComponents = consumer.bitMap.getAllComponents([(_constants || _load_constants()).COMPONENT_ORIGINS.IMPORTED, (_constants || _load_constants()).COMPONENT_ORIGINS.NESTED]);
            dirs = Object.keys(candidateComponents).map(function (id) {
              return candidateComponents[id].rootDir;
            }).filter(function (dir) {
              return dir;
            });
            consumerPath = consumer.getPath();
            dirsAbsolute = dirs.map(function (dir) {
              return (_path || _load_path()).default.join(consumerPath, dir);
            });
            _context.next = 6;
            return (0, (_installPackages || _load_installPackages()).installPackages)(consumer, dirsAbsolute, verbose, true);

          case 6:
            return _context.abrupt('return', (0, (_links || _load_links()).linkAllToNodeModules)(consumer));

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

  return function install(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

/**
 * does the following (the order is important):
 * 1) install npm packages of the provided ids.
 * 2) link the provided ids.
 */


var installIds = exports.installIds = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(consumer, ids, verbose) {
    var _ref3, components, dirs;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return consumer.loadComponents(ids);

          case 2:
            _ref3 = _context2.sent;
            components = _ref3.components;
            dirs = components.map(function (component) {
              return component.componentMap.rootDir;
            }).filter(function (dir) {
              return dir;
            });

            if (!dirs.length) {
              _context2.next = 8;
              break;
            }

            _context2.next = 8;
            return (0, (_installPackages || _load_installPackages()).installPackages)(consumer, dirs, verbose);

          case 8:
            (_analytics || _load_analytics()).Analytics.setExtraData('num_components', components.length);
            return _context2.abrupt('return', (0, (_links || _load_links()).linkComponentsToNodeModules)(components, consumer));

          case 10:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, this);
  }));

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

var _path;

function _load_path() {
  return _path = _interopRequireDefault(require('path'));
}

var _links;

function _load_links() {
  return _links = require('../../links');
}

var _installPackages;

function _load_installPackages() {
  return _installPackages = require('../../npm-client/install-packages');
}

var _constants;

function _load_constants() {
  return _constants = require('../../constants');
}

var _;

function _load_() {
  return _ = require('..');
}

var _bitId;

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

var _analytics;

function _load_analytics() {
  return _analytics = require('../../analytics/analytics');
}

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"); }); }; }