'use strict';

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

var _regenerator;

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

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _ramda;

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

var _componentDependencies;

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

var _componentVersion;

function _load_componentVersion() {
  return _componentVersion = _interopRequireDefault(require('./component-version'));
}

var _componentObjects;

function _load_componentObjects() {
  return _componentObjects = _interopRequireDefault(require('./component-objects'));
}

var _repository;

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

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var VersionDependencies = function () {
  function VersionDependencies(component, dependencies, devDependencies, sourceScope) {
    _classCallCheck(this, VersionDependencies);

    this.component = component;
    this.dependencies = dependencies;
    this.devDependencies = devDependencies;
    this.allDependencies = this.dependencies.concat(this.devDependencies);
    this.sourceScope = sourceScope;
  }

  _createClass(VersionDependencies, [{
    key: 'toConsumer',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(repo) {
        var dependenciesP, devDependenciesP, componentP, _ref2, _ref3, component, dependencies, devDependencies;

        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                dependenciesP = Promise.all(this.dependencies.map(function (dep) {
                  return dep.toConsumer(repo);
                }));
                devDependenciesP = Promise.all(this.devDependencies.map(function (dep) {
                  return dep.toConsumer(repo);
                }));
                componentP = this.component.toConsumer(repo);
                _context.next = 5;
                return Promise.all([componentP, dependenciesP, devDependenciesP]);

              case 5:
                _ref2 = _context.sent;
                _ref3 = _slicedToArray(_ref2, 3);
                component = _ref3[0];
                dependencies = _ref3[1];
                devDependencies = _ref3[2];
                return _context.abrupt('return', new (_componentDependencies || _load_componentDependencies()).default({
                  component,
                  dependencies,
                  devDependencies
                }));

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

      function toConsumer(_x) {
        return _ref.apply(this, arguments);
      }

      return toConsumer;
    }()
  }, {
    key: 'toObjects',
    value: function toObjects(repo) {
      var depsP = Promise.all(this.allDependencies.map(function (dep) {
        return dep.toObjects(repo);
      }));
      var compP = this.component.toObjects(repo);

      return Promise.all([compP, depsP]).then(function (_ref4) {
        var _ref5 = _slicedToArray(_ref4, 2),
            component = _ref5[0],
            dependencies = _ref5[1];

        var flattened = dependencies.reduce(function (array, compObjects) {
          array.push.apply(array, _toConsumableArray(compObjects.objects.concat([compObjects.component])));
          return array;
        }, []);

        return new (_componentObjects || _load_componentObjects()).default(component.component, flattened.concat(component.objects));
      });
    }
  }]);

  return VersionDependencies;
}();

exports.default = VersionDependencies;