<commit_before>var _ = require("lodash");
var λ = require("contra");
var applyInFiber = require("./applyInFiber");
var noopTrue = function(){
  return true;
};

var doPrelude = function(rule, ctx, callback){
  if(!_.isFunction(rule.prelude)){
    callback();
    return;
  }
  applyInFiber(rule.prelude, null, [ctx], callback);
};

var doActions = function(rule, ctx, callback){
  var condition = _.get(rule, ["action_block", "condition"], noopTrue);
  applyInFiber(condition, null, [ctx], function(err, cond){
    if(err) return callback(err);
    if(!cond){
      return callback();
    }
    var actions = _.get(rule, ["action_block", "actions"], []);
    λ.map(actions, function(action, done){
      applyInFiber(action, null, [ctx], done);
    }, callback);
  });
};

var doPostlude = function(rule, ctx, did_fire){
  var getPostFn = function(name){
    var fn = _.get(rule, ["postlude", name]);
    return _.isFunction(fn) ? fn : _.noop;
  };
  if(did_fire){
    getPostFn("fired")(ctx);
  }else{
    getPostFn("notfired")(ctx);
  }
  getPostFn("always")(ctx);
};

module.exports = function(rule, ctx, callback){

  doPrelude(rule, ctx, function(err, new_vars){
    if(err) return callback(err);

    doActions(rule, ctx, function(err, responses){
      //TODO collect errors and respond individually to the client
      if(err) return callback(err);

      var did_fire = false;

      //TODO handle more than one response type
      var resp_data = _.compact(_.map(responses, function(response){
        if((response === void 0) || (response === null)){
          return;//noop
        }
        did_fire = true;
        return {
          type: "directive",
          options: response.options,
          name: response.name,
          meta: {
            rid: rule.rid,
            rule_name: rule.rule_name,
            txn_id: "TODO",//TODO transactions<commit_msg>fixing did_fire<commit_after>var _ = require("lodash");
var λ = require("contra");
var applyInFiber = require("./applyInFiber");
var noopTrue = function(){
  return true;
};

var doPrelude = function(rule, ctx, callback){
  if(!_.isFunction(rule.prelude)){
    callback();
    return;
  }
  applyInFiber(rule.prelude, null, [ctx], callback);
};

var doActions = function(rule, ctx, callback){
  var condition = _.get(rule, ["action_block", "condition"], noopTrue);
  applyInFiber(condition, null, [ctx], function(err, cond){
    if(err) return callback(err);
    if(!cond){
      return callback();
    }
    var actions = _.get(rule, ["action_block", "actions"], []);
    λ.map(actions, function(action, done){
      applyInFiber(action, null, [ctx], done);
    }, function(err, responses){
      callback(err, responses, true);
    });
  });
};

var doPostlude = function(rule, ctx, did_fire){
  var getPostFn = function(name){
    var fn = _.get(rule, ["postlude", name]);
    return _.isFunction(fn) ? fn : _.noop;
  };
  if(did_fire){
    getPostFn("fired")(ctx);
  }else{
    getPostFn("notfired")(ctx);
  }
  getPostFn("always")(ctx);
};

module.exports = function(rule, ctx, callback){

  doPrelude(rule, ctx, function(err, new_vars){
    if(err) return callback(err);

    doActions(rule, ctx, function(err, responses, did_fire){
      //TODO collect errors and respond individually to the client
      if(err) return callback(err);


      //TODO handle more than one response type
      var resp_data = _.compact(_.map(responses, function(response){
        if((response === void 0) || (response === null)){
          return;//noop
        }
        return {
          type: "directive",
          options: response.options,
          name: response.name,
          meta: {
            rid: rule.rid,
            rule_name: rule.rule_name,
            txn_id: "TODO",//TODO transactions
{'fraction_non_alphanumeric': 0.15765765765765766, 'fraction_numerical': 0.0005299417064122947, 'mean_word_length': 3.134720700985761, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5146218', 'n_tokens_mistral': 1229, 'n_tokens_neox': 1204, 'n_words': 292}