'use strict';

var _chai;

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

var _componentsIndex;

function _load_componentsIndex() {
  return _componentsIndex = _interopRequireDefault(require('./components-index'));
}

var _bitId;

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

var _models;

function _load_models() {
  return _models = require('../models');
}

var _object;

function _load_object() {
  return _object = _interopRequireDefault(require('./object'));
}

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

describe('ComponentsIndex', function () {
  describe('addOne', function () {
    var componentsIndex = void 0;
    beforeEach(function () {
      componentsIndex = new (_componentsIndex || _load_componentsIndex()).default('scope-path');
    });
    it('should add to the index array', function () {
      var modelComponent = new (_models || _load_models()).ModelComponent({ scope: 'my-scope', name: 'is-string' });
      componentsIndex.addOne(modelComponent);
      var allIds = componentsIndex.getIds();
      var id = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'is-string' });
      (0, (_chai || _load_chai()).expect)(allIds[0]).to.deep.equal(id);
    });
    it('should not add the same component multiple times', function () {
      var modelComponent = new (_models || _load_models()).ModelComponent({ scope: 'my-scope', name: 'is-string' });
      componentsIndex.addOne(modelComponent);
      componentsIndex.addOne(modelComponent);
      componentsIndex.addOne(modelComponent);
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(1);
    });
    it('should not add BitObjects that are not Symlink nor ModelComponent', function () {
      var bitObject = new (_object || _load_object()).default({ scope: 'my-scope', name: 'is-string' });
      var result = componentsIndex.addOne(bitObject);
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(0);
      (0, (_chai || _load_chai()).expect)(result).to.be.false;
    });
  });
  describe('remove', function () {
    var componentsIndex = void 0;
    beforeEach(function () {
      componentsIndex = new (_componentsIndex || _load_componentsIndex()).default('scope-path');
    });
    it('should remove from the index array', function () {
      var modelComponent = new (_models || _load_models()).ModelComponent({ scope: 'my-scope', name: 'is-string' });
      componentsIndex.addOne(modelComponent);
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(1);
      componentsIndex.removeOne(modelComponent.hash().toString());
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(0);
    });
    it('should remove the correct one when there are multiple', function () {
      var isStringComponent = new (_models || _load_models()).ModelComponent({ scope: 'my-scope', name: 'is-string' });
      var isTypeComponent = new (_models || _load_models()).ModelComponent({ scope: 'my-scope', name: 'is-type' });
      componentsIndex.addOne(isStringComponent);
      componentsIndex.addOne(isTypeComponent);
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(2);
      componentsIndex.removeOne(isStringComponent.hash().toString());
      var allIds = componentsIndex.getIds();
      (0, (_chai || _load_chai()).expect)(allIds).to.have.lengthOf(1);
      var id = new (_bitId || _load_bitId()).BitId({ scope: 'my-scope', name: 'is-type' });
      (0, (_chai || _load_chai()).expect)(allIds[0]).to.deep.equal(id);
    });
  });
  describe('removeMany', function () {
    var componentsIndex = void 0;
    var isStringComponent = void 0;
    var isTypeComponent = void 0;
    beforeEach(function () {
      componentsIndex = new (_componentsIndex || _load_componentsIndex()).default('scope-path');
      isStringComponent = new (_models || _load_models()).ModelComponent({ name: 'is-string' });
      isTypeComponent = new (_models || _load_models()).ModelComponent({ name: 'is-type' });
      componentsIndex.addMany([isStringComponent, isTypeComponent]);
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(2);
    });
    it('should remove multiple when removing them at the same removeMany call', function () {
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(2);
      componentsIndex.removeMany([isStringComponent.hash().toString(), isTypeComponent.hash().toString()]);
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(0);
    });
    it('should remove multiple when removing them with separate removeMany calls', function () {
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(2);
      componentsIndex.removeMany([isStringComponent.hash().toString()]);
      componentsIndex.removeMany([isTypeComponent.hash().toString()]);
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(0);
    });
    it('should remove multiple when calling them with separate removeMany calls using array.map', function () {
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(2);
      [isStringComponent.hash().toString(), isTypeComponent.hash().toString()].map(function (h) {
        return componentsIndex.removeMany([h]);
      });
      (0, (_chai || _load_chai()).expect)(componentsIndex.getIds()).to.have.lengthOf(0);
    });
  });
  describe('getIds', function () {
    var componentsIndex = void 0;
    beforeEach(function () {
      componentsIndex = new (_componentsIndex || _load_componentsIndex()).default('scope-path');
    });
    it('should not return symlinks', function () {
      var symlink = new (_models || _load_models()).Symlink({ scope: 'my-scope', name: 'is-type' });
      componentsIndex.addOne(symlink);
      var allIds = componentsIndex.getIds();
      (0, (_chai || _load_chai()).expect)(allIds).to.have.lengthOf(0);
    });
  });
  describe('getIdsIncludesSymlinks', function () {
    var componentsIndex = void 0;
    beforeEach(function () {
      componentsIndex = new (_componentsIndex || _load_componentsIndex()).default('scope-path');
    });
    it('should return symlinks', function () {
      var symlink = new (_models || _load_models()).Symlink({ scope: 'my-scope', name: 'is-type' });
      componentsIndex.addOne(symlink);
      var allIds = componentsIndex.getIdsIncludesSymlinks();
      (0, (_chai || _load_chai()).expect)(allIds).to.have.lengthOf(1);
    });
    it('should return components and symlinks if both exist', function () {
      var symlink = new (_models || _load_models()).Symlink({ scope: 'my-scope', name: 'is-type' });
      var modelComponent = new (_models || _load_models()).ModelComponent({ scope: 'my-scope', name: 'is-string' });
      componentsIndex.addMany([symlink, modelComponent]);
      var allIds = componentsIndex.getIdsIncludesSymlinks();
      (0, (_chai || _load_chai()).expect)(allIds).to.have.lengthOf(2);
    });
  });
});