"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

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

  _componentDependencies = function () {
    return data;
  };

  return data;
}

function _componentVersion() {
  const data = _interopRequireDefault(require("./component-version"));

  _componentVersion = function () {
    return data;
  };

  return data;
}

function _componentObjects() {
  const data = _interopRequireDefault(require("./component-objects"));

  _componentObjects = function () {
    return data;
  };

  return data;
}

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

  _repository = function () {
    return data;
  };

  return data;
}

class VersionDependencies {
  constructor(component, dependencies, devDependencies, compilerDependencies, testerDependencies, sourceScope) {
    this.component = component;
    this.dependencies = dependencies;
    this.devDependencies = devDependencies;
    this.compilerDependencies = compilerDependencies;
    this.testerDependencies = testerDependencies;
    this.allDependencies = [...this.dependencies, ...this.devDependencies, ...this.compilerDependencies, ...this.testerDependencies];
    this.sourceScope = sourceScope;
  }

  toConsumer(repo, manipulateDirData) {
    var _this = this;

    return (0, _bluebird().coroutine)(function* () {
      const depToConsumer = dep => dep.toConsumer(repo, manipulateDirData);

      const dependenciesP = Promise.all(_this.dependencies.map(depToConsumer));
      const devDependenciesP = Promise.all(_this.devDependencies.map(depToConsumer));
      const compilerDependenciesP = Promise.all(_this.compilerDependencies.map(depToConsumer));
      const testerDependenciesP = Promise.all(_this.testerDependencies.map(depToConsumer));

      const componentP = _this.component.toConsumer(repo, manipulateDirData);

      const [component, dependencies, devDependencies, compilerDependencies, testerDependencies] = yield Promise.all([componentP, dependenciesP, devDependenciesP, compilerDependenciesP, testerDependenciesP]);
      return new (_componentDependencies().default)({
        component,
        dependencies,
        devDependencies,
        compilerDependencies,
        testerDependencies
      });
    })();
  }

  toObjects(repo, clientVersion) {
    const depsP = Promise.all(this.allDependencies.map(dep => dep.toObjects(repo, clientVersion)));
    const compP = this.component.toObjects(repo, clientVersion);
    return Promise.all([compP, depsP]).then(([component, dependencies]) => {
      const flattened = dependencies.reduce((array, compObjects) => {
        array.push(...compObjects.objects.concat([compObjects.component]));
        return array;
      }, []);
      return new (_componentObjects().default)(component.component, flattened.concat(component.objects));
    });
  }

}

exports.default = VersionDependencies;