'use strict';

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

var _regenerator;

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

var installPackages = exports.installPackages = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(consumer, dirs, verbose) {
    var installRootPackageJson = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
    var silentPackageManagerResult = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
    var installPeerDependencies = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
    var packageManager, packageManagerArgs, packageManagerProcessOptions, useWorkspaces, results;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            packageManager = consumer.bitJson.packageManager;
            packageManagerArgs = consumer.packageManagerArgs.length ? consumer.packageManagerArgs : consumer.bitJson.packageManagerArgs;
            packageManagerProcessOptions = consumer.bitJson.packageManagerProcessOptions;
            useWorkspaces = consumer.bitJson.useWorkspaces;


            (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_INSTALL_NPM_DEPENDENCIES);

            // don't pass the packages to npmClient.install function.
            // otherwise, it'll try to npm install the packages in one line 'npm install packageA packageB' and when
            // there are mix of public and private packages it fails with 404 error.
            // passing an empty array, results in installing packages from the package.json file
            _context.next = 7;
            return (_ || _load_()).default.install({
              modules: [],
              packageManager,
              packageManagerArgs,
              packageManagerProcessOptions,
              useWorkspaces,
              dirs,
              rootDir: consumer.getPath(),
              installRootPackageJson,
              installPeerDependencies,
              verbose
            });

          case 7:
            results = _context.sent;


            (_loader || _load_loader()).default.stop();

            if (!Array.isArray(results)) {
              results = [results];
            }
            if (!silentPackageManagerResult || verbose) {
              results.forEach(function (result) {
                if (result) (_ || _load_()).default.printResults(result);
              });
            }

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

  return function installPackages(_x4, _x5, _x6) {
    return _ref.apply(this, arguments);
  };
}();

var installNpmPackagesForComponents = exports.installNpmPackagesForComponents = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(consumer, componentsWithDependencies) {
    var verbose = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    var silentPackageManagerResult = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
    var installPeerDependencies = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
    var componentsWithDependenciesFlatten, componentDirs;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            // if dependencies are installed as bit-components, go to each one of the dependencies and install npm packages
            // otherwise, if the dependencies are installed as npm packages, npm already takes care of that
            componentsWithDependenciesFlatten = (_ramda || _load_ramda()).default.flatten(componentsWithDependencies.map(function (oneComponentWithDependencies) {
              return oneComponentWithDependencies.component.dependenciesSavedAsComponents ? [oneComponentWithDependencies.component].concat(_toConsumableArray(oneComponentWithDependencies.dependencies)) : [oneComponentWithDependencies.component];
            }));
            componentDirs = componentsWithDependenciesFlatten.map(function (component) {
              return component.writtenPath;
            });
            return _context2.abrupt('return', installPackages(consumer, componentDirs, verbose, false, silentPackageManagerResult, installPeerDependencies));

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

  return function installNpmPackagesForComponents(_x10, _x11) {
    return _ref2.apply(this, arguments);
  };
}();

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _;

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

var _loader;

function _load_loader() {
  return _loader = _interopRequireDefault(require('../cli/loader'));
}

var _loaderMessages;

function _load_loaderMessages() {
  return _loaderMessages = require('../cli/loader/loader-messages');
}

var _scope;

function _load_scope() {
  return _scope = require('../scope');
}

var _consumer;

function _load_consumer() {
  return _consumer = require('../consumer');
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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