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
		);
};

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.request);
			},
			err => reply(err) // 500 error - will only be thrown on bad implementation
		);
};

call trackSession with request, not response