// Generated by CoffeeScript 2.3.2
(function() {
  module.exports.facetCounts = function(request, settings, attributes0) {
    return function({aggregations}) {
      var attributes, reducer, ref, ref1, type;
      ({type} = request);
      attributes = (ref = attributes0 != null ? attributes0 : (ref1 = settings.types[type]) != null ? ref1.attributes : void 0) != null ? ref : [];
      reducer = function(counts, attr) {
        if ((attr.interpreteAggResult != null) && ((aggregations != null ? aggregations[attr.name] : void 0) != null)) {
          counts[attr.name] = attr.interpreteAggResult(aggregations[attr.name], request, settings);
        }
        return counts;
      };
      return attributes.reduce(reducer, {});
    };
  };

  module.exports.sections = function({type, query = {}}, settings, SortParser0) {
    return function({aggregations, hits}) {
      var SortParser, parseSortSpec, ref, ref1, ref2, ref3, sortSpec, sorter;
      SortParser = (ref = SortParser0 != null ? SortParser0 : settings.SortParser) != null ? ref : require("./sort-parser");
      parseSortSpec = SortParser((ref1 = (ref2 = settings.types) != null ? (ref3 = ref2[type]) != null ? ref3.sort : void 0 : void 0) != null ? ref1 : {});
      sortSpec = query != null ? query.sort : void 0;
      sorter = parseSortSpec(sortSpec);
      if ((sorter.interpreteAggResult != null) && ((aggregations != null ? aggregations._offsets : void 0) != null)) {
        return sorter.interpreteAggResult(aggregations._offsets, hits.total);
      }
    };
  };

  module.exports.suggestions = function(searchRequest, settings, suggestions0) {
    return function({suggest}) {
      var reducer, ref, ref1, suggestions;
      suggestions = (ref = suggestions0 != null ? suggestions0 : (ref1 = settings.types[searchRequest.type]) != null ? ref1.suggestions : void 0) != null ? ref : [];
      reducer = function(suggs, suggestor) {
        var input, v;
        input = suggest != null ? suggest[suggestor.name] : void 0;
        if (input != null) {
          v = suggestor.transform != null ? suggestor.transform(input, searchRequest, settings) : input;
          suggs[suggestor.name] = v;
        }
        return suggs;
      };
      return suggestions.reduce(reducer, {});
    };
  };

  module.exports.hits = function() {
    return function({hits}) {
      return hits.hits;
    };
  };

  module.exports._request = function() {
    return function({_request}) {
      return _request;
    };
  };

  module.exports._ast = function() {
    return function({_ast}) {
      return _ast;
    };
  };

  module.exports.offset = function({query}) {
    return function() {
      var ref;
      return (ref = query != null ? query.offset : void 0) != null ? ref : 0;
    };
  };

  module.exports.total = function() {
    return function({hits}) {
      return hits.total;
    };
  };

  module.exports.augmentHit = function(searchRequest, settings, attributes0, domainSpecificBehaviour0) {
    return function(hit) {
      var attr, attributes, domainSpecificBehaviour, dstHit, i, len, ref, ref1, ref2, ref3, type, typeName, x;
      typeName = (ref = (ref1 = hit._type) != null ? ref1 : searchRequest.type) != null ? ref : settings.defaultType;
      type = (ref2 = settings.types[typeName]) != null ? ref2 : {};
      attributes = (ref3 = attributes0 != null ? attributes0 : type.attributes) != null ? ref3 : [];
      domainSpecificBehaviour = domainSpecificBehaviour0 != null ? domainSpecificBehaviour0 : type.domainSpecificBehaviour;
      dstHit = {
        score: hit._score,
        data: {
          id: hit._id,
          type: typeName
        }
      };
      if (hit._explanation != null) {
        dstHit.explanation = hit._explanation;
      }
      if (hit.fields) {
        dstHit.fields = hit.fields;
      }
// apply attribute-specific augmentations
      for (i = 0, len = attributes.length; i < len; i++) {
        attr = attributes[i];
        if (!(attr.augmentHit != null)) {
          continue;
        }
        x = attr.augmentHit(hit, dstHit);
        x;
      }
      // apply domain-specific augmentations not related to any particular attribute
      if (domainSpecificBehaviour != null) {
        domainSpecificBehaviour.augmentHit(hit, dstHit);
      }
      return dstHit;
    };
  };

}).call(this);
