'use strict';

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

var _regenerator;

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

var parseValues = function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(consumer, values) {
    var isVersion, componentsList, _bitIds, firstValue, lastValue, oneBeforeLastValue, isLastItemVersion, isOneBeforeLastItemVersion;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            isVersion = function isVersion(str) {
              return (_semver || _load_semver()).default.valid(str);
            };
            // option #1: bit diff
            // no arguments


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

            componentsList = new (_componentsList || _load_componentsList()).default(consumer);
            _context2.next = 5;
            return componentsList.listModifiedComponents();

          case 5:
            _bitIds = _context2.sent;
            return _context2.abrupt('return', { bitIds: _bitIds });

          case 7:
            firstValue = values[0];
            lastValue = values[values.length - 1];
            oneBeforeLastValue = values[values.length - 2];
            isLastItemVersion = isVersion(lastValue);
            isOneBeforeLastItemVersion = isVersion(oneBeforeLastValue);
            // option #2: bit diff [ids...]
            // all arguments are ids

            if (isLastItemVersion) {
              _context2.next = 14;
              break;
            }

            return _context2.abrupt('return', { bitIds: values.map(function (id) {
                return (_bitId || _load_bitId()).BitId.parse(id);
              }) });

          case 14:
            if (isOneBeforeLastItemVersion) {
              _context2.next = 18;
              break;
            }

            if (!(values.length !== 2)) {
              _context2.next = 17;
              break;
            }

            throw new (_generalError || _load_generalError()).default(`bit diff [id] [version] syntax was used, however, ${values.length} arguments were given instead of 2`);

          case 17:
            return _context2.abrupt('return', { bitIds: [(_bitId || _load_bitId()).BitId.parse(firstValue)], version: lastValue });

          case 18:
            if (!(values.length !== 3)) {
              _context2.next = 20;
              break;
            }

            throw new (_generalError || _load_generalError()).default(`bit diff [id] [version] [to_version] syntax was used, however, ${values.length} arguments were given instead of 3`);

          case 20:
            return _context2.abrupt('return', { bitIds: [(_bitId || _load_bitId()).BitId.parse(firstValue)], version: oneBeforeLastValue, toVersion: lastValue });

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

  return function parseValues(_x2, _x3) {
    return _ref3.apply(this, arguments);
  };
}();

var _semver;

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

var _consumer;

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

var _bitId;

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

var _componentsList;

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

var _generalError;

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

var _componentsDiff;

function _load_componentsDiff() {
  return _componentsDiff = _interopRequireDefault(require('../../../consumer/component-ops/components-diff'));
}

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

exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(values) {
    var consumer, _ref2, bitIds, version, toVersion, diffResults;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 2:
            consumer = _context.sent;
            _context.next = 5;
            return parseValues(consumer, values);

          case 5:
            _ref2 = _context.sent;
            bitIds = _ref2.bitIds;
            version = _ref2.version;
            toVersion = _ref2.toVersion;

            if (!(!bitIds || !bitIds.length)) {
              _context.next = 11;
              break;
            }

            throw new (_generalError || _load_generalError()).default('there are no modified components to diff');

          case 11:
            _context.next = 13;
            return (0, (_componentsDiff || _load_componentsDiff()).default)(consumer, bitIds, version, toVersion);

          case 13:
            diffResults = _context.sent;
            _context.next = 16;
            return consumer.onDestroy();

          case 16:
            return _context.abrupt('return', diffResults);

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

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

  return diff;
}();