"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _scopeLoader() {
  const data = _interopRequireDefault(require("../../scope-loader"));

  _scopeLoader = function () {
    return data;
  };

  return data;
}

function _scope() {
  const data = require("../../../api/scope");

  _scope = function () {
    return data;
  };

  return data;
}

function _componentObjects() {
  const data = _interopRequireDefault(require("../../component-objects"));

  _componentObjects = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = require("../../../bit-id");

  _bitId = function () {
    return data;
  };

  return data;
}

function _exceptions() {
  const data = require("../exceptions");

  _exceptions = function () {
    return data;
  };

  return data;
}

function _utils() {
  const data = require("../../../utils");

  _utils = function () {
    return data;
  };

  return data;
}

function _search() {
  const data = require("../../../search");

  _search = function () {
    return data;
  };

  return data;
}

function _componentsList() {
  const data = _interopRequireDefault(require("../../../consumer/component/components-list"));

  _componentsList = function () {
    return data;
  };

  return data;
}

function _scopeComponentsImporter() {
  const data = _interopRequireDefault(require("../../component-ops/scope-components-importer"));

  _scopeComponentsImporter = function () {
    return data;
  };

  return data;
}

class Fs {
  constructor(scopePath) {
    this.scopePath = scopePath;
  }

  close() {
    return this;
  }

  getScope() {
    if (!this.scope) throw new (_exceptions().FsScopeNotLoaded)();
    return this.scope;
  }

  describeScope() {
    return Promise.resolve(this.getScope().describe());
  }

  push(componentObjects) {
    return this.pushMany([componentObjects]);
  }

  pushMany(components) {
    return (0, _scope().put)({
      path: this.scopePath,
      componentObjects: components
    });
  }

  deleteMany(ids, force) {
    return (0, _scope().remove)({
      path: this.scopePath,
      ids,
      force
    });
  }

  deprecateMany(ids) {
    return (0, _scope().deprecate)({
      path: this.scopePath,
      ids
    });
  }

  undeprecateMany(ids) {
    return (0, _scope().undeprecate)({
      path: this.scopePath,
      ids
    });
  }

  fetch(bitIds, noDependencies = false) {
    const idsStr = bitIds.serialize();
    return (0, _scope().fetch)(this.scopePath, idsStr, noDependencies).then(bitsMatrix => {
      if (noDependencies) return bitsMatrix;
      return (0, _utils().flatten)(bitsMatrix);
    });
  }

  latestVersions(componentIds) {
    return this.getScope().latestVersions(componentIds).then(componentsIds => componentsIds.map(componentId => componentId.toString()));
  }

  list(namespacesUsingWildcards) {
    return _componentsList().default.listLocalScope(this.getScope(), namespacesUsingWildcards);
  }

  search(query, reindex) {
    return _search().searchAdapter.scopeSearch(this.scopePath, query, reindex);
  }

  show(bitId) {
    const scopeComponentsImporter = _scopeComponentsImporter().default.getInstance(this.getScope());

    return scopeComponentsImporter.loadComponent(bitId);
  }

  connect() {
    return (0, _scopeLoader().default)(this.scopePath).then(scope => {
      this.scope = scope;
      return this;
    });
  }

}

exports.default = Fs;