'use strict';

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

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _semver;

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

var _bitId;

function _load_bitId() {
  return _bitId = _interopRequireDefault(require('../../../bit-id/bit-id'));
}

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../../../logger/logger'));
}

var _constants;

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

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

/**
 * Change the dependencies versions and the compiler / testers ids to semver
 * @param {*} versionModel - The parsed version model
 */
function changeVersionToSemVer(versionModel) {
  var getUpdatedDependency = function getUpdatedDependency(dependency) {
    // Take care of very old models when the dependencies were strings
    // in this case we will keep it string but change it to contain semver
    // Those old model will still not work after this migration
    if (typeof dependency === 'string') {
      (_logger || _load_logger()).default.warn("The dependency dependency is stored with an old format, this version won't work properly");
      dependency = { id: _getUpdatedId(dependency), relativePaths: [] };
      return dependency;
    }
    dependency.id = _getUpdatedId(dependency.id);
    return dependency;
  };

  // Go over the versions array and update them
  var dependencies = (_ramda || _load_ramda()).default.map(getUpdatedDependency, versionModel.dependencies);
  var flattenedDependencies = (_ramda || _load_ramda()).default.map(_getUpdatedId, versionModel.flattenedDependencies);
  versionModel.dependencies = dependencies;
  versionModel.flattenedDependencies = flattenedDependencies;
  if (versionModel.tester) {
    versionModel.tester = _getUpdatedId(versionModel.tester);
  }
  if (versionModel.compiler) {
    versionModel.compiler = _getUpdatedId(versionModel.compiler);
  }
  return versionModel;
}


function _getUpdatedId(id) {
  try {
    // Fix also old version seperator (::)
    if (id.includes('::')) {
      id = id.replace('::', (_constants || _load_constants()).VERSION_DELIMITER);
    }
    var parsedId = (_bitId || _load_bitId()).default.parseBackwardCompatible(id);
    // Don't convert latest word
    if (parsedId.getVersion().latest) {
      return id;
    }

    var version = parsedId.getVersion().versionNum;
    // In case there is already a semver, do nothing
    if (!(_semver || _load_semver()).default.valid(version)) {
      version = `0.0.${version}`;
    }
    var newId = `${parsedId.toStringWithoutVersion()}${(_constants || _load_constants()).VERSION_DELIMITER}${version}`;
    return newId;
  } catch (err) {
    (_logger || _load_logger()).default.error(`couldn't parse the id ${id} in order to migrate it to semver`);
    throw err;
  }
}

var changeVersionToSemVerDeclartaion = {
  name: "change version's (deps & compiler / tester) to SemVer",
  migrate: changeVersionToSemVer
};

exports.default = changeVersionToSemVerDeclartaion;