"use strict";

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

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

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

  _semver = function () {
    return data;
  };

  return data;
}

function _diagnosis() {
  const data = _interopRequireDefault(require("../diagnosis"));

  _diagnosis = function () {
    return data;
  };

  return data;
}

function _npmClient() {
  const data = _interopRequireDefault(require("../../npm-client"));

  _npmClient = function () {
    return data;
  };

  return data;
}

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

  _constants = function () {
    return data;
  };

  return data;
}

function _package() {
  const data = _interopRequireDefault(require("../../../package.json"));

  _package = function () {
    return data;
  };

  return data;
}

class ValidateBitVersion extends _diagnosis().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'validate bit version');
    (0, _defineProperty2().default)(this, "description", 'validate that bit version is up to date');
    (0, _defineProperty2().default)(this, "category", 'core');
  }

  _formatSymptoms(bareResult) {
    if (!bareResult.data) throw new Error('ValidateBitVersion, bareResult.data is missing');

    if (!bareResult.data.latestVersion) {
      return 'could not fetch bit latest version';
    }

    return `bit is not up to date. 
    your version: ${_constants().BIT_VERSION} 
    latest version: ${bareResult.data.latestVersion}`;
  }

  _formatManualTreat(bareResult) {
    if (!bareResult.data) throw new Error('ValidateBitVersion, bareResult.data is missing');

    if (!bareResult.data.latestVersion) {
      return 'please make sure you have an internet connection';
    }

    return 'please upgrade your bit version';
  }

  _runExamine() {
    return (0, _bluebird().coroutine)(function* () {
      const bitLatestVersion = yield _npmClient().default.getPackageLatestVersion(_package().default.name);

      const bitCurrentVersion = _constants().BIT_VERSION;

      if (bitLatestVersion) {
        if (_semver().default.lt(bitCurrentVersion, bitLatestVersion)) {
          return {
            valid: false,
            data: {
              latestVersion: bitLatestVersion
            }
          };
        }

        return {
          valid: true
        };
      }

      return {
        valid: false,
        data: {
          latestVersion: null
        }
      };
    })();
  }

}

exports.default = ValidateBitVersion;