// Generated by CoffeeScript 2.3.2
(function() {
  var ConfigNode, MultiMatchQuery, Transformer, defaultParser, isArray, parse;

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

  ({parse} = require("@l.degener/irma-query-syntax"));

  defaultParser = {parse};

  Transformer = require("./ast-transformer");

  ({isArray} = require("util"));

  module.exports = MultiMatchQuery = class MultiMatchQuery extends ConfigNode {
    dependencies() {
      return {
        io: ['/', 'io']
      };
    }

    init({io}) {
      var _options, createEsQuery;
      //FIXME: issue a deprecation warning if a custom transformer is used.
      if (this._options.transformer != null) {
        throw new Error("ERROR:  multi-match-query: overriding the default AST transformer is not possible any more.\n        The AST-Transformer is not considered public API and will be removed.\n        All reasonyble settings of the transformer can be accessed through the\n        multi-match-query settings instead.  If you need to go beyond that, consider\n        writing your own custom query.");
      }
      if ((this._options.postProcess != null) || (this._options.postprocess != null)) {
        throw new Error("ERROR:  Don't try to add a post-processing step here! Do it in a separate request filter.");
      }
      _options = this._options;
      createEsQuery = function(query, type, attributes0) {
        var ast, attributes, parser, queryString, ref, ref1, ref2, transformer;
        parser = (ref = _options.parser) != null ? ref : defaultParser;
        queryString = query.q;
        attributes = (ref1 = attributes0 != null ? attributes0 : type.attributes) != null ? ref1 : [];
        if ((queryString != null) && queryString.trim().length > 0) {
          ast = parser.parse(queryString);
        } else {
          ast = [];
        }
        transformer = (ref2 = _options.transformer) != null ? ref2 : new Transformer({
          fields: _options.fields,
          fieldQualifiers: _options.fieldQualifiers,
          customize: _options.customize,
          defaultOperator: _options.defaultOperator
        });
        return transformer.transform(ast, attributes, query);
      };
      this.create = function(query, type, attributes) {
        var body;
        body = createEsQuery(query, type, attributes);
        if (Object.keys(body).length === 0) {
          return {};
        } else {
          return {
            bool: {
              must: [body]
            }
          };
        }
      };
      return this.apply = function({
          query,
          type: typeName
        }, settings, attributes) {
        var body, ref, type;
        type = settings != null ? (ref = settings.types) != null ? ref[typeName] : void 0 : void 0;
        body = createEsQuery(query, type, attributes);
        if (Object.keys(body).length === 0) {
          return {
            match_all: {}
          };
        } else {
          return body;
        }
      };
    }

  };

}).call(this);
