"use strict";

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _indexer() {
  const data = _interopRequireDefault(require("./indexer"));

  _indexer = function () {
    return data;
  };

  return data;
}

function _searcher() {
  const data = require("./searcher");

  _searcher = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

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

  _scope = function () {
    return data;
  };

  return data;
}

function _scopeRemotes() {
  const data = require("../scope/scope-remotes");

  _scopeRemotes = function () {
    return data;
  };

  return data;
}

// import serverlessIndex from './serverless-index';
function searchLocally(_x) {
  return _searchLocally.apply(this, arguments);
}

function _searchLocally() {
  _searchLocally = (0, _bluebird().coroutine)(function* (queryStr, reindex = false) {
    let scopePath;

    if (reindex) {
      return (0, _consumer().loadConsumer)().then(consumer => {
        scopePath = consumer.scope.path;
        return consumer.scope.listLocal();
      }).then(components => {
        return _indexer().default.indexAll(scopePath, components);
      }).then(() => (0, _searcher().search)(queryStr, scopePath)); // .catch(Promise.reject);
    }

    return (0, _consumer().loadConsumer)().then(consumer => {
      scopePath = consumer.scope.path;
      return (0, _searcher().search)(queryStr, scopePath);
    });
  });
  return _searchLocally.apply(this, arguments);
}

function searchRemotely(_x2, _x3) {
  return _searchRemotely.apply(this, arguments);
}

function _searchRemotely() {
  _searchRemotely = (0, _bluebird().coroutine)(function* (queryStr, scope, reindex = false) {
    return (0, _consumer().loadConsumer)().then(consumer => {
      return (0, _scopeRemotes().getScopeRemotes)(consumer.scope).then(remotes => remotes.resolve(scope, consumer.scope.name).then(remote => {
        return remote.search(queryStr, reindex);
      }));
    }); // .catch(Promise.reject);
  });
  return _searchRemotely.apply(this, arguments);
}

function scopeSearch(_x4, _x5, _x6) {
  return _scopeSearch.apply(this, arguments);
}

function _scopeSearch() {
  _scopeSearch = (0, _bluebird().coroutine)(function* (path, query, reindex) {
    if (reindex) {
      return (0, _scope().loadScope)(path).then(scope => {
        return scope.listLocal();
      }).then(components => {
        return _indexer().default.indexAll(path, components);
      }).then(() => (0, _searcher().search)(query, path)); // .catch(Promise.reject);
    }

    return (0, _searcher().search)(query, path);
  });
  return _scopeSearch.apply(this, arguments);
}

module.exports = {
  searchLocally,
  searchRemotely,
  scopeSearch
};