'use strict';

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

var _regenerator;

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

var _consumer;

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

var _bitId;

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

var _untagComponent;

function _load_untagComponent() {
  return _untagComponent = require('../../../scope/component-ops/untag-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"); }); }; }

/**
 * in case the untagged version is the current version in bitmap, update to the latest version
 * or, remove the version completely in case all versions were untagged
 */
function updateBitMap(consumer, untagResults) {
  untagResults.forEach(function (result) {
    var id = result.id,
        versions = result.versions,
        component = result.component;

    var currentId = consumer.bitMap.getBitId(id, { ignoreVersion: true });
    if (currentId.hasVersion() && versions.includes(currentId.version)) {
      var newVersion = component.versionArray.length ? component.latest() : null;
      var newId = currentId.changeVersion(newVersion);
      consumer.bitMap.updateComponentId(newId);
    }
  });
}

exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(version, force, id) {
    var _this = this;

    var consumer, untag, results;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 2:
            consumer = _context2.sent;

            untag = function () {
              var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee() {
                var bitId, result;
                return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        if (!id) {
                          _context.next = 7;
                          break;
                        }

                        bitId = consumer.getParsedId(id);
                        // a user might run the command `bit untag id@version` instead of `bit untag id version`

                        if (bitId.hasVersion() && !version) version = bitId.version;
                        _context.next = 5;
                        return (0, (_untagComponent || _load_untagComponent()).removeLocalVersion)(consumer.scope, bitId, version, force);

                      case 5:
                        result = _context.sent;
                        return _context.abrupt('return', [result]);

                      case 7:
                        return _context.abrupt('return', (0, (_untagComponent || _load_untagComponent()).removeLocalVersionsForAllComponents)(consumer.scope, version, force));

                      case 8:
                      case 'end':
                        return _context.stop();
                    }
                  }
                }, _callee, _this);
              }));

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

            _context2.next = 6;
            return untag();

          case 6:
            results = _context2.sent;

            updateBitMap(consumer, results);
            _context2.next = 10;
            return consumer.onDestroy();

          case 10:
            return _context2.abrupt('return', results);

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

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

  return unTagAction;
}();