// Generated by CoffeeScript 2.3.2
(function() {
  var Attribute, ConfigNode, extend;

  extend = require("deep-extend");

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

  module.exports = Attribute = (function() {
    var prefix;

    class Attribute extends ConfigNode {
      constructor(options) {
        var ref, ref1;
        super(options);
        // The `field` option defines what index field is backing this attribute.
        // If `field` contains dots (`.`), it will be interpreted
        // as a path of dot-separated attribute names and traversed recursively.
        this.field = options.field;
        this.name = (ref = options.name) != null ? ref : options.field;
        // The `source` option defines how the field value is extracted from
        // the document source when displaying search result entries.
        // By default, it will look at the `field` option and extract
        // the corresponding attribute from the document.
        if (this.source == null) {
          this.source = (ref1 = options.source) != null ? ref1 : function(src) {
            return this.field.split('.').reduce((function(prev, cur) {
              return prev != null ? prev[cur] : void 0;
            }), src);
          };
        }
        if (this._options.augmentHit != null) {
          if (this.augmentHit == null) {
            this.augmentHit = function(src, dst) {
              var augmentation, f, input;
              f = this._options.augmentHit;
              input = this.renderResult(src);
              augmentation = f.call(this, input, src, dst);
              if (f.length < 3) { // the callback did not explicitly deal with dst
                switch (typeof augmentation) {
                  case 'function':
                    return augmentation(dst);
                  case 'object':
                    return extend(dst, augmentation);
                  default:
                    throw new Error('duh?!');
                }
              }
            };
          }
        }
      }

      findHighlightedMatches(hit) {
        var candidate, candidateField, candidateFields, i, j, len1, len2, ref, ref1, subfield;
        candidateFields = [this.field];
        if (this.highlightSubfields != null) {
          ref = Object.keys(this.highlightSubfields());
          for (i = 0, len1 = ref.length; i < len1; i++) {
            subfield = ref[i];
            candidateFields.push(this.field + "." + subfield);
          }
        }
        for (j = 0, len2 = candidateFields.length; j < len2; j++) {
          candidateField = candidateFields[j];
          candidate = (ref1 = hit.highlight) != null ? ref1[candidateField] : void 0;
          if (candidate != null) {
            return candidate;
          }
        }
      }

      renderResult(hit) {
        var ref, ref1, ref2;
        return (ref = (ref1 = this.findHighlightedMatches(hit)) != null ? ref1.join(" … ") : void 0) != null ? ref : prefix(this.source(hit._source), (ref2 = this._options.teaserLength) != null ? ref2 : 150);
      }

    };

    prefix = function(input, len) {
      var m;
      if (input == null) {
        return null;
      }
      if (typeof input !== "string") {
        return input;
      }
      input = input.trim();
      if (input.length <= len) {
        return input;
      } else {
        m = input.match(RegExp(`^.{1,${len}}\\b`));
        if (m) {
          return m[0].trim() + " …";
        } else {
          return input;
        }
      }
    };

    return Attribute;

  }).call(this);

}).call(this);
