/**
 * Create a ci server context.
 * @function serverside.context.newCiContext
 * @param {object} [context] - Predefined context object.
 * @returns {object} - Defined server context object.
 * @author Taka Okunishi
 *
 */

var object = require('../../lib/object'),
    deepCopy = object.deepCopy,
    _newContext = require('./_new_context'),
    rq = require('../request'),
    rs = require('../response'),
    md = require('../middleware');


exports = module.exports = function (context) {
    if (!context) {
        context = {};
    }

    deepCopy(exports._constants, context);


    return _newContext(context, {
        requestEnhanceFunctions: {
            lang: rq.defineReqLang
        },
        responseEnhanceFunctions: {
            pageNotFound: rs.defineResPageNotFound,
            redirect: rs.defineResRedirect,
            somethingWrong: rs.defineResSomethingWrong
        },
        middlewares: [
            md.defineAccessLogMiddleware,
            md.defineReqEnhanceMiddleware,
            md.defineResEnhanceMiddleware,
            md.definePublicFilesMiddleware,
            md.definePageNotFoundMiddleware,
            md.defineSomethingWrongMiddleware
        ]
    });

};

exports._constants = require('../constants/ci-constants');