"use strict";
var _ = require('../lodash.custom');
var debug = require('debug')('cb:task.tree.transform');
function applyBooleanPropertyToChildren(tasks, skipped) {
    tasks.forEach(function (task) {
        if (skipped) {
            task.skipped = true;
        }
        if (task.skipped) {
            if (task.tasks.length) {
                applyBooleanPropertyToChildren(task.tasks, true);
                return;
            }
        }
        if (task.tasks.length) {
            applyBooleanPropertyToChildren(task.tasks, false);
        }
    });
}
exports.transforms = {
    'Add skipped property to children': {
        predicate: function (tasks) {
            return true;
        },
        fn: function (tasks) {
            applyBooleanPropertyToChildren(tasks, false);
            return tasks;
        }
    },
    'Add if property to children': {
        predicate: function (tasks) {
            return true;
        },
        fn: function (tasks) {
            applyBooleanPropertyToChildren(tasks, false, 'ifChanged', '');
            function applyBooleanPropertyToChildren(tasks, add, property, value) {
                tasks.forEach(function (task) {
                    if (add) {
                        task[property].unshift.apply(task[property], value);
                    }
                    if (task[property].length) {
                        if (task.tasks.length) {
                            applyBooleanPropertyToChildren(task.tasks, true, property, task[property]);
                            return;
                        }
                    }
                    if (task.tasks.length) {
                        applyBooleanPropertyToChildren(task.tasks, false, property);
                    }
                });
            }
            return tasks;
        }
    }
};
/**
 * Allow transformations on tasks before error collections
 */
function applyTreeTransforms(incoming) {
    return Object.keys(exports.transforms).reduce(function (task, key) {
        var transform = exports.transforms[key];
        if (transform.predicate(task)) {
            debug("Applying transform " + key);
            return transform.fn(task);
        }
        return incoming;
    }, incoming);
}
exports.applyTreeTransforms = applyTreeTransforms;
//# sourceMappingURL=task.tree.transforms.js.map