'use strict';

var _chai;

function _load_chai() {
  return _chai = require('chai');
}

var _bitIds;

function _load_bitIds() {
  return _bitIds = _interopRequireDefault(require('../bit-id/bit-ids'));
}

var _bitId;

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

var _resolveLatestVersion;

function _load_resolveLatestVersion() {
  return _resolveLatestVersion = _interopRequireDefault(require('./resolveLatestVersion'));
}

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

describe('getLatestVersionNumber', function () {
  var idWithNoVersion = new (_bitId || _load_bitId()).default({ name: 'is-string' });
  var idWithVersionLatest = new (_bitId || _load_bitId()).default({ name: 'is-string', version: 'latest' });
  var idWithVersion1 = new (_bitId || _load_bitId()).default({ name: 'is-string', version: '0.0.1' });
  var idWithVersion2 = new (_bitId || _load_bitId()).default({ name: 'is-string', version: '0.0.2' });
  var idWithVersion3 = new (_bitId || _load_bitId()).default({ name: 'is-string', version: '0.0.3' });
  var idWithNoVersionWithScope = new (_bitId || _load_bitId()).default({ scope: 'my-scope', name: 'is-string' });
  var idWithVersion1WithScope = new (_bitId || _load_bitId()).default({ scope: 'my-scope', name: 'is-string', version: '0.0.1' });

  it('should return the same id when bitIds is empty', function () {
    var bitIds = new (_bitIds || _load_bitIds()).default();
    var result = (0, (_resolveLatestVersion || _load_resolveLatestVersion()).default)(bitIds, idWithNoVersion);
    (0, (_chai || _load_chai()).expect)(result).to.deep.equal(idWithNoVersion);
  });
  it('should return the same id when bitIds does not have the bit id', function () {
    var anotherId = new (_bitId || _load_bitId()).default({ name: 'is-type' });
    var bitIds = new (_bitIds || _load_bitIds()).default(anotherId);
    var result = (0, (_resolveLatestVersion || _load_resolveLatestVersion()).default)(bitIds, idWithNoVersion);
    (0, (_chai || _load_chai()).expect)(result).to.deep.equal(idWithNoVersion);
  });
  it('should return the id from the bitIds array if it is there with a version', function () {
    var bitIds = new (_bitIds || _load_bitIds()).default(idWithVersion1);
    var result = (0, (_resolveLatestVersion || _load_resolveLatestVersion()).default)(bitIds, idWithNoVersion);
    (0, (_chai || _load_chai()).expect)(result).to.not.deep.equal(idWithNoVersion);
    (0, (_chai || _load_chai()).expect)(result).to.deep.equal(idWithVersion1);
  });
  it('should return the id with the latest version when given id does not have a version', function () {
    var bitIds = new (_bitIds || _load_bitIds()).default(idWithVersion2, idWithVersion3, idWithVersion1);
    var result = (0, (_resolveLatestVersion || _load_resolveLatestVersion()).default)(bitIds, idWithNoVersion);
    (0, (_chai || _load_chai()).expect)(result).to.not.deep.equal(idWithNoVersion);
    (0, (_chai || _load_chai()).expect)(result).to.deep.equal(idWithVersion3);
  });
  it('should return the id with the latest version when given id has version "latest"', function () {
    var bitIds = new (_bitIds || _load_bitIds()).default(idWithVersion2, idWithVersion3, idWithVersion1);
    var result = (0, (_resolveLatestVersion || _load_resolveLatestVersion()).default)(bitIds, idWithVersionLatest);
    (0, (_chai || _load_chai()).expect)(result).to.not.deep.equal(idWithVersionLatest);
    (0, (_chai || _load_chai()).expect)(result).to.deep.equal(idWithVersion3);
  });
  it('should not return the id with the latest version when given id has already a version', function () {
    var bitIds = new (_bitIds || _load_bitIds()).default(idWithVersion2, idWithVersion3, idWithVersion1);
    var result = (0, (_resolveLatestVersion || _load_resolveLatestVersion()).default)(bitIds, idWithVersion1);
    (0, (_chai || _load_chai()).expect)(result).to.not.deep.equal(idWithVersion3);
    (0, (_chai || _load_chai()).expect)(result).to.deep.equal(idWithVersion1);
  });
  it('should return the id from the bitIds array if it is there with same version and same scope', function () {
    var bitIds = new (_bitIds || _load_bitIds()).default(idWithVersion1WithScope);
    var result = (0, (_resolveLatestVersion || _load_resolveLatestVersion()).default)(bitIds, idWithNoVersionWithScope);
    (0, (_chai || _load_chai()).expect)(result).to.not.deep.equal(idWithNoVersionWithScope);
    (0, (_chai || _load_chai()).expect)(result).to.deep.equal(idWithVersion1WithScope);
  });
  it('should not return the id from the bitIds array if it is there with same version but different scope', function () {
    var idWithDifferentScope = new (_bitId || _load_bitId()).default({ scope: 'my-another-scope', name: 'is-string', version: '0.0.1' });
    var bitIds = new (_bitIds || _load_bitIds()).default(idWithDifferentScope);
    var result = (0, (_resolveLatestVersion || _load_resolveLatestVersion()).default)(bitIds, idWithNoVersionWithScope);
    (0, (_chai || _load_chai()).expect)(result).to.not.deep.equal(idWithDifferentScope);
    (0, (_chai || _load_chai()).expect)(result).to.deep.equal(idWithNoVersionWithScope);
  });
  it('should not return the id from the bitIds array if it is there with same version but empty scope', function () {
    var bitIds = new (_bitIds || _load_bitIds()).default(idWithVersion1);
    var result = (0, (_resolveLatestVersion || _load_resolveLatestVersion()).default)(bitIds, idWithNoVersionWithScope);
    (0, (_chai || _load_chai()).expect)(result).to.not.deep.equal(idWithVersion1);
    (0, (_chai || _load_chai()).expect)(result).to.deep.equal(idWithNoVersionWithScope);
  });
  it('should return the same id when bitIds has a similar id where its name is equal to scope+name of the id', function () {
    var idWithScope = new (_bitId || _load_bitId()).default({ scope: 'is', name: 'string' });
    var idWithoutScope = new (_bitId || _load_bitId()).default({ name: 'is/string', version: '0.0.1' });
    var bitIds = new (_bitIds || _load_bitIds()).default(idWithoutScope);
    var result = (0, (_resolveLatestVersion || _load_resolveLatestVersion()).default)(bitIds, idWithScope);
    (0, (_chai || _load_chai()).expect)(result).to.deep.equal(idWithScope);
  });
});