'use strict';

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

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 _scopeLoader;

function _load_scopeLoader() {
  return _scopeLoader = _interopRequireDefault(require('../../scope-loader'));
}

var _scope;

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

var _scope2;

function _load_scope2() {
  return _scope2 = require('../../../api/scope');
}

var _componentObjects;

function _load_componentObjects() {
  return _componentObjects = _interopRequireDefault(require('../../component-objects'));
}

var _bitId;

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

var _exceptions;

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

var _utils;

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

var _search;

function _load_search() {
  return _search = require('../../../search');
}

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

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

var Fs = function () {
  function Fs(scopePath) {
    _classCallCheck(this, Fs);

    this.scopePath = scopePath;
  }

  _createClass(Fs, [{
    key: 'close',
    value: function close() {
      return this;
    }
  }, {
    key: 'getScope',
    value: function getScope() {
      if (!this.scope) throw new (_exceptions || _load_exceptions()).FsScopeNotLoaded();
      return this.scope;
    }
  }, {
    key: 'describeScope',
    value: function describeScope() {
      return Promise.resolve(this.getScope().describe());
    }
  }, {
    key: 'push',
    value: function push(componentObjects) {
      return this.pushMany([componentObjects]);
    }
  }, {
    key: 'pushMany',
    value: function pushMany(components) {
      return (0, (_scope2 || _load_scope2()).put)({ path: this.scopePath, componentObjects: components });
    }
  }, {
    key: 'deleteMany',
    value: function deleteMany(bitIds, force) {
      return (0, (_scope2 || _load_scope2()).remove)({ path: this.scopePath, ids: bitIds, force });
    }
  }, {
    key: 'deprecateMany',
    value: function deprecateMany(bitIds) {
      return (0, (_scope2 || _load_scope2()).deprecate)({ path: this.scopePath, ids: bitIds });
    }
  }, {
    key: 'fetch',
    value: function fetch(bitIds) {
      var noDependencies = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

      return (0, (_scope2 || _load_scope2()).fetch)(this.scopePath, bitIds, noDependencies).then(function (bitsMatrix) {
        if (noDependencies) return bitsMatrix;
        return (0, (_utils || _load_utils()).flatten)(bitsMatrix);
      });
    }
  }, {
    key: 'latestVersions',
    value: function latestVersions(componentIds) {
      return this.getScope().latestVersions(componentIds).then(function (componentsIds) {
        return componentsIds.map(function (componentId) {
          return componentId.toString();
        });
      });
    }
  }, {
    key: 'list',
    value: function list() {
      return this.getScope().listStage();
    }
  }, {
    key: 'search',
    value: function search(query, reindex) {
      return (_search || _load_search()).searchAdapter.scopeSearch(this.scopePath, query, reindex);
    }
  }, {
    key: 'show',
    value: function show(bitId) {
      return this.getScope().loadComponent(bitId);
    }
  }, {
    key: 'connect',
    value: function connect() {
      var _this = this;

      return (0, (_scopeLoader || _load_scopeLoader()).default)(this.scopePath).then(function (scope) {
        _this.scope = scope;
        return _this;
      });
    }
  }]);

  return Fs;
}();

exports.default = Fs;