'use strict';

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

var _regenerator;

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

var _scope;

function _load_scope() {
  return _scope = require('../../../scope');
}

var _consumer;

function _load_consumer() {
  return _consumer = require('../../../consumer');
}

var _logger;

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

var _migrationHelper;

function _load_migrationHelper() {
  return _migrationHelper = require('../../../migration/migration-helper');
}

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

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

/**
 * Running migration process for consumer and / or scope - to update the stores (bitObjects, bit.map.json) to the current version
 *
 * @export
 * @param {string} scopePath - A path to scope directory, it a path was provided the migration won't run on consumer
 * @param {boolean} verbose - print debug logs
 * @returns {Promise<MigrationResult>} - wether the process run and wether it successeded
 */
exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(scopePath, verbose) {
    var scope, consumer;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            (_logger || _load_logger()).default.debug('starting migration process');
            if (verbose) console.log('starting migration process'); // eslint-disable-line no-console
            scope = void 0;
            // If a scope path provided we will run the migrate only for the scope

            if (!scopePath) {
              _context.next = 10;
              break;
            }

            (_logger || _load_logger()).default.debug(`running migration process for scope in path ${scopePath}`);
            if (verbose) console.log(`running migration process for scope in path ${scopePath}`); // eslint-disable-line no-console
            _context.next = 8;
            return (0, (_scope || _load_scope()).loadScope)(scopePath);

          case 8:
            scope = _context.sent;
            return _context.abrupt('return', scope.migrate(verbose));

          case 10:
            _context.next = 12;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 12:
            consumer = _context.sent;

            scope = consumer.scope;
            _context.next = 16;
            return consumer.migrate(verbose);

          case 16:
            // const consumerMigrationResult = await consumer.migrate(verbose);
            // if (!consumerMigrationResult)
            (_logger || _load_logger()).default.debug('running migration process for scope in consumer');
            if (verbose) console.log('running migration process for scope in consumer'); // eslint-disable-line no-console
            return _context.abrupt('return', scope.migrate(verbose));

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

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

  return migrate;
}();