// Generated by CoffeeScript 2.3.2
(function() {
  var ConfigNode, FilterQuery, createFilters, isArray;

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

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

  createFilters = function(query, type, attributes0) {
    var attr, attrs, elm, filters, i, j, len, len1, ref, value;
    attrs = (ref = attributes0 != null ? attributes0 : type != null ? type.attributes : void 0) != null ? ref : [];
    filters = [];
    for (i = 0, len = attrs.length; i < len; i++) {
      attr = attrs[i];
      if (!(attr.filter != null)) {
        continue;
      }
      value = query[attr != null ? attr.name : void 0];
      if (value == null) {
        continue;
      }
      if (!isArray(value)) {
        value = [value];
      }
      for (j = 0, len1 = value.length; j < len1; j++) {
        elm = value[j];
        filters.push(attr.filter(elm));
      }
    }
    return filters;
  };

  module.exports = FilterQuery = class FilterQuery extends ConfigNode {
    // legacy api
    create(query, type, attributes0) {
      var filters;
      filters = createFilters(query, type, attributes0);
      if (filters.length === 0) {
        return {};
      } else {
        return {
          bool: {
            filter: filters
          }
        };
      }
    }

    // new api
    apply({
        query,
        type: typeName
      }, settings, attributes) {
      var filters, ref, type;
      type = settings != null ? (ref = settings.types) != null ? ref[typeName] : void 0 : void 0;
      filters = createFilters(query, type, attributes);
      if (filters.length === 0) {
        return {
          match_all: {}
        };
      } else {
        return filters;
      }
    }

  };

}).call(this);
