'use strict';

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

var _regenerator;

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

/**
 * when user didn't enter any id and used '--all' flag, populate all ids.
 */
var populateIds = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(consumer, checkoutProps) {
    var idsFromBitMap;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (checkoutProps.all) {
              _context.next = 2;
              break;
            }

            throw new (_generalError || _load_generalError()).default('please specify [ids...] or use --all flag');

          case 2:
            idsFromBitMap = consumer.bitMap.getAllBitIds([(_constants || _load_constants()).COMPONENT_ORIGINS.AUTHORED, (_constants || _load_constants()).COMPONENT_ORIGINS.IMPORTED]);

            checkoutProps.ids = idsFromBitMap.map(function (bitId) {
              var version = checkoutProps.latestVersion ? (_constants || _load_constants()).LATEST : bitId.version;
              return bitId.changeVersion(version);
            });

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

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

var parseValues = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(consumer, values, checkoutProps) {
    var firstValue, ids;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            firstValue = (_ramda || _load_ramda()).default.head(values);

            checkoutProps.version = firstValue && ((_bitId || _load_bitId()).BitId.isValidVersion(firstValue) || firstValue === (_constants || _load_constants()).LATEST) ? firstValue : undefined;
            ids = checkoutProps.version ? (_ramda || _load_ramda()).default.tail(values) : values; // if first value is a version, the rest are ids

            checkoutProps.latestVersion = Boolean(checkoutProps.version && checkoutProps.version === (_constants || _load_constants()).LATEST);

            if (!(!firstValue && !checkoutProps.reset && !checkoutProps.all)) {
              _context2.next = 6;
              break;
            }

            throw new (_generalError || _load_generalError()).default('please enter [values...] or use --reset --all flags');

          case 6:
            if (!(checkoutProps.reset && checkoutProps.version)) {
              _context2.next = 8;
              break;
            }

            throw new (_generalError || _load_generalError()).default(`the first argument "${checkoutProps.version}" seems to be a version. however, --reset flag doesn't support a version`);

          case 8:
            if (!(!checkoutProps.reset && !checkoutProps.version)) {
              _context2.next = 14;
              break;
            }

            if (!ids.length) {
              _context2.next = 13;
              break;
            }

            throw new (_generalError || _load_generalError()).default(`the specified version "${ids[0]}" is not a valid version`);

          case 13:
            throw new (_generalError || _load_generalError()).default('please specify a version');

          case 14:
            if (!(ids.length && checkoutProps.all)) {
              _context2.next = 16;
              break;
            }

            throw new (_generalError || _load_generalError()).default('please specify either [ids...] or --all, not both');

          case 16:
            if (ids.length) {
              _context2.next = 21;
              break;
            }

            _context2.next = 19;
            return populateIds(consumer, checkoutProps);

          case 19:
            _context2.next = 22;
            break;

          case 21:
            checkoutProps.ids = ids.map(function (id) {
              return consumer.getParsedId(id);
            });

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

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

var _ramda;

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

var _consumer;

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

var _checkoutVersion;

function _load_checkoutVersion() {
  return _checkoutVersion = _interopRequireDefault(require('../../../consumer/versions-ops/checkout-version'));
}

var _loader;

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

var _loaderMessages;

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

var _bitId;

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

var _generalError;

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

var _constants;

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

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 _ref3 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(values, checkoutProps) {
    var consumer, checkoutResults;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_CHECKOUT);
            _context3.next = 3;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 3:
            consumer = _context3.sent;
            _context3.next = 6;
            return parseValues(consumer, values, checkoutProps);

          case 6:
            _context3.next = 8;
            return (0, (_checkoutVersion || _load_checkoutVersion()).default)(consumer, checkoutProps);

          case 8:
            checkoutResults = _context3.sent;
            _context3.next = 11;
            return consumer.onDestroy();

          case 11:
            return _context3.abrupt('return', checkoutResults);

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

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

  return checkout;
}();