// Generated by CoffeeScript 2.3.2
(function() {
  var ConfigNode, ElasticSearch, EsAdapter, Promise, Yaml, fs, merge, path;

  ({ConfigNode} = require("@l.degener/irma-config"));

  ElasticSearch = require("elasticsearch");

  Yaml = require("js-yaml");

  fs = require("fs");

  path = require("path");

  Promise = require("bluebird");

  merge = require("deepmerge");

  module.exports = EsAdapter = class EsAdapter extends ConfigNode {
    dependencies() {
      return {
        io: ["/", "io"],
        //FIXME: remove this dependency, we only need it to provide legacy api
        types: ["/", "types"]
      };
    }

    init({types, io}) {
      var client, console, debug, debugSettings, defaultIndex, defaultType, host, keepAlive, legacyFetch, port;
      ({console} = io);
      ({
        debug: debugSettings,
        host,
        port,
        keepAlive,
        index: defaultIndex,
        defaultType
      } = this._options);
      debug = function(key, msg) {
        if ((debugSettings === true) || (typeof debugSettings === "object") && (debugSettings != null ? debugSettings[key] : void 0)) {
          return console.log(key, msg);
        }
      };
      client = this.client = new ElasticSearch.Client({
        host: `${host}:${port}`,
        keepAlive: keepAlive
      });
      this.get = function(req, settings) {
        return function({id, typeName = defaultType}) {
          var body, ref, ref1;
          body = {
            index: (ref = (ref1 = settings.types[typeName]) != null ? ref1.index : void 0) != null ? ref : defaultIndex,
            type: typeName,
            id: id
          };
          return client.get(body);
        };
      };
      legacyFetch = function(id, typeName) {
        return this.get(null, {types})({id, typeName});
      };
      Object.defineProperty(this, "fetch", {
        writeable: true,
        configurable: true,
        enumerable: true,
        get: function() {
          console.warn("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\nWARNING: es-helper.fetch(id, typeName) is deprecated and *will be removed*.\n\nYou should use es-helper.get(request,settings)({id, typeName}) instead.\nAlternatively, you can use the build-in plugin `legacy-fetch` which will\nprovide the legacy API.\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
          delete this.fetch;
          this.fetch = legacyFetch;
          return this.fetch;
        },
        set: function(customFetch) {
          delete this.fetch;
          return this.fetch = customFetch;
        }
      });
      this.search = function(req, settings) {
        return function(searchReq0) {
          var explain, index, ref, ref1, ref2, searchReq, typeName;
          typeName = (ref = searchReq0.type) != null ? ref : defaultType;
          index = (ref1 = (ref2 = settings.types[typeName]) != null ? ref2.index : void 0) != null ? ref1 : defaultIndex;
          searchReq = merge(searchReq0, {
            index: index
          });
          ({
            body: {explain = false} = {}
          } = searchReq);
          debug("searchReq", JSON.stringify(searchReq, null, "  "));
          return client.search(searchReq).then(function(resp) {
            debug("resp", JSON.stringify(resp, null, "  "));
            if (explain) {
              resp._request = searchReq;
            }
            if (explain) {
              resp._ast = ast;
            }
            return resp;
          });
        };
      };
      this.analyze = function(req, settings) {
        return function({
            field,
            text,
            type: typeName = defaultType
          }) {
          var index, ref, ref1;
          index = (ref = (ref1 = settings.types[typeName]) != null ? ref1.index : void 0) != null ? ref : defaultIndex;
          return client.indices.analyze({index, text, field});
        };
      };
      return this.random = function(req, settings) {
        return function({
            type: typeName = defaultType,
            query,
            seed = Date.now()
          }) {
          var index, ref, ref1;
          index = (ref = (ref1 = settings.types[typeName]) != null ? ref1.index : void 0) != null ? ref : defaultIndex;
          return client.search({
            index: index,
            type: typeName,
            size: 1,
            from: 0,
            body: {
              query: {
                function_score: {
                  query: query,
                  functions: [
                    {
                      random_score: {
                        seed: seed
                      }
                    }
                  ]
                }
              }
            }
          }).then(function(resp) {
            return resp.hits.hits[0]._source;
          });
        };
      };
    }

  };

}).call(this);
