/**
 * Compile coffee files in directory.
 * @function task.worker.compileCoffeeDirectory
 * @param {object} config - Work configuration.
 * @param {string} config.srcDir - Source coffee directory.
 * @param {string} config.ignore - Ignore  file pattern. Glob.
 * @param {string} config.pattern - Source coffee file pattern. Glob.
 * @param {string} config.destDir - Destination js directory.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */

var path = require('path'),
    debug = require('./_debug'),
    compile = require('../../lib/compile'),
    lib = require('../../lib'),
    file = lib.file,
    writeReadonlyFile = file.writeReadonlyFile,
    _convertDirectory = require('./_convert_directory'),
    _beautifyJavascriptString = require('./_beautify_javascript_string'),
    compileCoffeeScriptFile = compile.compileCoffeeScriptFile;

exports = module.exports = function (config, callback) {
    _convertDirectory({
        srcDir: config.srcDir,
        destDir: config.destDir,
        pattern: config.pattern,
        ignore: config.ignore,
        fileNameConverter: function (filename) {
            return filename.replace(/\.coffee$/, '.js');
        },
        fileContentConverter: function (src, dest, callback) {
            exports._compile(src, dest, function (err) {
                if (!err) {
                    debug.didCreateFile(dest);
                }
                callback(err);
            });
        }
    }, function (err) {
        callback(err);
    });
};

exports._compile = function (src, dest, data, callback) {
    compileCoffeeScriptFile(src, dest, data, callback);
};

