/**
 * Define lang method for requests.
 * @function serverside.req.defineReqLang
 * @see {@link http://github.com/jed/locale | locale}
 * @param {object} context - Server context object.
 * @param {string|string[]} context.SUPPORTED_LANGS - Supported languages.
 * @param {string} context.DEFAULT_LANG - Default language.
 * @return {function} - Req lang function.
 * @author Taka Okunishi
 *
 */
module.exports = function (context) {
    context.assertKeys(['SUPPORTED_LANGS', 'DEFAULT_LANG']);

    var Locales = require("locale").Locales,
        supported = new Locales(context.SUPPORTED_LANGS.split(',')),
        defaultLang = context.DEFAULT_LANG;

    return function () {
        var req = this,
            accepted = new Locales(req.headers["accept-language"]);
        var best = accepted.best(supported);
        return best && best.language || best.toString() || defaultLang;
    }
};