/**
 * Evaluate a data
 * @function ci.worker.evaluateData
 * @param {object} data - Data to evaluate.
 * @returns {object} - Evaluated data.
 * @example
 *
 *  var data = evaluateData({
 *      foo: 'bar',
 *      baz: '${foo}/quz'
 *  });
 * // data.baz will be 'bar/quz'
 *
 * @protected
 * @ignore
 * @author Taka Okunishi
 */
module.exports = function (data) {
    var result = {};
    with (data) {
        Object.keys(data).sort().forEach(function (key) {
            function evaluate(value) {
                if (typeof(value) === 'string') {
                    var pattern = /(#\{)(.*?)(\})/g;
                    value = value.replace(pattern, function ($0, $1, $2) {
                        return eval($2);
                    });
                    if (!!value.match(pattern)) value = evaluate(value);
                }
                return value;
            }

            result[key] = evaluate(data[key]);
        });
    }
    return result;
};