'use strict';

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

var _utils;

function _load_utils() {
  return _utils = require('../utils');
}

var _versionDependencies;

function _load_versionDependencies() {
  return _versionDependencies = _interopRequireDefault(require('./version-dependencies'));
}

var _repository;

function _load_repository() {
  return _repository = _interopRequireDefault(require('./objects/repository'));
}

var _bitId;

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

var _componentDependencies;

function _load_componentDependencies() {
  return _componentDependencies = _interopRequireDefault(require('./component-dependencies'));
}

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

function flattenDependencies(dependencies) {
  return (0, (_utils || _load_utils()).values)((0, (_utils || _load_utils()).flatten)(dependencies.map(function (dep) {
    return dep.allDependencies.concat(dep.component);
  })).reduce(function (components, component) {
    components[component.id.toString()] = component;
    return components;
  }, {}));
}
function flattenDependencyIds(dependencies, repo) {
  return Promise.all(dependencies.map(function (dep) {
    var depCompId = dep.component.id;
    depCompId.scope = dep.sourceScope;
    return dep.component.flattenedDependencies(repo).then(function (flattnedDeps) {
      return flattnedDeps.concat(depCompId);
    });
  })).then(function (idMatrix) {
    var ids = (0, (_utils || _load_utils()).flatten)(idMatrix);
    return (0, (_utils || _load_utils()).values)(ids.reduce(function (components, id) {
      components[id.toString()] = id;
      return components;
    }, {}));
  });
}