'use strict';

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

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

exports.default = versionParser;

var _semver;

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

var _version;

function _load_version() {
  return _version = _interopRequireDefault(require('./version'));
}

var _constants;

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

var _utils;

function _load_utils() {
  return _utils = require('../utils');
}

var _exceptions;

function _load_exceptions() {
  return _exceptions = require('./exceptions');
}

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

function isLatest(versionStr) {
  return versionStr === (_constants || _load_constants()).LATEST;
}

function isLatestTested(versionStr) {
  if (!(0, (_utils || _load_utils()).contains)(versionStr, (_constants || _load_constants()).LATEST_TESTED_MARK)) return false;
  var splited = versionStr.split((_constants || _load_constants()).LATEST_TESTED_MARK);
  if (splited.length !== 2) return false;

  var _splited = _slicedToArray(splited, 2),
      numberStr = _splited[1];

  var version = isRegular(numberStr);
  if (!version) return false;
  return true;
}

function isRegular(versionStr) {
  return (_semver || _load_semver()).default.valid(versionStr);
}

function returnRegular(versionStr) {
  return new (_version || _load_version()).default(versionStr, false);
}

function returnLatestTestedVersion(versionStr) {
  var _versionStr$split = versionStr.split((_constants || _load_constants()).LATEST_TESTED_MARK),
      _versionStr$split2 = _slicedToArray(_versionStr$split, 2),
      numberStr = _versionStr$split2[1];

  return new (_version || _load_version()).default(numberStr, true);
}

function returnLatest() {
  return new (_version || _load_version()).default(null, true);
}

function convertToSemVer(versionStr) {
  return returnRegular(`0.0.${versionStr}`);
}

function versionParser(versionStr) {
  if (!versionStr) return returnLatest();
  if (isLatest(versionStr)) return returnLatest();
  if (isLatestTested(versionStr)) return returnLatestTestedVersion(versionStr);
  if (isRegular(versionStr)) return returnRegular(versionStr);
  if (!Number.isNaN(versionStr)) return convertToSemVer(versionStr);
  throw new (_exceptions || _load_exceptions()).InvalidVersion(versionStr);
}