const json = require('./json');
const yaml = require('./yaml');
const geojson = require('./geojson');
const seis = require('./seis');
const csv = require('./csv');
const setContentDisposition = require('../../../../lib/utils/setContentDisposition');


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

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

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