const json = require('./json');
const geojson = require('./geojson');
const html = require('./html');
const pdf = require('./pdf');
const csv = require('./csv');

module.exports = async function (req, res, next) {
	try {
		const handlers = {
			"application/json": json,
			"application/geo+json": geojson,
			"text/csv": csv,
			"text/html": html,
			"application/pdf": pdf
		};

		const mimetype = (handlers[req.query.mime] && req.query.mime) || req.accepts(Object.keys(handlers));

		if (mimetype) {
			res.set("Content-Type", mimetype);
			await handlers[mimetype](req, res, next);
		} else {
			res.status(406).send();
			next();
		}
	} catch (err) {
		next(err);
	}
}
