'use strict';

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

var _semver;

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

var _chalk;

function _load_chalk() {
  return _chalk = _interopRequireDefault(require('chalk'));
}

var _constants;

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

var _loader;

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

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

var createMajorMessage = function createMajorMessage(remoteVersion, currentVersion) {
  return (_chalk || _load_chalk()).default.red(`Fatal: There is a mismatch between the remote scope version - "${remoteVersion}" and your bit version - "${currentVersion}", please update\n`);
};

var createMinorMessage = function createMinorMessage(remoteVersion, currentVersion) {
  return (_chalk || _load_chalk()).default.yellow(`Warning: There is a mismatch between the remote scope version - "${remoteVersion}" and your bit version - "${currentVersion}", please update\n`);
};

function checkVersionCompatibility(remoteVersion) {
  // In case the client is newer than the server version don't check computability
  // (Should be change in the future, but right now we won't release a client which we don't support
  //  on the server)
  if ((_semver || _load_semver()).gte((_constants || _load_constants()).BIT_VERSION, remoteVersion)) {
    return;
  }

  var remoteMajor = (_semver || _load_semver()).major(remoteVersion);
  var remoteMinor = (_semver || _load_semver()).minor(remoteVersion);
  var remotePatch = (_semver || _load_semver()).patch(remoteVersion);
  var localMajor = (_semver || _load_semver()).major((_constants || _load_constants()).BIT_VERSION);
  var localMinor = (_semver || _load_semver()).minor((_constants || _load_constants()).BIT_VERSION);
  var localPatch = (_semver || _load_semver()).patch((_constants || _load_constants()).BIT_VERSION);

  if (remoteMajor > localMajor) {
    (_loader || _load_loader()).default.stop();
    console.log(createMajorMessage(remoteVersion, (_constants || _load_constants()).BIT_VERSION)); // eslint-disable-line
    (_loader || _load_loader()).default.start();
    return;
  }

  if (remoteMinor > localMinor) {
    (_loader || _load_loader()).default.stop();
    console.log(createMajorMessage(remoteVersion, (_constants || _load_constants()).BIT_VERSION)); // eslint-disable-line
    (_loader || _load_loader()).default.start();
    return;
  }

  if (remotePatch > localPatch) {
    (_loader || _load_loader()).default.stop();
    console.log(createMinorMessage(remoteVersion, (_constants || _load_constants()).BIT_VERSION)); // eslint-disable-line
    (_loader || _load_loader()).default.start();
  }
}