/**
 * Generate color less.
 * @function task.worker.generateColorLess
 * @param {object} config - Task configuration.
 * @param {string} config.dest - Destination file name.
 * @param {string} config.srcPattern - Source file name pattern.
 * @para {string} config.settingFile - Setting file name.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 */
var path = require('path'),
    fs = require('fs'),
    debug = require('./_debug'),
    lib = require('../../lib'),
    file = lib.file;

exports = module.exports = function (config, callback) {
    var dest = path.resolve(config.dest),
        setting = require(path.resolve(config.settingFile)),
        src = exports._resolvePattern(config.srcPattern, setting);
    fs.exists(src, function (exists) {
        if (!exists) {
            callback(new Error('Source file not exists: ' + src));
            return;
        }
        file.relativeSymlink(src, dest, function (err) {
            if (!err) {
                debug.didCreateLink(dest, src);
            }
            callback(err);
        });
    });
};

exports._resolvePattern = function (pattern, data) {
    var resolved = path.resolve(pattern);
    Object.keys(data).forEach(function (key) {
        resolved = resolved.replace(new RegExp('{{' + key + '}}', 'g'), data[key]);
    });
    return resolved;
};