"use strict";

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

function _serverlessIndex() {
  const data = _interopRequireDefault(require("./serverless-index"));

  _serverlessIndex = function () {
    return data;
  };

  return data;
}

function _queryBuilder() {
  const data = _interopRequireDefault(require("./query-builder"));

  _queryBuilder = function () {
    return data;
  };

  return data;
}

const numOfResultsPerPage = 15; // function totalHits(index: Promise<any>, query: string) {
//   return new Promise((resolve, reject) => {
//     return index.then((indexInstance) => {
//       indexInstance.totalHits({
//         query: buildQuery(query)
//       }, (err, count) => {
//         if (err) reject(err);
//         resolve(JSON.stringify(count));
//       });
//     });
//   });
// }
// function countDocs(index: Promise<any>) {
//   return new Promise((resolve, reject) => {
//     return index.then((indexInstance) => {
//       indexInstance.countDocs((err, info) => {
//         if (err) reject(err);
//         resolve(info);
//       });
//     });
//   });
// }
// function getDoc(index: Promise<any>, docIds: string[]) {
//   return new Promise((resolve, reject) => {
//     return index.then((indexInstance) => {
//       indexInstance.get(docIds).on('data', function (doc) {
//         // console.log(doc);
//       });
//     });
//   });
// }

/**
 * Sort by the score. If the score is equal, sort by the length of the name.
 * @param {Array<any>} results
 * @return {Array<any>}
 */

function sortSearchResults(results) {
  return results.sort((a, b) => {
    if (a.score !== b.score) return a.score - b.score;
    return a.document.name.length - b.document.name.length;
  });
}

function formatter(doc) {
  // $FlowFixMe
  if (doc.owner && typeof doc.owner === 'string' && typeof doc.scope === 'string') {
    // from web search
    return `> ${doc.owner}.${doc.scope}/${doc.name}`;
  }

  return `> ${doc.name}`;
}
/**
 * Search in a local LevelUp index.
 *
 * @param {string} queryStr
 * @param {string} path
 * @return {Promise}
 */


function search(queryStr, path) {
  return new Promise(resolve => {
    const index = _serverlessIndex().default.initializeIndex(path);

    const searchResults = [];
    const query = (0, _queryBuilder().default)(queryStr);
    return index.then(indexInstance => {
      indexInstance.search({
        query,
        pageSize: numOfResultsPerPage
      }).on('data', function (data) {
        searchResults.push(data);
      }).on('end', function () {
        const searchResultsSorted = sortSearchResults(searchResults);
        return resolve(searchResultsSorted.map(result => result.document));
      });
    });
  });
}

module.exports = {
  search,
  formatter
};