'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 _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var installPackages = exports.installPackages = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__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 dirsWithPkgJson, packageManager, packageManagerArgs, packageManagerProcessOptions, useWorkspaces, results;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return filterDirsWithoutPackageJson(dirs);

          case 2:
            dirsWithPkgJson = _context.sent;
            packageManager = consumer.config.packageManager;
            packageManagerArgs = consumer.packageManagerArgs.length ? consumer.packageManagerArgs : consumer.config.packageManagerArgs;
            packageManagerProcessOptions = consumer.config.packageManagerProcessOptions;
            useWorkspaces = consumer.config.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 = 10;
            return (_ || _load_()).default.install({
              modules: [],
              packageManager,
              packageManagerArgs,
              packageManagerProcessOptions,
              useWorkspaces,
              dirs: dirsWithPkgJson,
              rootDir: consumer.getPath(),
              installRootPackageJson,
              installPeerDependencies,
              verbose
            });

          case 10:
            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 14:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

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

var installNpmPackagesForComponents = exports.installNpmPackagesForComponents = function () {
  var _ref2 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(_ref3) {
    var consumer = _ref3.consumer,
        basePath = _ref3.basePath,
        componentsWithDependencies = _ref3.componentsWithDependencies,
        _ref3$verbose = _ref3.verbose,
        verbose = _ref3$verbose === undefined ? false : _ref3$verbose,
        _ref3$silentPackageMa = _ref3.silentPackageManagerResult,
        silentPackageManagerResult = _ref3$silentPackageMa === undefined ? false : _ref3$silentPackageMa,
        _ref3$installPeerDepe = _ref3.installPeerDependencies,
        installPeerDependencies = _ref3$installPeerDepe === undefined ? false : _ref3$installPeerDepe;
    var componentsWithDependenciesFlatten, componentDirsRelative, componentDirsRelativeUniq, 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];
            }));
            componentDirsRelative = componentsWithDependenciesFlatten.map(function (component) {
              return component.writtenPath;
            });
            componentDirsRelativeUniq = (_ramda || _load_ramda()).default.uniq(componentDirsRelative);
            componentDirs = componentDirsRelativeUniq.map(function (dir) {
              return basePath ? (_path || _load_path()).default.join(basePath, dir) : dir;
            });
            return _context2.abrupt('return', installPackages(consumer, componentDirs, verbose, false, silentPackageManagerResult, installPeerDependencies));

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

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

var filterDirsWithoutPackageJson = function () {
  var _ref4 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(dirs) {
    return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt('return', (0, (_filterAsync || _load_filterAsync()).default)(dirs, function (dir) {
              return (_fsExtra || _load_fsExtra()).default.exists((_path || _load_path()).default.join(dir, (_constants || _load_constants()).PACKAGE_JSON));
            }));

          case 1:
          case 'end':
            return _context3.stop();
        }
      }
    }, _callee3, this);
  }));

  return function filterDirsWithoutPackageJson(_x8) {
    return _ref4.apply(this, arguments);
  };
}();

var _ramda;

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

var _fsExtra;

function _load_fsExtra() {
  return _fsExtra = _interopRequireDefault(require('fs-extra'));
}

var _path;

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

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 _filterAsync;

function _load_filterAsync() {
  return _filterAsync = _interopRequireDefault(require('../utils/array/filter-async'));
}

var _constants;

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

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