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

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

  module.exports = CsvView = class CsvView extends ConfigNode {
    render({types = {}}, {type}) {
      return ({hits}) => {
        var attributes, headers, ref;
        attributes = types != null ? (ref = types[type]) != null ? ref.attributes : void 0 : void 0;
        headers = attributes.map(function({name}) {
          return name;
        });
        return {
          headers: {
            "Content-Disposition": "attachment; filename=\"export.csv\""
          },
          mimeType: "text/csv",
          data: [headers].concat(hits.map((hit) => {
            return attributes.map(function(attr) {
              var source;
              source = attr.source(hit._source);
              if (source == null) {
                return null;
              }
              if (attr.options.export) {
                return attr.options.export(source);
              } else {
                return source.toString();
              }
            });
          }))
        };
      };
    }

  };

}).call(this);
