define(function (require) {
  return function sankeyProvider(Private, Notifier) {
    var _ = require('lodash');
    var arrayToLinkedList = require('ui/agg_response/hierarchical/_array_to_linked_list');
    var notify = new Notifier({
      location: 'Sankey chart response converter'
    });
    var nodes = {};
    var links = {};
    var lastNode = -1;

    function processEntry(aggConfig, metric, aggData, prevNode) {
      _.each(aggData.buckets, function (b) {
        if (isNaN(nodes[b.key])) {
          nodes[b.key] = lastNode + 1;
          lastNode = _.max(_.values(nodes));
        }
        if (aggConfig._previous) {
          var k = prevNode + 'sankeysplitchar' + nodes[b.key];
          if (isNaN(links[k])) {
            links[k] = metric.getValue(b);
          } else {
            links[k] += metric.getValue(b);
          }
        }
        if (aggConfig._next) {
          processEntry(aggConfig._next, metric, b[aggConfig._next.id], nodes[b.key]);
        }
      });
    }

    return function (vis, resp) {

      var metric = vis.aggs.bySchemaGroup.metrics[0];
      var buckets = vis.aggs.bySchemaGroup.buckets;
      buckets = arrayToLinkedList(buckets);
      if (!buckets)  {
        return {'slices':{'nodes':[],'links':[]}};
      }

      var firstAgg = buckets[0];
      var aggData = resp.aggregations[firstAgg.id];

      if (!firstAgg._next) {
        notify.error('need more than one sub aggs');
      }

      // reset these three variables, so that everything is fresh for refresh
      nodes = {};
      links = {};
      lastNode = -1;
    
      processEntry(firstAgg, metric, aggData, -1);

      var invertNodes = _.invert(nodes);
      var chart = {
        'slices': {
          'nodes' : _.map(_.keys(invertNodes), function (k) { return {'name':invertNodes[k]}; }),
          'links' : _.map(_.keys(links), function (k) {
            var s = k.split('sankeysplitchar');
            return {'source': parseInt(s[0]), 'target': parseInt(s[1]), 'value': links[k]};
          })
        }
      };

      return chart;
    };
  };
});

@@ -458,24 +458,78 @@
 ction (b) %7B%0A
+        var bkey = aggConfig.fieldFormatter()(b.key);%0A
         if (
@@ -537,25 +537,24 @@
 sNaN(nodes%5Bb
-.
 key%5D)) %7B%0A   
@@ -563,25 +563,24 @@
      nodes%5Bb
-.
 key%5D = lastN
@@ -730,25 +730,24 @@
 r' + nodes%5Bb
-.
 key%5D;%0A      
@@ -1010,17 +1010,16 @@
  nodes%5Bb
-.
 key%5D);%0A 

use fieldFormatter()