"use strict";

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

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

function _utils() {
  const data = require("../utils");

  _utils = function () {
    return data;
  };

  return data;
}

function _versionDependencies() {
  const data = _interopRequireDefault(require("./version-dependencies"));

  _versionDependencies = function () {
    return data;
  };

  return data;
}

function _repository() {
  const data = _interopRequireDefault(require("./objects/repository"));

  _repository = function () {
    return data;
  };

  return data;
}

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

  _bitId = function () {
    return data;
  };

  return data;
}

function _componentDependencies() {
  const data = _interopRequireDefault(require("./component-dependencies"));

  _componentDependencies = function () {
    return data;
  };

  return data;
}

function flattenDependencies(dependencies) {
  return (0, _utils().values)((0, _utils().flatten)(dependencies.map(dep => dep.allDependencies.concat(dep.component))).reduce((components, component) => {
    components[component.id.toString()] = component;
    return components;
  }, {}));
}

function flattenDependencyIds(dependencies, repo) {
  return Promise.all(dependencies.map(dep => {
    // $FlowFixMe
    const depCompId = dep.component.id.changeScope(dep.sourceScope);
    return dep.component.flattenedDependencies(repo).then(flattenedDeps => flattenedDeps.concat(depCompId));
  })).then(idMatrix => {
    const ids = (0, _utils().flatten)(idMatrix);
    return (0, _utils().values)(ids.reduce((components, id) => {
      components[id.toString()] = id;
      return components;
    }, {}));
  });
}