/**
 * Define html serve function.
 * @function serverside.res._defineHtml
 * @param {object} config - Server context object.
 * @param {object} config.filenames - Filename map. Keys are locale names, values are file names.
 * @param {number} config.statusCode - Status code to send.
 * @param {string} config.defaultLang - Default language.
 * @return {function} - Res html send function.
 * @protected
 * @ignore
 * @author Taka Okunishi
 *
 */
var path = require('path'),
    object = require('../../lib/object'),
    objWithKeyAndValues = object.objWithKeyAndValues,
    _resolveFilenames = require('./_resolve_file_names');


exports = module.exports = function (config) {
    if (!config.filenames) throw new Error('File names are required.');
    var defaultLang = config.defaultLang;

    var filenames = _resolveFilenames(config.filenames);
    if (typeof(filenames) === 'string') {
        filenames = objWithKeyAndValues(defaultLang, filenames);
    }
    return function (lang, callback) {
        switch (arguments.length) {
            case 1:
                if (typeof(arguments[0]) === 'function') {
                    callback = arguments[0];
                    lang = defaultLang;
                }
                break;
        }
        var res = this;
        var filename = filenames[lang || filenames[defaultLang]];
        exports._sendHtmlWithStatus(res, filename, {
            statusCode: config.statusCode,
            contentType: 'text/html'
        }, function (err) {
            callback && callback(err);
        });
    };
};

exports._sendHtmlWithStatus = require('./_send_file');