const { bundle } = require('../../../../lib/binary');
const { sequence } = require('../../../../lib/db');

module.exports = async function (req, res, next) {

	try {
		let json = await sequence.get(req.params.project, req.params.sequence, req.query);
		const type = req.query.type ?? 2;
		if (type == 2) {
			// Filter out missing raw data
			json = json.filter(el => el.geometryraw);
		} else if (type == 3) {
			// Filter out missing final data
			json = json.filter(el => el.geometryfinal);
		}

		if (json.length) {
			const data = bundle(json, {type});
			res.status(200).send(Buffer.from(data));
		} else {
			res.status(404).send();
		}

		next();
	} catch (err) {
		next(err);
	}

};
