// @flow
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import getHandler from './handler';

export const onPreResponse = {
	/*
	 * This function processes the route response before it is sent to the client.
	 *
	 * - In dev, it transforms the generic 500 error response JSON into a full dev-
	 *   friendly rendering of the stack trace.
	 */
	method: (request: HapiRequest, reply: HapiReply) => {
		const response = request.response;

		if (response && response.header) {
			response.header('vary', 'User-Agent')
		}

		if (!response.isBoom || process.env.NODE_ENV === 'production') {
			return reply.continue();
		}
		const error = response;
		const { RedBoxError } = require('redbox-react');
		const errorMarkup = ReactDOMServer.renderToString(
			React.createElement(RedBoxError, { error })
		);
		const errorResponse = reply(
			`<!DOCTYPE html><html><body>${errorMarkup}</body></html>`
		);
		errorResponse.code(error.output.statusCode);
		return errorResponse;
	},
};

/*
 * Wildcard route for all application GET requests - see the 'handler' module
 * for rendering details
 *
 * Route config:
 *
 * - `onPreResponse`: process the response before it is sent to client
 * - `state`: skip cookie validation because cookies can be set by other services
 *   on the same domain and may not conform to the validation rules that are
 *   used by Hapi
 */
export default (languageRenderers: { [string]: LanguageRenderer }) => ({
	method: 'GET',
	path: '/{wild*}',
	config: {
		ext: {
			onPreResponse,
		},
		plugins: {
			'electrode-csrf-jwt': {
				enabled: true, // need to generate tokens on page request
			},
		},
		state: {
			failAction: 'ignore', // ignore cookie validation, just accept
		},
	},
	handler: getHandler(languageRenderers),
});

@@ -460,46 +460,8 @@
 e;%0A%0A
-%09%09if (response && response.header) %7B%0A%09
 %09%09re
@@ -495,20 +495,16 @@
 -Agent')
-%0A%09%09%7D
 %0A%0A%09%09if (

remove unneeded checks from route plugin