'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.commitAllAction = exports.commitAction = undefined;

var _regenerator;

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

var commitAction = exports.commitAction = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(args) {
    var id, message, exactVersion, releaseType, force, verbose, ignoreUnresolvedDependencies, ignoreNewestVersion, skipTests, validExactVersion, consumer, componentsList, newComponents, bitId, componentStatus, commitResults;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            id = args.id, message = args.message, exactVersion = args.exactVersion, releaseType = args.releaseType, force = args.force, verbose = args.verbose, ignoreUnresolvedDependencies = args.ignoreUnresolvedDependencies, ignoreNewestVersion = args.ignoreNewestVersion, skipTests = args.skipTests;
            validExactVersion = _validateVersion(exactVersion);

            HooksManagerInstance.triggerHook((_constants || _load_constants()).PRE_TAG_HOOK, args);
            _context.next = 5;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 5:
            consumer = _context.sent;
            componentsList = new (_componentsList || _load_componentsList()).default(consumer);
            _context.next = 9;
            return componentsList.listNewComponents();

          case 9:
            newComponents = _context.sent;
            bitId = consumer.getParsedId(id);

            if (force) {
              _context.next = 17;
              break;
            }

            _context.next = 14;
            return consumer.getComponentStatusById(bitId);

          case 14:
            componentStatus = _context.sent;

            if (!(componentStatus.modified === false)) {
              _context.next = 17;
              break;
            }

            return _context.abrupt('return', null);

          case 17:
            _context.next = 19;
            return consumer.tag(new (_bitId || _load_bitId()).BitIds(bitId), message, validExactVersion, releaseType, force, verbose, ignoreUnresolvedDependencies, ignoreNewestVersion, skipTests);

          case 19:
            commitResults = _context.sent;

            commitResults.newComponents = newComponents;
            HooksManagerInstance.triggerHook((_constants || _load_constants()).POST_TAG_HOOK, commitResults);
            _context.next = 24;
            return consumer.onDestroy();

          case 24:
            return _context.abrupt('return', commitResults);

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

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

var getCommitPendingComponents = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(consumer, isAllScope, exactVersion, includeImported) {
    var componentsList, commitPendingComponents, warnings;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            componentsList = new (_componentsList || _load_componentsList()).default(consumer);

            if (!isAllScope) {
              _context2.next = 3;
              break;
            }

            return _context2.abrupt('return', componentsList.listCommitPendingOfAllScope(exactVersion, includeImported));

          case 3:
            _context2.next = 5;
            return componentsList.listCommitPendingComponents();

          case 5:
            commitPendingComponents = _context2.sent;
            warnings = [];
            return _context2.abrupt('return', { commitPendingComponents, warnings });

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

  return function getCommitPendingComponents(_x2, _x3, _x4, _x5) {
    return _ref2.apply(this, arguments);
  };
}();

var commitAllAction = exports.commitAllAction = function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(args) {
    var message, exactVersion, releaseType, force, verbose, ignoreUnresolvedDependencies, ignoreNewestVersion, skipTests, scope, includeImported, validExactVersion, consumer, componentsList, newComponents, _ref4, commitPendingComponents, warnings, commitResults;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            message = args.message, exactVersion = args.exactVersion, releaseType = args.releaseType, force = args.force, verbose = args.verbose, ignoreUnresolvedDependencies = args.ignoreUnresolvedDependencies, ignoreNewestVersion = args.ignoreNewestVersion, skipTests = args.skipTests, scope = args.scope, includeImported = args.includeImported;
            validExactVersion = _validateVersion(exactVersion);

            HooksManagerInstance.triggerHook((_constants || _load_constants()).PRE_TAG_ALL_HOOK, args);
            _context3.next = 5;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 5:
            consumer = _context3.sent;
            componentsList = new (_componentsList || _load_componentsList()).default(consumer);
            _context3.next = 9;
            return componentsList.listNewComponents();

          case 9:
            newComponents = _context3.sent;
            _context3.next = 12;
            return getCommitPendingComponents(consumer, scope, exactVersion, includeImported);

          case 12:
            _ref4 = _context3.sent;
            commitPendingComponents = _ref4.commitPendingComponents;
            warnings = _ref4.warnings;

            if (!(_ramda || _load_ramda()).default.isEmpty(commitPendingComponents)) {
              _context3.next = 17;
              break;
            }

            return _context3.abrupt('return', null);

          case 17:
            _context3.next = 19;
            return consumer.tag(commitPendingComponents, message, validExactVersion, releaseType, force, verbose, ignoreUnresolvedDependencies, ignoreNewestVersion, skipTests);

          case 19:
            commitResults = _context3.sent;

            commitResults.warnings = warnings;

            commitResults.newComponents = newComponents;
            HooksManagerInstance.triggerHook((_constants || _load_constants()).POST_TAG_ALL_HOOK, commitResults);
            (_analytics || _load_analytics()).Analytics.setExtraData('num_components', (_ramda || _load_ramda()).default.concat(commitResults.taggedComponents, commitResults.autoTaggedComponents, commitResults.newComponents).length);
            _context3.next = 26;
            return consumer.onDestroy();

          case 26:
            return _context3.abrupt('return', commitResults);

          case 27:
          case 'end':
            return _context3.stop();
        }
      }
    }, _callee3, this);
  }));

  return function commitAllAction(_x6) {
    return _ref3.apply(this, arguments);
  };
}();

var _semver;

function _load_semver() {
  return _semver = _interopRequireDefault(require('semver'));
}

var _ramda;

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

var _consumer;

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

var _componentsList;

function _load_componentsList() {
  return _componentsList = _interopRequireDefault(require('../../../consumer/component/components-list'));
}

var _bitId;

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

var _hooks;

function _load_hooks() {
  return _hooks = _interopRequireDefault(require('../../../hooks'));
}

var _constants;

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

var _invalidVersion;

function _load_invalidVersion() {
  return _invalidVersion = _interopRequireDefault(require('./exceptions/invalid-version'));
}

var _analytics;

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

var _component;

function _load_component() {
  return _component = _interopRequireDefault(require('../../../consumer/component'));
}

var _component2;

function _load_component2() {
  return _component2 = _interopRequireDefault(require('../../../scope/models/component'));
}

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

var HooksManagerInstance = (_hooks || _load_hooks()).default.getInstance();

function _validateVersion(version) {
  if (version) {
    var validVersion = (_semver || _load_semver()).default.valid(version);
    if (!validVersion) throw new (_invalidVersion || _load_invalidVersion()).default(version);
    return validVersion;
  }
  return null;
}