"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

function _semver() {
  const data = _interopRequireDefault(require("semver"));

  _semver = function () {
    return data;
  };

  return data;
}

function _version() {
  const data = _interopRequireDefault(require("./version"));

  _version = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../constants");

  _constants = function () {
    return data;
  };

  return data;
}

function _exceptions() {
  const data = require("./exceptions");

  _exceptions = function () {
    return data;
  };

  return data;
}

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

function isLatestTested(versionStr) {
  if (!versionStr.includes(_constants().LATEST_TESTED_MARK)) return false;
  const splited = versionStr.split(_constants().LATEST_TESTED_MARK);
  if (splited.length !== 2) return false;
  const [, numberStr] = splited;
  const version = isRegular(numberStr);
  if (!version) return false;
  return true;
}

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

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

function returnLatestTestedVersion(versionStr) {
  const [, numberStr] = versionStr.split(_constants().LATEST_TESTED_MARK);
  return new (_version().default)(numberStr, true);
}

function returnLatest() {
  return new (_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().InvalidVersion)(versionStr);
}