'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 _bitMap;

function _load_bitMap() {
  return _bitMap = _interopRequireDefault(require('../../bit-map'));
}

var _constants;

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

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

/**
 * Change all the component's version to be a valid semver
 * @param {*} bitMap - The bit map object
 */
function changeVersionToSemVer(bitMap) {
  var addUpdatedVersion = function addUpdatedVersion(value, key) {
    // In case there is already a semver, do nothing
    var splitedId = key.split((_constants || _load_constants()).VERSION_DELIMITER);
    var version = splitedId[1];
    if (version && !(_semver || _load_semver()).default.valid(version)) {
      var newVersion = `0.0.${version}`;
      var newId = `${splitedId[0]}${(_constants || _load_constants()).VERSION_DELIMITER}${newVersion}`;
      bitMap.components[newId] = value;
      delete bitMap.components[key];
    }
  };
  // Go over the versions array and update them
  (_ramda || _load_ramda()).default.forEachObjIndexed(addUpdatedVersion, bitMap.getAllComponents());
  return bitMap;
}

var changeVersionToSemVerDeclartaion = {
  name: 'change bit map versions to SemVer',
  migrate: changeVersionToSemVer
};

exports.default = changeVersionToSemVerDeclartaion;