'use strict';

var _regenerator;

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

var searchLocally = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(queryStr) {
    var reindex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    var scopePath;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            scopePath = void 0;

            if (!reindex) {
              _context.next = 3;
              break;
            }

            return _context.abrupt('return', (0, (_consumer || _load_consumer()).loadConsumer)().then(function (consumer) {
              scopePath = consumer.scope.path;
              return consumer.scope.listStage();
            }).then(function (components) {
              return (_indexer || _load_indexer()).default.indexAll(scopePath, components);
            }).then(function () {
              return (0, (_searcher || _load_searcher()).search)(queryStr, scopePath);
            }));

          case 3:
            return _context.abrupt('return', (0, (_consumer || _load_consumer()).loadConsumer)().then(function (consumer) {
              scopePath = consumer.scope.path;
              return (0, (_searcher || _load_searcher()).search)(queryStr, scopePath);
            }));

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

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

var searchRemotely = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(queryStr, scope) {
    var reindex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt('return', (0, (_consumer || _load_consumer()).loadConsumer)().then(function (consumer) {
              return consumer.scope.remotes().then(function (remotes) {
                return remotes.resolve(scope, consumer.scope.name).then(function (remote) {
                  return remote.search(queryStr, reindex);
                });
              });
            }));

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

  return function searchRemotely(_x4, _x5) {
    return _ref2.apply(this, arguments);
  };
}();

var scopeSearch = function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(path, query, reindex) {
    return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            if (!reindex) {
              _context3.next = 2;
              break;
            }

            return _context3.abrupt('return', (0, (_scope || _load_scope()).loadScope)(path).then(function (scope) {
              return scope.listStage();
            }).then(function (components) {
              return (_indexer || _load_indexer()).default.indexAll(path, components);
            }).then(function () {
              return (0, (_searcher || _load_searcher()).search)(query, path);
            }));

          case 2:
            return _context3.abrupt('return', (0, (_searcher || _load_searcher()).search)(query, path));

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

  return function scopeSearch(_x6, _x7, _x8) {
    return _ref3.apply(this, arguments);
  };
}();

var _indexer;

function _load_indexer() {
  return _indexer = _interopRequireDefault(require('./indexer'));
}

var _searcher;

function _load_searcher() {
  return _searcher = require('./searcher');
}

var _consumer;

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

var _scope;

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

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"); }); }; }
// import serverlessIndex from './serverless-index';


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