'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _semver;

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

var _exceptions;

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

var _versionParser;

function _load_versionParser() {
  return _versionParser = _interopRequireDefault(require('./version-parser'));
}

var _constants;

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

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Version = function () {
  function Version(versionNum, latest) {
    _classCallCheck(this, Version);

    this.versionNum = versionNum;
    this.latest = latest;
  }

  _createClass(Version, [{
    key: 'increase',
    value: function increase() {
      var releaseType = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (_constants || _load_constants()).DEFAULT_BIT_RELEASE_TYPE;

      if (!this.versionNum) throw new (_exceptions || _load_exceptions()).InvalidVersionChange();
      this.versionNum = (_semver || _load_semver()).default.inc(this.versionNum, releaseType);
      return this;
    }
  }, {
    key: 'resolve',
    value: function resolve(availableVersion) {
      var getLatest = function getLatest() {
        return (_semver || _load_semver()).default.maxSatisfying(availableVersion, '*');
      };

      if (this.latest) return getLatest();
      return this.versionNum;
    }
  }, {
    key: 'toString',
    value: function toString() {
      if (!this.versionNum && this.latest) return 'latest';
      if (this.versionNum && this.latest) return `*${this.versionNum}`;
      if (this.versionNum && !this.latest) return this.versionNum.toString();
      throw new (_exceptions || _load_exceptions()).InvalidVersion();
    }
  }], [{
    key: 'parse',
    value: function parse(versionStr) {
      // $FlowFixMe unclear error, might be a bug, try to remove with next Flow version
      return (0, (_versionParser || _load_versionParser()).default)(versionStr);
    }
  }, {
    key: 'validate',
    value: function validate(versionStr) {
      try {
        (0, (_versionParser || _load_versionParser()).default)(versionStr);
        return true;
      } catch (err) {
        return false;
      }
    }
  }]);

  return Version;
}();

exports.default = Version;