import Transform from '../Transform';
import {inherits} from '../util/Functions';

/**
 * Applies a function to a data tuple and stores the result.
 * @constructor
 * @param {object} params - The parameters for this operator.
 * @param {function(object): *} params.apply - The function to apply to each tuple.
 * @param {string} params.as - the field name under which to store the result.
 */
export default function Apply(params) {
  Transform.call(this, null, params);
}

var prototype = inherits(Apply, Transform);

prototype.transform = function(_, pulse) {
  var func = _.apply,
      field = _.as,
      flags;

  function set(t) {
    t[field] = func(t, _);
  }

  if (_.modified()) {
    // parameters updated, need to reflow
    pulse
      .materialize()
      .visit(pulse.ADD_MOD, set)
      .visit(pulse.REFLOW, function(t) { set(t); pulse.mod.push(t); });
  } else {
    flags = pulse.ADD | (pulse.modified(func.fields) ? pulse.MOD : 0);
    pulse.visit(flags, set);
  }

  return pulse.modifies(field);
};

@@ -334,17 +334,17 @@
 ms.as - 
-t
+T
 he field
@@ -603,21 +603,19 @@
 ,%0A      
-flags
+mod
 ;%0A%0A  fun
@@ -734,23 +734,16 @@
    pulse
-%0A      
 .materia
@@ -752,150 +752,65 @@
 ze()
-%0A      .visit(pulse.ADD_MOD, set)%0A      .visit(pulse.REFLOW, function(t) %7B set(t); pulse.mod.push(t); %7D);%0A  %7D else %7B%0A    flags = pulse.ADD %7C (
+.reflow().visit(pulse.SOURCE, set);%0A  %7D else %7B%0A    mod = 
 puls
@@ -832,16 +832,37 @@
 .fields)
+;%0A    pulse.visit(mod
  ? pulse
@@ -866,39 +866,27 @@
 lse.
+ADD_
 MOD : 
-0);%0A    pulse.visit(flags
+pulse.ADD
 , se

Clean up Apply transform.