var LessError = require('../less-error'),

tree = require("../tree");

var FunctionImporter = module.exports = function FunctionImporter(context, fileInfo) {

this.fileInfo = fileInfo;

};

FunctionImporter.prototype.eval = function(contents, callback) {

var loaded = {},
    loader,
    registry;

registry = {
    add: function(name, func) {
        loaded[name] = func;
    },
    addMultiple: function(functions) {
        Object.keys(functions).forEach(function(name) {
            loaded[name] = functions[name];
        });
    }
};

try {
    loader = new Function("functions", "tree", "fileInfo", contents);
    loader(registry, tree, this.fileInfo);
} catch(e) {
    callback(new LessError({
        message: "Plugin evaluation error: '" + e.name + ': ' + e.message.replace(/["]/g, "'") + "'" ,
        filename: this.fileInfo.filename
    }), null );
}

callback(null, { functions: loaded });

};