'use strict';

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _chai;

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

var _componentsList;

function _load_componentsList() {
  return _componentsList = _interopRequireDefault(require('./components-list'));
}

var _models;

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

var _bitId;

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

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

describe('ComponentList', function () {
  var getModelComponent = function getModelComponent() {
    return (_models || _load_models()).ModelComponent.fromBitId({ name: 'myName', scope: 'scope' });
  };
  describe('listLocalScope', function () {
    var modelComponent = void 0;
    before(function () {
      modelComponent = getModelComponent();
    });
    it('should return an empty array when there are no components in the scope', _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee() {
      var scope, results;
      return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              scope = { listLocal: function listLocal() {
                  return [];
                } };
              _context.next = 3;
              return (_componentsList || _load_componentsList()).default.listLocalScope(scope);

            case 3:
              results = _context.sent;

              (0, (_chai || _load_chai()).expect)(results).to.deep.equal([]);

            case 5:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, undefined);
    })));
    it('should return results with the correct id', _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2() {
      var scope, results, result;
      return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              scope = {
                listLocal: function listLocal() {
                  return [modelComponent];
                }
              };
              _context2.next = 3;
              return (_componentsList || _load_componentsList()).default.listLocalScope(scope);

            case 3:
              results = _context2.sent;
              result = results[0];

              (0, (_chai || _load_chai()).expect)(result).to.have.property('id');
              (0, (_chai || _load_chai()).expect)(result.id).to.be.an.instanceOf((_bitId || _load_bitId()).BitId);

            case 7:
            case 'end':
              return _context2.stop();
          }
        }
      }, _callee2, undefined);
    })));
    it('should return results with the correct deprecated status', _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3() {
      var scope, results, result;
      return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              modelComponent.deprecated = true;
              scope = {
                listLocal: function listLocal() {
                  return [modelComponent];
                }
              };
              _context3.next = 4;
              return (_componentsList || _load_componentsList()).default.listLocalScope(scope);

            case 4:
              results = _context3.sent;
              result = results[0];

              (0, (_chai || _load_chai()).expect)(result).to.have.property('deprecated');
              (0, (_chai || _load_chai()).expect)(result.deprecated).to.be.true;

            case 8:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, undefined);
    })));
  });
  describe('listScope', function () {
    var componentList = void 0;
    var scope = {};
    before(function () {
      var bitMap = { getAuthoredAndImportedBitIds: function getAuthoredAndImportedBitIds() {
          return new (_bitId || _load_bitId()).BitIds();
        } };
      var consumer = { scope, bitMap };
      componentList = new (_componentsList || _load_componentsList()).default(consumer);
    });
    it('should return results with the correct id', _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee5() {
      var modelComponent, results, result;
      return (_regenerator || _load_regenerator()).default.wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              modelComponent = getModelComponent();

              scope.list = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee4() {
                return (_regenerator || _load_regenerator()).default.wrap(function _callee4$(_context4) {
                  while (1) {
                    switch (_context4.prev = _context4.next) {
                      case 0:
                        return _context4.abrupt('return', [modelComponent]);

                      case 1:
                      case 'end':
                        return _context4.stop();
                    }
                  }
                }, _callee4, undefined);
              }));
              _context5.next = 4;
              return componentList.listScope(false, true);

            case 4:
              results = _context5.sent;
              result = results[0];

              (0, (_chai || _load_chai()).expect)(result).to.have.property('id');
              (0, (_chai || _load_chai()).expect)(result.id).to.be.an.instanceOf((_bitId || _load_bitId()).BitId);

            case 8:
            case 'end':
              return _context5.stop();
          }
        }
      }, _callee5, undefined);
    })));
  });
  describe('filterComponentsByWildcard', function () {
    describe('passing bit ids', function () {
      var bitIds = void 0;
      before(function () {
        bitIds = [(_bitId || _load_bitId()).BitId.parse('utils/is/string'), (_bitId || _load_bitId()).BitId.parse('utils/is/type'), (_bitId || _load_bitId()).BitId.parse('utils/fs/read'), (_bitId || _load_bitId()).BitId.parse('utils/fs/write'), (_bitId || _load_bitId()).BitId.parse('bar/foo'), (_bitId || _load_bitId()).BitId.parse('vuz/vuz')];
      });
      var expectToMatch = function expectToMatch(idWithWildCard, expectedResults) {
        var results = (_componentsList || _load_componentsList()).default.filterComponentsByWildcard(bitIds, idWithWildCard);
        var resultsStr = results.map(function (result) {
          return result.toString();
        });
        expectedResults.forEach(function (expectedResult) {
          return (0, (_chai || _load_chai()).expect)(resultsStr).to.include(expectedResult);
        });
        (0, (_chai || _load_chai()).expect)(results.length).to.equal(expectedResults.length);
      };
      it('should match utils/is/*', function () {
        expectToMatch('utils/is/*', ['utils/is/string', 'utils/is/type']);
      });
      it('should match utils/*', function () {
        expectToMatch('utils/*', ['utils/is/string', 'utils/is/type', 'utils/fs/read', 'utils/fs/write']);
      });
      it('should match *', function () {
        expectToMatch('*', ['utils/is/string', 'utils/is/type', 'utils/fs/read', 'utils/fs/write', 'bar/foo', 'vuz/vuz']);
      });
      it('should match */fs/*', function () {
        expectToMatch('*/fs/*', ['utils/fs/read', 'utils/fs/write']);
      });
      it('should match utils/*/read', function () {
        expectToMatch('utils/*/read', ['utils/fs/read']);
      });
      it('should match v*', function () {
        expectToMatch('v*', ['vuz/vuz']);
      });
      it('should not match non-exist*', function () {
        expectToMatch('non-exist*', []);
      });
      it('should match bit ids also without the scope name', function () {
        expectToMatch('fs*', ['utils/fs/read', 'utils/fs/write']);
      });
      it('should not match s* as non of the ids starts with "s" (with and without scope names)', function () {
        expectToMatch('s*', []);
      });
      it('when no wildcard is specified, it should match an exact id with a scope name', function () {
        expectToMatch('utils/fs/read', ['utils/fs/read']);
      });
      it('when no wildcard is specified, it should match an exact id without a scope name', function () {
        expectToMatch('fs/read', ['utils/fs/read']);
      });
      it('should match multiple different ids when using an array of ids with wildcard', function () {
        expectToMatch(['vuz/*', 'utils/fs/*'], ['vuz/vuz', 'utils/fs/read', 'utils/fs/write']);
      });
    });
  });
});