'use strict';

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

var _regenerator;

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

var _bluebird;

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

var getFlattenedDependencies = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(scope, component, graph, cache, notFoundDependencies, prodGraph) {
    var _this = this;

    var id, edges, dependencies, flattenDependency, flattened, flattenedUnique;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            id = component.id.toString();
            edges = getEdges(graph, id);

            if (edges) {
              _context2.next = 4;
              break;
            }

            return _context2.abrupt('return', new (_bitId || _load_bitId()).BitIds());

          case 4:
            dependencies = getEdgesWithProdGraph(prodGraph, edges);

            if (dependencies.length) {
              _context2.next = 7;
              break;
            }

            return _context2.abrupt('return', new (_bitId || _load_bitId()).BitIds());

          case 7:
            flattenDependency = function () {
              var _ref2 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(dependency) {
                var dependencyBitId, versionDependencies, scopeComponentsImporter, flattenedDependencies;
                return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        if (!cache[dependency]) {
                          _context.next = 2;
                          break;
                        }

                        return _context.abrupt('return', cache[dependency]);

                      case 2:
                        // $FlowFixMe if graph doesn't have the node, prodGraph must have it
                        dependencyBitId = graph.node(dependency) || prodGraph.node(dependency);
                        versionDependencies = void 0;
                        scopeComponentsImporter = (_scopeComponentsImporter || _load_scopeComponentsImporter()).default.getInstance(scope);

                        if (!notFoundDependencies.has(dependencyBitId)) {
                          _context.next = 7;
                          break;
                        }

                        return _context.abrupt('return', [dependencyBitId]);

                      case 7:
                        _context.prev = 7;
                        _context.next = 10;
                        return scopeComponentsImporter.importDependencies((_bitId || _load_bitId()).BitIds.fromArray([dependencyBitId]));

                      case 10:
                        versionDependencies = _context.sent;
                        _context.next = 20;
                        break;

                      case 13:
                        _context.prev = 13;
                        _context.t0 = _context['catch'](7);

                        if (!(_context.t0 instanceof (_exceptions2 || _load_exceptions2()).DependencyNotFound)) {
                          _context.next = 19;
                          break;
                        }

                        notFoundDependencies.push(dependencyBitId);
                        throwWhenDepNotIncluded(component.id, dependencyBitId);
                        return _context.abrupt('return', [dependencyBitId]);

                      case 19:
                        throw _context.t0;

                      case 20:
                        _context.next = 22;
                        return (0, (_flattenDependencies || _load_flattenDependencies()).flattenDependencyIds)(versionDependencies, scope.objects);

                      case 22:
                        flattenedDependencies = _context.sent;

                        // Store the flatten dependencies in cache
                        cache[dependency] = flattenedDependencies;
                        return _context.abrupt('return', flattenedDependencies);

                      case 25:
                      case 'end':
                        return _context.stop();
                    }
                  }
                }, _callee, _this, [[7, 13]]);
              }));

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

            _context2.next = 10;
            return (0, (_pMapSeries || _load_pMapSeries()).default)(dependencies, flattenDependency);

          case 10:
            flattened = _context2.sent;
            flattenedUnique = (_bitId || _load_bitId()).BitIds.uniqFromArray((_ramda || _load_ramda()).default.flatten(flattened));
            // when a component has cycle dependencies, the flattenedDependencies contains the component itself. remove it.

            return _context2.abrupt('return', flattenedUnique.removeIfExistWithoutVersion(component.id));

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

  return function getFlattenedDependencies(_x, _x2, _x3, _x4, _x5, _x6) {
    return _ref.apply(this, arguments);
  };
}();

var setFutureVersions = function () {
  var _ref3 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee4(componentsToTag, scope, releaseType, exactVersion) {
    var _this2 = this;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            _context4.next = 2;
            return Promise.all(componentsToTag.map(function () {
              var _ref4 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(componentToTag) {
                var modelComponent, version;
                return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
                  while (1) {
                    switch (_context3.prev = _context3.next) {
                      case 0:
                        _context3.next = 2;
                        return scope.sources.findOrAddComponent(componentToTag);

                      case 2:
                        modelComponent = _context3.sent;
                        version = modelComponent.getVersionToAdd(releaseType, exactVersion);
                        // $FlowFixMe usedVersion is needed only for this, that's why it's not declared on the instance

                        componentToTag.usedVersion = componentToTag.version;
                        componentToTag.version = version;

                      case 6:
                      case 'end':
                        return _context3.stop();
                    }
                  }
                }, _callee3, _this2);
              }));

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

          case 2:
          case 'end':
            return _context4.stop();
        }
      }
    }, _callee4, this);
  }));

  return function setFutureVersions(_x8, _x9, _x10, _x11) {
    return _ref3.apply(this, arguments);
  };
}();

/**
 * make sure the originallySharedDir was added before saving the component. also, make sure it was
 * not added twice.
 * we need three objects for this:
 * 1) component.pendingVersion => version pending to be saved in the filesystem. we want to make sure it has the added sharedDir.
 * 2) component.componentFromModel => previous version of the component. it has the original sharedDir.
 * 3) component.componentMap => current paths in the filesystem, which don't have the sharedDir.
 *
 * The component may be changed from the componentFromModel. The files may be removed and added and
 * new files may added, so we can't compare the files of componentFromModel to component.
 *
 * What we can do is calculating the sharedDir from component.componentFromModel
 * then, make sure that calculatedSharedDir + pathFromComponentMap === component.pendingVersion
 *
 * Also, make sure that the wrapDir has been removed
 */


var _ramda;

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

var _ramdaAdjunct;

function _load_ramdaAdjunct() {
  return _ramdaAdjunct = _interopRequireWildcard(require('ramda-adjunct'));
}

var _graphlib;

function _load_graphlib() {
  return _graphlib = _interopRequireDefault(require('graphlib'));
}

var _graphlib2;

function _load_graphlib2() {
  return _graphlib2 = require('graphlib');
}

var _pMapSeries;

function _load_pMapSeries() {
  return _pMapSeries = _interopRequireDefault(require('p-map-series'));
}

var _loaderMessages;

function _load_loaderMessages() {
  return _loaderMessages = require('../../cli/loader/loader-messages');
}

var _loader;

function _load_loader() {
  return _loader = _interopRequireDefault(require('../../cli/loader'));
}

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../../logger/logger'));
}

var _analytics;

function _load_analytics() {
  return _analytics = require('../../analytics/analytics');
}

var _exceptions;

function _load_exceptions() {
  return _exceptions = require('../../consumer/exceptions');
}

var _utils;

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

var _exceptions2;

function _load_exceptions2() {
  return _exceptions2 = require('../exceptions');
}

var _bitId;

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

var _flattenDependencies;

function _load_flattenDependencies() {
  return _flattenDependencies = require('../flatten-dependencies');
}

var _validationError;

function _load_validationError() {
  return _validationError = _interopRequireDefault(require('../../error/validation-error'));
}

var _constants;

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

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../../error/general-error'));
}

var _dependencies;

function _load_dependencies() {
  return _dependencies = require('../../consumer/component/dependencies');
}

var _autoTag;

function _load_autoTag() {
  return _autoTag = require('./auto-tag');
}

var _scopeComponentsImporter;

function _load_scopeComponentsImporter() {
  return _scopeComponentsImporter = _interopRequireDefault(require('./scope-components-importer'));
}

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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 buildComponentsGraph(components) {
  var setGraphEdges = function setGraphEdges(component, dependencies, graph) {
    var id = component.id.toString();
    dependencies.get().forEach(function (dependency) {
      var depId = dependency.id.toString();
      // save the full BitId of a string id to be able to retrieve it later with no confusion
      if (!graph.hasNode(id)) graph.setNode(id, component.id);
      if (!graph.hasNode(depId)) graph.setNode(depId, dependency.id);
      graph.setEdge(id, depId);
    });
  };

  var graphDeps = new (_graphlib2 || _load_graphlib2()).Graph();
  var graphDevDeps = new (_graphlib2 || _load_graphlib2()).Graph();
  var graphCompilerDeps = new (_graphlib2 || _load_graphlib2()).Graph();
  var graphTesterDeps = new (_graphlib2 || _load_graphlib2()).Graph();
  components.forEach(function (component) {
    setGraphEdges(component, component.dependencies, graphDeps);
    setGraphEdges(component, component.devDependencies, graphDevDeps);
    setGraphEdges(component, component.compilerDependencies, graphCompilerDeps);
    setGraphEdges(component, component.testerDependencies, graphTesterDeps);
  });
  return { graphDeps, graphDevDeps, graphCompilerDeps, graphTesterDeps };
}

function throwWhenDepNotIncluded(componentId, dependencyId) {
  if (!dependencyId.hasScope() && !dependencyId.hasVersion()) {
    throw new (_generalError || _load_generalError()).default(`fatal: "${componentId.toString()}" has a dependency "${dependencyId.toString()}".
this dependency was not included in the tag command.`);
  }
}

function getEdges(graph, id) {
  if (!graph.hasNode(id)) return null;
  var edges = (_graphlib || _load_graphlib()).default.alg.preorder(graph, id);
  return (_ramda || _load_ramda()).default.tail(edges); // the first item is the component itself
}

/**
 * for non-prod files, such as test files, we're interested also with its prod dependency.
 * for example, a test file foo.spec.js of component 'foo', requires bar.js from component
 * 'bar'. 'bar.js' requires 'baz.js' from component 'baz'.
 * when calculating the edges of foo.spec.js by devGraph only, we'll get bar.js but not
 * baz.js because the relationship between bar and baz are set on prodGraph only.
 * @see dev-dependencies.e2e, 'dev-dependency that requires prod-dependency' case.
 */
function getEdgesWithProdGraph(prodGraph, dependencies) {
  if (!prodGraph) return dependencies;
  // $FlowFixMe
  var prodDependencies = (_ramda || _load_ramda()).default.flatten(dependencies.map(function (dependency) {
    return getEdges(prodGraph, dependency);
  })).filter(function (x) {
    return x;
  });
  return (_ramda || _load_ramda()).default.uniq([].concat(_toConsumableArray(dependencies), _toConsumableArray(prodDependencies)));
}

function updateDependenciesVersions(componentsToTag) {
  var updateDependencyVersion = function updateDependencyVersion(dependency) {
    var foundDependency = componentsToTag.find(function (component) {
      return component.id.isEqualWithoutVersion(dependency.id);
    });
    if (foundDependency) dependency.id = dependency.id.changeVersion(foundDependency.version);
  };
  componentsToTag.forEach(function (oneComponentToTag) {
    oneComponentToTag.getAllDependencies().forEach(function (dependency) {
      return updateDependencyVersion(dependency);
    });
  });
}

function validateDirManipulation(components) {
  var throwOnError = function throwOnError(expectedPath, actualPath) {
    if (expectedPath !== actualPath) {
      throw new (_validationError || _load_validationError()).default(`failed validating the component paths with sharedDir, expected path ${expectedPath}, got ${actualPath}`);
    }
  };
  var validateComponent = function validateComponent(component) {
    if (!component.componentMap) throw new Error(`componentMap is missing from ${component.id.toString()}`);
    if (!component.componentFromModel) return;
    // component.componentFromModel.setOriginallySharedDir();
    var sharedDir = component.componentFromModel.originallySharedDir;
    var wrapDir = component.componentFromModel.wrapDir;
    var pathWithSharedDir = function pathWithSharedDir(pathStr) {
      // $FlowFixMe componentMap is set here
      if (sharedDir && component.componentMap.origin === (_constants || _load_constants()).COMPONENT_ORIGINS.IMPORTED) {
        return (0, (_utils || _load_utils()).pathJoinLinux)(sharedDir, pathStr);
      }
      return pathStr;
    };
    var pathWithoutWrapDir = function pathWithoutWrapDir(pathStr) {
      if (wrapDir) {
        return pathStr.replace(`${wrapDir}/`, '');
      }
      return pathStr;
    };
    var pathAfterDirManipulation = function pathAfterDirManipulation(pathStr) {
      var withoutWrapDir = pathWithoutWrapDir(pathStr);
      return pathWithSharedDir(withoutWrapDir);
    };
    var expectedMainFile = pathAfterDirManipulation(component.componentMap.mainFile);
    throwOnError(expectedMainFile, component.pendingVersion.mainFile);
    // $FlowFixMe componentMap is set here
    var componentMapFiles = component.componentMap.getAllFilesPaths();
    var componentFiles = component.pendingVersion.files.map(function (file) {
      return file.relativePath;
    });
    componentMapFiles.forEach(function (file) {
      var expectedFile = pathAfterDirManipulation(file);
      if (!componentFiles.includes(expectedFile)) {
        throw new (_validationError || _load_validationError()).default(`failed validating the component paths, expected a file ${expectedFile} to be in ${componentFiles.toString()} array`);
      }
    });
  };
  components.forEach(function (component) {
    return validateComponent(component);
  });
}

exports.default = function () {
  var _ref5 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee7(_ref6) {
    var _this3 = this;

    var consumerComponents = _ref6.consumerComponents,
        scope = _ref6.scope,
        message = _ref6.message,
        exactVersion = _ref6.exactVersion,
        releaseType = _ref6.releaseType,
        force = _ref6.force,
        consumer = _ref6.consumer,
        _ref6$ignoreNewestVer = _ref6.ignoreNewestVersion,
        ignoreNewestVersion = _ref6$ignoreNewestVer === undefined ? false : _ref6$ignoreNewestVer,
        _ref6$skipTests = _ref6.skipTests,
        skipTests = _ref6$skipTests === undefined ? false : _ref6$skipTests,
        _ref6$verbose = _ref6.verbose,
        verbose = _ref6$verbose === undefined ? false : _ref6$verbose;

    var consumerComponentsIdsMap, componentsToTag, componentsToTagIds, componentsToTagIdsLatest, autoTagCandidates, autoTagComponents, autoTagComponentsLoaded, autoTagConsumerComponents, componentsToBuildAndTest, newestVersionsP, newestVersions, newestVersionsWithoutEmpty, testsResults, testsResultsP, _buildComponentsGraph, graphDeps, graphDevDeps, graphCompilerDeps, graphTesterDeps, dependenciesCache, notFoundDependencies, persistComponent, taggedComponents, autoTaggedResults;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_IMPORT_PUT_ON_SCOPE);
            consumerComponentsIdsMap = {};
            // Concat and unique all the dependencies from all the components so we will not import
            // the same dependency more then once, it's mainly for performance purpose

            consumerComponents.forEach(function (consumerComponent) {
              var componentIdString = consumerComponent.id.toString();
              // Store it in a map so we can take it easily from the sorted array which contain only the id
              consumerComponentsIdsMap[componentIdString] = consumerComponent;
            });
            componentsToTag = (_ramda || _load_ramda()).default.values(consumerComponentsIdsMap); // consumerComponents unique

            componentsToTagIds = componentsToTag.map(function (c) {
              return c.id;
            });
            _context7.next = 7;
            return scope.latestVersions(componentsToTagIds, false);

          case 7:
            componentsToTagIdsLatest = _context7.sent;
            autoTagCandidates = consumer.potentialComponentsForAutoTagging(componentsToTagIdsLatest);
            // $FlowFixMe unclear error

            _context7.next = 11;
            return (0, (_autoTag || _load_autoTag()).getAutoTagPending)(scope, autoTagCandidates, componentsToTagIdsLatest);

          case 11:
            autoTagComponents = _context7.sent;
            _context7.next = 14;
            return consumer.loadComponents(autoTagComponents.map(function (c) {
              return c.toBitId();
            }));

          case 14:
            autoTagComponentsLoaded = _context7.sent;
            autoTagConsumerComponents = autoTagComponentsLoaded.components;
            componentsToBuildAndTest = componentsToTag.concat(autoTagConsumerComponents);

            // check for each one of the components whether it is using an old version

            if (ignoreNewestVersion) {
              _context7.next = 25;
              break;
            }

            newestVersionsP = componentsToBuildAndTest.map(function () {
              var _ref7 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee5(component) {
                var modelComponent, latest;
                return (_regenerator || _load_regenerator()).default.wrap(function _callee5$(_context5) {
                  while (1) {
                    switch (_context5.prev = _context5.next) {
                      case 0:
                        if (!component.componentFromModel) {
                          _context5.next = 9;
                          break;
                        }

                        _context5.next = 3;
                        return scope.getModelComponentIfExist(component.id);

                      case 3:
                        modelComponent = _context5.sent;

                        if (modelComponent) {
                          _context5.next = 6;
                          break;
                        }

                        throw new (_generalError || _load_generalError()).default(`component ${component.id} was not found in the model`);

                      case 6:
                        latest = modelComponent.latest();

                        if (!(latest !== component.version)) {
                          _context5.next = 9;
                          break;
                        }

                        return _context5.abrupt('return', {
                          componentId: component.id.toStringWithoutVersion(),
                          currentVersion: component.version,
                          latestVersion: latest
                        });

                      case 9:
                        return _context5.abrupt('return', null);

                      case 10:
                      case 'end':
                        return _context5.stop();
                    }
                  }
                }, _callee5, _this3);
              }));

              return function (_x14) {
                return _ref7.apply(this, arguments);
              };
            }());
            _context7.next = 21;
            return Promise.all(newestVersionsP);

          case 21:
            newestVersions = _context7.sent;
            newestVersionsWithoutEmpty = newestVersions.filter(function (newest) {
              return newest;
            });

            if ((_ramdaAdjunct || _load_ramdaAdjunct()).isNilOrEmpty(newestVersionsWithoutEmpty)) {
              _context7.next = 25;
              break;
            }

            throw new (_exceptions || _load_exceptions()).NewerVersionFound(newestVersionsWithoutEmpty);

          case 25:

            (_logger || _load_logger()).default.debug('scope.putMany: sequentially build all components');
            (_analytics || _load_analytics()).Analytics.addBreadCrumb('scope.putMany', 'scope.putMany: sequentially build all components');
            _context7.next = 29;
            return scope.buildMultiple(componentsToBuildAndTest, consumer, false, verbose);

          case 29:

            (_logger || _load_logger()).default.debug('scope.putMany: sequentially test all components');
            testsResults = [];

            if (skipTests) {
              _context7.next = 45;
              break;
            }

            testsResultsP = scope.testMultiple({
              components: componentsToBuildAndTest,
              consumer,
              verbose,
              rejectOnFailure: !force
            });
            _context7.prev = 33;
            _context7.next = 36;
            return testsResultsP;

          case 36:
            testsResults = _context7.sent;
            _context7.next = 45;
            break;

          case 39:
            _context7.prev = 39;
            _context7.t0 = _context7['catch'](33);

            if (force) {
              _context7.next = 45;
              break;
            }

            if (verbose) {
              _context7.next = 44;
              break;
            }

            throw new (_exceptions || _load_exceptions()).ComponentSpecsFailed();

          case 44:
            throw _context7.t0;

          case 45:

            (_logger || _load_logger()).default.debug('scope.putMany: sequentially persist all components');
            (_analytics || _load_analytics()).Analytics.addBreadCrumb('scope.putMany', 'scope.putMany: sequentially persist all components');

            // go through all components and find the future versions for them
            _context7.next = 49;
            return setFutureVersions(componentsToTag, scope, releaseType, exactVersion);

          case 49:
            // go through all dependencies and update their versions
            updateDependenciesVersions(componentsToTag);
            // build the dependencies graph
            _buildComponentsGraph = buildComponentsGraph(componentsToTag), graphDeps = _buildComponentsGraph.graphDeps, graphDevDeps = _buildComponentsGraph.graphDevDeps, graphCompilerDeps = _buildComponentsGraph.graphCompilerDeps, graphTesterDeps = _buildComponentsGraph.graphTesterDeps;
            dependenciesCache = {};
            notFoundDependencies = new (_bitId || _load_bitId()).BitIds();

            persistComponent = function () {
              var _ref8 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee6(consumerComponent) {
                var testResult, flattenedDependencies, flattenedDevDependencies, flattenedCompilerDependencies, flattenedTesterDependencies;
                return (_regenerator || _load_regenerator()).default.wrap(function _callee6$(_context6) {
                  while (1) {
                    switch (_context6.prev = _context6.next) {
                      case 0:
                        testResult = void 0;

                        if (!skipTests) {
                          testResult = testsResults.find(function (result) {
                            return consumerComponent.id.isEqualWithoutScopeAndVersion(result.componentId);
                          });
                        }
                        _context6.next = 4;
                        return getFlattenedDependencies(scope, consumerComponent, graphDeps, dependenciesCache, notFoundDependencies);

                      case 4:
                        flattenedDependencies = _context6.sent;
                        _context6.next = 7;
                        return getFlattenedDependencies(scope, consumerComponent, graphDevDeps, dependenciesCache, notFoundDependencies, graphDeps);

                      case 7:
                        flattenedDevDependencies = _context6.sent;
                        _context6.next = 10;
                        return getFlattenedDependencies(scope, consumerComponent, graphCompilerDeps, dependenciesCache, notFoundDependencies, graphDeps);

                      case 10:
                        flattenedCompilerDependencies = _context6.sent;
                        _context6.next = 13;
                        return getFlattenedDependencies(scope, consumerComponent, graphTesterDeps, dependenciesCache, notFoundDependencies, graphDeps);

                      case 13:
                        flattenedTesterDependencies = _context6.sent;
                        _context6.next = 16;
                        return scope.sources.addSource({
                          source: consumerComponent,
                          consumer,
                          flattenedDependencies,
                          flattenedDevDependencies,
                          flattenedCompilerDependencies,
                          flattenedTesterDependencies,
                          message,
                          exactVersion,
                          releaseType,
                          specsResults: testResult ? testResult.specs : undefined
                        });

                      case 16:
                        return _context6.abrupt('return', consumerComponent);

                      case 17:
                      case 'end':
                        return _context6.stop();
                    }
                  }
                }, _callee6, _this3);
              }));

              return function persistComponent(_x15) {
                return _ref8.apply(this, arguments);
              };
            }();

            // Run the persistence one by one not in parallel!


            (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_PERSISTING_PUT_ON_SCOPE);

            _context7.next = 57;
            return (0, (_pMapSeries || _load_pMapSeries()).default)(componentsToTag, function (consumerComponent) {
              return persistComponent(consumerComponent);
            });

          case 57:
            taggedComponents = _context7.sent;
            _context7.next = 60;
            return (0, (_autoTag || _load_autoTag()).bumpDependenciesVersions)(scope, autoTagCandidates, taggedComponents);

          case 60:
            autoTaggedResults = _context7.sent;

            validateDirManipulation(taggedComponents);
            _context7.next = 64;
            return scope.objects.persist();

          case 64:
            return _context7.abrupt('return', { taggedComponents, autoTaggedResults });

          case 65:
          case 'end':
            return _context7.stop();
        }
      }
    }, _callee7, this, [[33, 39]]);
  }));

  function tagModelComponent(_x13) {
    return _ref5.apply(this, arguments);
  }

  return tagModelComponent;
}();