var util = require('util');
var _ = require('lodash');

var app = {};

/**
 * Filter
 * Cleanup the data, e.g.: remove double fields, happen when overwrite a global inherited field with a local definition.
 *
 * @param {Object} _app
 */
function Filter(_app) {
    var self = this;

    // global variables
    app = _app;

    // class variables
    this.filters = {};

    // load filters
    var filters = Object.keys(app.filters);
    filters.forEach(function(filter) {
        if (_.isObject( app.filters[filter] )) {
            app.log.debug('inject filter: ' + filter);
            self.addFilter(filter, app.filters[filter] );
        } else {
            var filename = app.filters[filter];
            app.log.debug('load filter: ' + filter + ', ' + filename);
            self.addFilter(filter, require(filename));
        }
    });
}

/**
 * Inherit
 */
util.inherits(Filter, Object);

/**
 * Exports
 */
module.exports = Filter;

/**
 * Add Filter
 */
Filter.prototype.addFilter = function(name, filter) {
    this.filters[name] = filter;
};

/**
 * Execute filter
 */
Filter.prototype.process = function(parsedFiles, parsedFilenames) {
    // filter each @api-Parameter
    _.each(this.filters, function(filter, name) {
        if (filter.postFilter) {
            app.log.verbose('filter postFilter: ' + name);
            filter.postFilter(parsedFiles, parsedFilenames);
        }
    });

    // reduce to local blocks where global is empty
    var blocks = [];
    parsedFiles.forEach(function(parsedFile) {
        parsedFile.forEach(function(block) {
            if (Object.keys(block.global).length === 0 && Object.keys(block.local).length > 0)
                blocks.push(block.local);
        });
    });
    return blocks;
};
