/**
 * @function task.worker._generateStyleguideOverview
 * @param {string} filename - File name to generate
 * @param {object} data - Render data.
 * @param {function} callback - Callback when done.
 * @protected
 * @ignore
 */
var fs = require('fs'),
    path = require('path'),
    file = require('../../lib/file'),
    loadHbsTmpl = file.loadHbsTmpl;

exports = module.exports = function (filename, data, callback) {
    exports._loadStyleguideTmpl(function (err, tmpl) {
        if (err) {
            callback(err);
            return;
        }
        var content = tmpl(exports._overviewData(data));
        fs.writeFile(filename, content, callback);
    });
};

exports._loadStyleguideTmpl = function (callback) {
    var filename = path.resolve(__dirname, '../../tmpl/md/styleguide.md.hbs');
    loadHbsTmpl(filename, callback);
};
exports._overviewData = function (data) {
    var result = global.apemandata;
    Object.keys(data).forEach(function (key) {
        result[key] = data[key];
    });
    return result;
};

