'use strict';

var _chai;

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

var _bitId;

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

var _exceptions;

function _load_exceptions() {
  return _exceptions = require('./exceptions');
}

describe('Bit-id', function () {
  it('should be frozen', function () {
    var bitId = new (_bitId || _load_bitId()).BitId({ name: 'my-name' });
    (0, (_chai || _load_chai()).expect)(function () {
      return bitId.scope = 'name';
    }).to.throw();
    (0, (_chai || _load_chai()).expect)(function () {
      return bitId.name = 'name';
    }).to.throw();
    (0, (_chai || _load_chai()).expect)(function () {
      return bitId.version = 'name';
    }).to.throw();
  });
  describe('constructor', function () {
    it('should throw for an invalid scope', function () {
      var invalidScope = function invalidScope() {
        return new (_bitId || _load_bitId()).BitId({ scope: 123, name: 'my-name' });
      };
      (0, (_chai || _load_chai()).expect)(invalidScope).to.throw((_exceptions || _load_exceptions()).InvalidScopeName);
    });
    it('should throw for an invalid name', function () {
      var invalidName = function invalidName() {
        return new (_bitId || _load_bitId()).BitId({ name: ['a', 'b'] });
      };
      (0, (_chai || _load_chai()).expect)(invalidName).to.throw((_exceptions || _load_exceptions()).InvalidName);
    });
    it('should accept an empty scope', function () {
      var invalidScope = function invalidScope() {
        return new (_bitId || _load_bitId()).BitId({ scope: null, name: 'my-name' });
      };
      (0, (_chai || _load_chai()).expect)(invalidScope).to.not.throw();
    });
    it('should not accept an empty name', function () {
      (0, (_chai || _load_chai()).expect)(function () {
        return new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: null });
      }).to.throw((_exceptions || _load_exceptions()).InvalidName);
      (0, (_chai || _load_chai()).expect)(function () {
        return new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: undefined });
      }).to.throw((_exceptions || _load_exceptions()).InvalidName);
      (0, (_chai || _load_chai()).expect)(function () {
        return new (_bitId || _load_bitId()).BitId({ scope: 'my-scope' });
      }).to.throw((_exceptions || _load_exceptions()).InvalidName);
    });
  });
  describe('changeScope', function () {
    it('should return a new BitId with different scope', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ name: 'my-name' });
      (0, (_chai || _load_chai()).expect)(bitId.changeScope('my-scope')).to.have.property('scope').that.equal('my-scope');
    });
    it('should accept an empty parameter to remove the scope', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name' });
      (0, (_chai || _load_chai()).expect)(bitId.changeScope()).to.have.property('scope').that.is.null;
    });
  });
  describe('changeVersion', function () {
    it('should return a new BitId with different version', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ name: 'my-name' });
      (0, (_chai || _load_chai()).expect)(bitId.changeVersion('0.0.1')).to.have.property('version').that.equal('0.0.1');
    });
    it('should accept an empty parameter to remove the version', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name', version: '0.0.1' });
      (0, (_chai || _load_chai()).expect)(bitId.changeVersion()).to.have.property('version').that.is.null;
    });
  });
  describe('hasScope', function () {
    it('should return false when there is no scope', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ name: 'my-name' });
      (0, (_chai || _load_chai()).expect)(bitId.hasScope()).to.be.false;
    });
    it('should return true when there is scope', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name' });
      (0, (_chai || _load_chai()).expect)(bitId.hasScope()).to.be.true;
    });
  });
  describe('getValidBitId', function () {
    it('should convert CSSComp to css-comp', function () {
      var bitName = 'CSSComp';
      (0, (_chai || _load_chai()).expect)((_bitId || _load_bitId()).BitId.getValidBitId('global', bitName).toString()).to.equal('global/css-comp');
    }).timeout(5000);
    it('should convert EN-US to en-us', function () {
      var bitName = 'EN-US';
      (0, (_chai || _load_chai()).expect)((_bitId || _load_bitId()).BitId.getValidBitId('global', bitName).toString()).to.equal('global/en-us');
    });
    it('should convert AppBar to app-bar', function () {
      var bitName = 'AppBar';
      (0, (_chai || _load_chai()).expect)((_bitId || _load_bitId()).BitId.getValidBitId('global', bitName).toString()).to.equal('global/app-bar');
    });
    it('should convert app-bar to app-bar', function () {
      var bitName = 'app-bar';
      (0, (_chai || _load_chai()).expect)((_bitId || _load_bitId()).BitId.getValidBitId('global', bitName).toString()).to.equal('global/app-bar');
    });
    it('should convert app-Bar to app-bar', function () {
      var bitName = 'app-Bar';
      (0, (_chai || _load_chai()).expect)((_bitId || _load_bitId()).BitId.getValidBitId('global', bitName).toString()).to.equal('global/app-bar');
    });
    it('should convert App-Bar to app-bar', function () {
      var bitName = 'App-Bar';
      (0, (_chai || _load_chai()).expect)((_bitId || _load_bitId()).BitId.getValidBitId('global', bitName).toString()).to.equal('global/app-bar');
    });
    it('should convert CSScomp to cs-scomp ', function () {
      var bitName = 'CSScomp';
      (0, (_chai || _load_chai()).expect)((_bitId || _load_bitId()).BitId.getValidBitId('global', bitName).toString()).to.equal('global/cs-scomp');
    });
    it('should convert CSS@comp/CSScomp to css@comp/cs-scomp ', function () {
      var bitName = 'CSScomp';
      var global = 'CSS@comp';
      (0, (_chai || _load_chai()).expect)((_bitId || _load_bitId()).BitId.getValidBitId(global, bitName).toString()).to.equal('css@comp/cs-scomp');
    });
    it('should convert CSS!!####@comp/app-Bar to app-Bar', function () {
      var bitName = 'app-Bar';
      var global = 'CSS!!####@comp';
      (0, (_chai || _load_chai()).expect)((_bitId || _load_bitId()).BitId.getValidBitId(global, bitName).toString()).to.equal('css!!####@comp/app-bar');
    });
  });
  describe('toString', function () {
    it('should not contain the version as latest', function () {
      var bitId = (_bitId || _load_bitId()).BitId.parse('bit.envs/mocha/react@latest');
      (0, (_chai || _load_chai()).expect)(bitId.toString()).to.equal('bit.envs/mocha/react');
    });
  });
  describe('parse', function () {
    it('should parse id with scope correctly', function () {
      var bitId = (_bitId || _load_bitId()).BitId.parse('scope/my/long/comp/id', true);
      (0, (_chai || _load_chai()).expect)(bitId.scope).to.equal('scope');
      (0, (_chai || _load_chai()).expect)(bitId.name).to.equal('my/long/comp/id');
      (0, (_chai || _load_chai()).expect)(bitId.box).to.be.null;
      (0, (_chai || _load_chai()).expect)(bitId.version).to.equal('latest');
    });
    it('should parse id without scope correctly', function () {
      var bitId = (_bitId || _load_bitId()).BitId.parse('scope/my/long/comp/id', false);
      (0, (_chai || _load_chai()).expect)(bitId.scope).to.be.null;
      (0, (_chai || _load_chai()).expect)(bitId.name).to.equal('scope/my/long/comp/id');
      (0, (_chai || _load_chai()).expect)(bitId.box).to.be.null;
      (0, (_chai || _load_chai()).expect)(bitId.version).to.equal('latest');
    });
    it('should throw for an invalid id', function () {
      var bitId = function bitId() {
        return (_bitId || _load_bitId()).BitId.parse('scope/my/long/co*mp/id', false);
      };
      (0, (_chai || _load_chai()).expect)(bitId).to.throw();
    });
  });
  describe('hasSameScope', function () {
    it('should return true when it has the same scope', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name' });
      var anotherId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'another-name' });
      (0, (_chai || _load_chai()).expect)(bitId.hasSameScope(anotherId)).to.be.true;
    });
    it('should return false when it does not the same scope', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name' });
      var anotherId = new (_bitId || _load_bitId()).BitId({ scope: 'another-scope', name: 'my-name' });
      (0, (_chai || _load_chai()).expect)(bitId.hasSameScope(anotherId)).to.be.false;
    });
    it('should return true when both scopes are null', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: null, name: 'my-name' });
      var anotherId = new (_bitId || _load_bitId()).BitId({ scope: null, name: 'my-name' });
      (0, (_chai || _load_chai()).expect)(bitId.hasSameScope(anotherId)).to.be.true;
    });
    it('should return false when one of the scope is null', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: null, name: 'my-name' });
      var anotherId = new (_bitId || _load_bitId()).BitId({ scope: 'another-scope', name: 'my-name' });
      (0, (_chai || _load_chai()).expect)(bitId.hasSameScope(anotherId)).to.be.false;
    });
  });
  describe('isEqual', function () {
    it('should return true for an exact match', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name', version: '0.0.1' });
      var anotherId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name', version: '0.0.1' });
      (0, (_chai || _load_chai()).expect)(bitId.isEqual(anotherId)).to.be.true;
    });
    it('should return false for a version mismatch', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name', version: '0.0.1' });
      var anotherId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name', version: '0.0.2' });
      (0, (_chai || _load_chai()).expect)(bitId.isEqual(anotherId)).to.be.false;
    });
    it('should return false for a name mismatch', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name', version: '0.0.1' });
      var anotherId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'another-name', version: '0.0.1' });
      (0, (_chai || _load_chai()).expect)(bitId.isEqual(anotherId)).to.be.false;
    });
    it('should return false for a scope mismatch', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name', version: '0.0.1' });
      var anotherId = new (_bitId || _load_bitId()).BitId({ scope: 'another-scope', name: 'my-name', version: '0.0.1' });
      (0, (_chai || _load_chai()).expect)(bitId.isEqual(anotherId)).to.be.false;
    });
  });
  describe('serialize', function () {
    it('should delete the scope property if not set', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ name: 'my-name', version: '0.0.1' });
      (0, (_chai || _load_chai()).expect)(bitId.serialize()).to.not.have.property('scope');
    });
    it('should delete the version property if not set', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ name: 'my-name' });
      (0, (_chai || _load_chai()).expect)(bitId.serialize()).to.not.have.property('version');
    });
    it('should include all properties if they are all set', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name', version: '0.0.1' });
      (0, (_chai || _load_chai()).expect)(bitId.serialize()).to.have.property('name');
      (0, (_chai || _load_chai()).expect)(bitId.serialize()).to.have.property('scope');
      (0, (_chai || _load_chai()).expect)(bitId.serialize()).to.have.property('version');
    });
    it('should not be an instance of BitId', function () {
      var bitId = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name', version: '0.0.1' });
      (0, (_chai || _load_chai()).expect)(bitId.serialize()).to.not.be.an.instanceof((_bitId || _load_bitId()).BitId);
    });
  });
  describe('parseBackwardCompatible', function () {
    it('should parse the new format, which is an object', function () {
      var bitIdObj = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'my-name', version: '0.0.1' }).serialize();
      var bitId = (_bitId || _load_bitId()).BitId.parseBackwardCompatible(bitIdObj);
      (0, (_chai || _load_chai()).expect)(bitId).to.be.an.instanceof((_bitId || _load_bitId()).BitId);
      (0, (_chai || _load_chai()).expect)(bitId.name).to.equal('my-name');
      (0, (_chai || _load_chai()).expect)(bitId.scope).to.equal('my-scope');
      (0, (_chai || _load_chai()).expect)(bitId.version).to.equal('0.0.1');
    });
    it('should parse the old format, which is a string', function () {
      var bitIdStr = 'my-scope/my-box/my-name@0.0.1';
      var bitId = (_bitId || _load_bitId()).BitId.parseBackwardCompatible(bitIdStr);
      (0, (_chai || _load_chai()).expect)(bitId).to.be.an.instanceof((_bitId || _load_bitId()).BitId);
      (0, (_chai || _load_chai()).expect)(bitId.name).to.equal('my-box/my-name');
      (0, (_chai || _load_chai()).expect)(bitId.scope).to.equal('my-scope');
      (0, (_chai || _load_chai()).expect)(bitId.version).to.equal('0.0.1');
    });
  });
});