import { getLanguage, checkLanguageRedirect } from '../util/languageUtils';

export const getAppRouteHandler = renderRequestMap => (request, reply) => {
	const supportedLangs = Object.keys(renderRequestMap);
	const requestLanguage = getLanguage(request, supportedLangs);
	const redirect = checkLanguageRedirect(
		request,
		reply,
		requestLanguage,
		supportedLangs
	);
	if (redirect) {
		return redirect;
	}
	request.app.localeCode = requestLanguage;
	request.app.supportedLocaleCodes = supportedLangs;
	const renderRequest = renderRequestMap[requestLanguage];

	return renderRequest(request)
		.do(() => request.server.app.logger.debug('HTML response ready'))
		.subscribe(
			({ redirect, result, statusCode }) => {
				// response is sent when this function returns (`nextTick`)
				if (redirect) {
					return reply.redirect(redirect.url).permanent(redirect.permanent);
				}
				const response = reply(result).code(statusCode);

				request.trackSession(response);
			},
			err => reply(err) // 500 error - will only be thrown on bad implementation
		);
};

@@ -963,16 +963,24 @@
 response
+.request
 );%0A%09%09%09%7D,

call trackSession with request, not response