'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator;

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

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _semver;

function _load_semver() {
  return _semver = _interopRequireDefault(require('semver'));
}

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _models;

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

var _scope;

function _load_scope() {
  return _scope = require('../../scope');
}

var _component;

function _load_component() {
  return _component = _interopRequireDefault(require('../component'));
}

var _bitId;

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

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../../logger/logger'));
}

var _bitMap;

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

var _consumer;

function _load_consumer() {
  return _consumer = _interopRequireDefault(require('../consumer'));
}

var _utils;

function _load_utils() {
  return _utils = require('../../utils');
}

var _constants;

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

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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"); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var ComponentsList = function () {
  function ComponentsList(consumer) {
    _classCallCheck(this, ComponentsList);

    this._fromFileSystem = [];
    this._fromBitMap = {};

    this.consumer = consumer;
    this.scope = consumer.scope;
    this.bitMap = consumer.bitMap;
  }

  /**
   * List all objects where the id is the object-id and the value is the Version object
   * It is useful when checking for modified components where the most important data is the Ref.
   */


  _createClass(ComponentsList, [{
    key: 'getFromObjects',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee() {
        var _this = this;

        var componentsObjects, componentsVersionsP, componentsVersions, allVersions;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (this._fromObjects) {
                  _context.next = 12;
                  break;
                }

                _context.next = 3;
                return this.scope.objects.listComponents(false);

              case 3:
                componentsObjects = _context.sent;
                componentsVersionsP = {};
                componentsVersions = {};

                componentsObjects.forEach(function (componentObjects) {
                  var latestVersionRef = componentObjects.versions[componentObjects.latest()];
                  var ObjId = new (_bitId || _load_bitId()).BitId({
                    scope: componentObjects.scope,
                    box: componentObjects.box,
                    name: componentObjects.name,
                    version: componentObjects.scope ? componentObjects.latest() : null
                  });
                  componentsVersionsP[ObjId.toString()] = _this.scope.getObject(latestVersionRef.hash);
                });

                _context.next = 9;
                return Promise.all((_ramda || _load_ramda()).default.values(componentsVersionsP));

              case 9:
                allVersions = _context.sent;


                Object.keys(componentsVersionsP).forEach(function (key, i) {
                  if (!allVersions[i]) {
                    // the component has a REF of its latest version, however, the object of the latest version is missing
                    var bitId = (_bitId || _load_bitId()).BitId.parse(key);
                    (_logger || _load_logger()).default.warn(`the model representation of ${bitId.toStringWithoutVersion()} is missing ${bitId.version} version, if this component is nested, this is a normal behavior, otherwise, the component is corrupted`);
                    // throw new CorruptedComponent(bitId.toStringWithoutVersion(), bitId.version);
                  }
                  componentsVersions[key] = allVersions[i];
                });
                // $FlowFixMe
                this._fromObjects = componentsVersions;

              case 12:
                return _context.abrupt('return', this._fromObjects);

              case 13:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function getFromObjects() {
        return _ref.apply(this, arguments);
      }

      return getFromObjects;
    }()
  }, {
    key: '_getAuthoredAndImportedFromFS',
    value: function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2() {
        var _ref3, _ref4, authored, imported;

        return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return Promise.all([this.getFromFileSystem((_constants || _load_constants()).COMPONENT_ORIGINS.AUTHORED), this.getFromFileSystem((_constants || _load_constants()).COMPONENT_ORIGINS.IMPORTED)]);

              case 2:
                _ref3 = _context2.sent;
                _ref4 = _slicedToArray(_ref3, 2);
                authored = _ref4[0];
                imported = _ref4[1];

                authored = authored || [];
                imported = imported || [];
                return _context2.abrupt('return', authored.concat(imported));

              case 9:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      function _getAuthoredAndImportedFromFS() {
        return _ref2.apply(this, arguments);
      }

      return _getAuthoredAndImportedFromFS;
    }()

    /**
     * Components that are in the model (either, committed from a local scope or imported), and were
     * changed in the file system
     *
     * @param {boolean} [load=false] - Whether to load the component (false will return only the id)
     * @return {Promise<string[]>}
     */

  }, {
    key: 'listModifiedComponents',
    value: function () {
      var _ref5 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3() {
        var _this2 = this;

        var load = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
        var fileSystemComponents, modifiedComponents;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.next = 2;
                return this._getAuthoredAndImportedFromFS();

              case 2:
                fileSystemComponents = _context3.sent;
                _context3.next = 5;
                return (0, (_utils || _load_utils()).filterAsync)(fileSystemComponents, function (component) {
                  return _this2.consumer.getComponentStatusById(component.id).then(function (status) {
                    return status.modified;
                  });
                });

              case 5:
                modifiedComponents = _context3.sent;

                if (!load) {
                  _context3.next = 8;
                  break;
                }

                return _context3.abrupt('return', modifiedComponents);

              case 8:
                return _context3.abrupt('return', modifiedComponents.map(function (component) {
                  return component.id;
                }));

              case 9:
              case 'end':
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));

      function listModifiedComponents() {
        return _ref5.apply(this, arguments);
      }

      return listModifiedComponents;
    }()
  }, {
    key: 'listOutdatedComponents',
    value: function () {
      var _ref6 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee4() {
        var fileSystemComponents, componentsFromModel;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _context4.next = 2;
                return this._getAuthoredAndImportedFromFS();

              case 2:
                fileSystemComponents = _context4.sent;
                _context4.next = 5;
                return this.scope.objects.listComponents(false);

              case 5:
                componentsFromModel = _context4.sent;
                return _context4.abrupt('return', fileSystemComponents.filter(function (component) {
                  var modelComponent = componentsFromModel.find(function (c) {
                    return c.id() === component.id.toStringWithoutVersion();
                  });
                  if (!modelComponent) return false;
                  var latestVersion = modelComponent.latest();
                  if (component.id.hasVersion() && (_semver || _load_semver()).default.gt(latestVersion, component.id.version)) {
                    component.latestVersion = latestVersion;
                    return true;
                  }
                  return false;
                }));

              case 7:
              case 'end':
                return _context4.stop();
            }
          }
        }, _callee4, this);
      }));

      function listOutdatedComponents() {
        return _ref6.apply(this, arguments);
      }

      return listOutdatedComponents;
    }()
  }, {
    key: 'newAndModifiedComponents',
    value: function () {
      var _ref7 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee5() {
        var _ref8, _ref9, newComponents, modifiedComponents, components;

        return (_regenerator || _load_regenerator()).default.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                _context5.next = 2;
                return Promise.all([this.listNewComponents(true), this.listModifiedComponents(true)]);

              case 2:
                _ref8 = _context5.sent;
                _ref9 = _slicedToArray(_ref8, 2);
                newComponents = _ref9[0];
                modifiedComponents = _ref9[1];
                components = [].concat(_toConsumableArray(newComponents), _toConsumableArray(modifiedComponents));
                return _context5.abrupt('return', Promise.all(components));

              case 8:
              case 'end':
                return _context5.stop();
            }
          }
        }, _callee5, this);
      }));

      function newAndModifiedComponents() {
        return _ref7.apply(this, arguments);
      }

      return newAndModifiedComponents;
    }()
  }, {
    key: 'idsFromObjects',
    value: function () {
      var _ref10 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee6() {
        var withScope = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
        var fromObjects, ids;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                _context6.next = 2;
                return this.getFromObjects();

              case 2:
                fromObjects = _context6.sent;
                ids = Object.keys(fromObjects);

                if (!withScope) {
                  _context6.next = 6;
                  break;
                }

                return _context6.abrupt('return', ids);

              case 6:
                return _context6.abrupt('return', ids.map(function (id) {
                  return (_bitId || _load_bitId()).BitId.parse(id).toStringWithoutScopeAndVersion();
                }));

              case 7:
              case 'end':
                return _context6.stop();
            }
          }
        }, _callee6, this);
      }));

      function idsFromObjects() {
        return _ref10.apply(this, arguments);
      }

      return idsFromObjects;
    }()

    /**
     * Components that are registered in bit.map but have never been committed
     *
     * @param {boolean} [load=false] - Whether to load the component (false will return only the id)
     * @return {Promise.<string[] | Component[]>}
     * @memberof ComponentsList
     */

  }, {
    key: 'listNewComponents',
    value: function () {
      var _ref11 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee7() {
        var load = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;

        var idsFromBitMap, idsFromObjects, newComponents, componentsIds, _ref12, components;

        return (_regenerator || _load_regenerator()).default.wrap(function _callee7$(_context7) {
          while (1) {
            switch (_context7.prev = _context7.next) {
              case 0:
                _context7.next = 2;
                return this.idsFromBitMap(false);

              case 2:
                idsFromBitMap = _context7.sent;
                _context7.next = 5;
                return this.idsFromObjects(false);

              case 5:
                idsFromObjects = _context7.sent;
                newComponents = [];

                idsFromBitMap.forEach(function (id) {
                  if (!idsFromObjects.includes(id)) {
                    newComponents.push(id);
                  }
                });

                if (!(!load || !newComponents.length)) {
                  _context7.next = 10;
                  break;
                }

                return _context7.abrupt('return', newComponents);

              case 10:
                componentsIds = newComponents.map(function (id) {
                  return (_bitId || _load_bitId()).BitId.parse(id);
                });
                _context7.next = 13;
                return this.consumer.loadComponents(componentsIds, false);

              case 13:
                _ref12 = _context7.sent;
                components = _ref12.components;
                return _context7.abrupt('return', components);

              case 16:
              case 'end':
                return _context7.stop();
            }
          }
        }, _callee7, this);
      }));

      function listNewComponents() {
        return _ref11.apply(this, arguments);
      }

      return listNewComponents;
    }()

    /**
     * Authored exported components (easily identified by having a scope) which are not saved in the model are
     * import-pending. Exclude them from the 'newComponents' and add them to 'importPendingComponents'.
     */

  }, {
    key: 'listNewComponentsAndImportPending',
    value: function () {
      var _ref13 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee8() {
        var allNewComponents, newComponents, importPendingComponents;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee8$(_context8) {
          while (1) {
            switch (_context8.prev = _context8.next) {
              case 0:
                _context8.next = 2;
                return this.listNewComponents(true);

              case 2:
                allNewComponents = _context8.sent;
                newComponents = [];
                importPendingComponents = [];

                allNewComponents.forEach(function (component) {
                  if (component.id.scope) {
                    importPendingComponents.push(component);
                  } else {
                    newComponents.push(component);
                  }
                });
                return _context8.abrupt('return', { newComponents, importPendingComponents });

              case 7:
              case 'end':
                return _context8.stop();
            }
          }
        }, _callee8, this);
      }));

      function listNewComponentsAndImportPending() {
        return _ref13.apply(this, arguments);
      }

      return listNewComponentsAndImportPending;
    }()
  }, {
    key: 'listCommitPendingOfAllScope',
    value: function () {
      var _ref14 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee9(version) {
        var includeImported = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
        var commitPendingComponents, importedComponents, commitPendingComponentsIds, commitPendingComponentsLatest, warnings;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee9$(_context9) {
          while (1) {
            switch (_context9.prev = _context9.next) {
              case 0:
                commitPendingComponents = void 0;
                _context9.next = 3;
                return this.idsFromBitMap(true, (_constants || _load_constants()).COMPONENT_ORIGINS.AUTHORED);

              case 3:
                commitPendingComponents = _context9.sent;

                if (!includeImported) {
                  _context9.next = 9;
                  break;
                }

                _context9.next = 7;
                return this.idsFromBitMap(true, (_constants || _load_constants()).COMPONENT_ORIGINS.IMPORTED);

              case 7:
                importedComponents = _context9.sent;

                commitPendingComponents = commitPendingComponents.concat(importedComponents);

              case 9:
                commitPendingComponentsIds = commitPendingComponents.map(function (componentId) {
                  return (_bitId || _load_bitId()).BitId.parse(componentId);
                });
                _context9.next = 12;
                return this.scope.latestVersions(commitPendingComponentsIds, false);

              case 12:
                commitPendingComponentsLatest = _context9.sent;
                warnings = [];

                commitPendingComponentsLatest.forEach(function (componentId) {
                  if ((_semver || _load_semver()).default.gt(componentId.version, version)) {
                    warnings.push(`warning: ${componentId.toString()} has a version greater than ${version}`);
                  }
                });
                return _context9.abrupt('return', { commitPendingComponents, warnings });

              case 16:
              case 'end':
                return _context9.stop();
            }
          }
        }, _callee9, this);
      }));

      function listCommitPendingOfAllScope(_x5) {
        return _ref14.apply(this, arguments);
      }

      return listCommitPendingOfAllScope;
    }()

    /**
     * New and modified components are commit pending
     *
     * @return {Promise<string[]>}
     */

  }, {
    key: 'listCommitPendingComponents',
    value: function () {
      var _ref15 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee10() {
        var _ref16, _ref17, newComponents, modifiedComponents, modifiedComponentsWithoutScopeAndVersion;

        return (_regenerator || _load_regenerator()).default.wrap(function _callee10$(_context10) {
          while (1) {
            switch (_context10.prev = _context10.next) {
              case 0:
                _context10.next = 2;
                return Promise.all([this.listNewComponents(), this.listModifiedComponents()]);

              case 2:
                _ref16 = _context10.sent;
                _ref17 = _slicedToArray(_ref16, 2);
                newComponents = _ref17[0];
                modifiedComponents = _ref17[1];
                modifiedComponentsWithoutScopeAndVersion = modifiedComponents.map(function (componentId) {
                  return componentId.toStringWithoutScopeAndVersion();
                });
                return _context10.abrupt('return', [].concat(_toConsumableArray(newComponents), _toConsumableArray(modifiedComponentsWithoutScopeAndVersion)));

              case 8:
              case 'end':
                return _context10.stop();
            }
          }
        }, _callee10, this);
      }));

      function listCommitPendingComponents() {
        return _ref15.apply(this, arguments);
      }

      return listCommitPendingComponents;
    }()

    /**
     * Components from the model where the scope is local are pending for export
     * Also, components that their model version is higher than their bit.map version.
     * @return {Promise<string[]>}
     */

  }, {
    key: 'listExportPendingComponents',
    value: function () {
      var _ref18 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee11() {
        var _this3 = this;

        var load = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
        var idsFromObjects, ids;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee11$(_context11) {
          while (1) {
            switch (_context11.prev = _context11.next) {
              case 0:
                _context11.next = 2;
                return this.idsFromObjects();

              case 2:
                idsFromObjects = _context11.sent;
                _context11.next = 5;
                return (0, (_utils || _load_utils()).filterAsync)(idsFromObjects, function (componentId) {
                  return _this3.consumer.getComponentStatusById((_bitId || _load_bitId()).BitId.parse(componentId)).then(function (status) {
                    return status.staged;
                  });
                });

              case 5:
                ids = _context11.sent;

                if (load) {
                  _context11.next = 8;
                  break;
                }

                return _context11.abrupt('return', ids);

              case 8:
                return _context11.abrupt('return', Promise.all(ids.map(function (id) {
                  return _this3.scope.sources.get((_bitId || _load_bitId()).BitId.parse(id));
                })));

              case 9:
              case 'end':
                return _context11.stop();
            }
          }
        }, _callee11, this);
      }));

      function listExportPendingComponents() {
        return _ref18.apply(this, arguments);
      }

      return listExportPendingComponents;
    }()
  }, {
    key: 'listAutoTagPendingComponents',
    value: function () {
      var _ref19 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee12() {
        var modifiedComponents, modifiedComponentsLatestVersions;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee12$(_context12) {
          while (1) {
            switch (_context12.prev = _context12.next) {
              case 0:
                _context12.next = 2;
                return this.listModifiedComponents();

              case 2:
                modifiedComponents = _context12.sent;

                if (!(!modifiedComponents || !modifiedComponents.length)) {
                  _context12.next = 5;
                  break;
                }

                return _context12.abrupt('return', []);

              case 5:
                _context12.next = 7;
                return this.scope.latestVersions(modifiedComponents);

              case 7:
                modifiedComponentsLatestVersions = _context12.sent;
                return _context12.abrupt('return', this.consumer.listComponentsForAutoTagging(modifiedComponentsLatestVersions));

              case 9:
              case 'end':
                return _context12.stop();
            }
          }
        }, _callee12, this);
      }));

      function listAutoTagPendingComponents() {
        return _ref19.apply(this, arguments);
      }

      return listAutoTagPendingComponents;
    }()
  }, {
    key: 'idsFromBitMap',
    value: function () {
      var _ref20 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee13() {
        var withScopeName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
        var origin = arguments[1];
        var fromBitMap, ids;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee13$(_context13) {
          while (1) {
            switch (_context13.prev = _context13.next) {
              case 0:
                _context13.next = 2;
                return this.getFromBitMap(origin);

              case 2:
                fromBitMap = _context13.sent;
                ids = Object.keys(fromBitMap);

                if (!withScopeName) {
                  _context13.next = 6;
                  break;
                }

                return _context13.abrupt('return', ids);

              case 6:
                return _context13.abrupt('return', ids.map(function (id) {
                  return (_bitId || _load_bitId()).BitId.parse(id).toStringWithoutScopeAndVersion();
                }));

              case 7:
              case 'end':
                return _context13.stop();
            }
          }
        }, _callee13, this);
      }));

      function idsFromBitMap() {
        return _ref20.apply(this, arguments);
      }

      return idsFromBitMap;
    }()

    /**
     * Finds all components that are saved in the file system.
     * Components might be stored in the default component directory and also might be outside
     * of that directory. The bit.map is used to find them all
     * If they are on bit.map but not on the file-system, populate them to _deletedComponents property
     * @return {Promise<Component[]>}
     */

  }, {
    key: 'getFromFileSystem',
    value: function () {
      var _ref21 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee14(origin) {
        var cacheKeyName, idsFromBitMap, parsedBitIds, _ref22, components, deletedComponents;

        return (_regenerator || _load_regenerator()).default.wrap(function _callee14$(_context14) {
          while (1) {
            switch (_context14.prev = _context14.next) {
              case 0:
                cacheKeyName = origin || 'all';

                if (this._fromFileSystem[cacheKeyName]) {
                  _context14.next = 13;
                  break;
                }

                _context14.next = 4;
                return this.idsFromBitMap(true, origin);

              case 4:
                idsFromBitMap = _context14.sent;
                parsedBitIds = idsFromBitMap.map(function (id) {
                  return (_bitId || _load_bitId()).BitId.parse(id);
                });
                _context14.next = 8;
                return this.consumer.loadComponents(parsedBitIds, false);

              case 8:
                _ref22 = _context14.sent;
                components = _ref22.components;
                deletedComponents = _ref22.deletedComponents;

                this._fromFileSystem[cacheKeyName] = components;
                if (!this._deletedComponents && !origin) {
                  this._deletedComponents = deletedComponents;
                }

              case 13:
                return _context14.abrupt('return', this._fromFileSystem[cacheKeyName]);

              case 14:
              case 'end':
                return _context14.stop();
            }
          }
        }, _callee14, this);
      }));

      function getFromFileSystem(_x8) {
        return _ref21.apply(this, arguments);
      }

      return getFromFileSystem;
    }()

    /**
     * components that are on bit.map but not on the file-system
     */

  }, {
    key: 'listDeletedComponents',
    value: function () {
      var _ref23 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee15() {
        return (_regenerator || _load_regenerator()).default.wrap(function _callee15$(_context15) {
          while (1) {
            switch (_context15.prev = _context15.next) {
              case 0:
                if (this._deletedComponents) {
                  _context15.next = 3;
                  break;
                }

                _context15.next = 3;
                return this.getFromFileSystem();

              case 3:
                return _context15.abrupt('return', this._deletedComponents);

              case 4:
              case 'end':
                return _context15.stop();
            }
          }
        }, _callee15, this);
      }));

      function listDeletedComponents() {
        return _ref23.apply(this, arguments);
      }

      return listDeletedComponents;
    }()
  }, {
    key: 'getFromBitMap',
    value: function () {
      var _ref24 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee16(origin) {
        var cacheKeyName;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee16$(_context16) {
          while (1) {
            switch (_context16.prev = _context16.next) {
              case 0:
                cacheKeyName = origin || 'all';

                if (!this._fromBitMap[cacheKeyName]) {
                  this._fromBitMap[cacheKeyName] = this.bitMap.getAllComponents(origin);
                }
                return _context16.abrupt('return', this._fromBitMap[cacheKeyName]);

              case 3:
              case 'end':
                return _context16.stop();
            }
          }
        }, _callee16, this);
      }));

      function getFromBitMap(_x9) {
        return _ref24.apply(this, arguments);
      }

      return getFromBitMap;
    }()
  }], [{
    key: 'sortComponentsByName',
    value: function sortComponentsByName(components) {
      var getName = function getName(component) {
        var name = void 0;
        if ((_ramda || _load_ramda()).default.is((_models || _load_models()).Component, component)) name = component.id();else if ((_ramda || _load_ramda()).default.is((_component || _load_component()).default, component)) name = component.id.toString();else name = component;
        return name.toUpperCase(); // ignore upper and lowercase
      };
      return components.sort(function (a, b) {
        var nameA = getName(a);
        var nameB = getName(b);
        if (nameA < nameB) {
          return -1;
        }
        if (nameA > nameB) {
          return 1;
        }

        // names must be equal
        return 0;
      });
    }
  }]);

  return ComponentsList;
}();

exports.default = ComponentsList;