const transform = require('../transform');
const prepare = require('../prepare');
const filename = require('./filename');


function makeGeoJSON (events) {
	return {
		type: "FeatureCollection",
		features: events.filter(event => event.geometry).map(event => {
			const feature = {
				type: "Feature",
				geometry: event.geometry,
				properties: event
			};
			delete feature.properties.geometry;
			return feature;
		})
	};
}

async function geojson ({project, sequence}) {
	const {events, sequences} = await prepare(project, {sequence});
	const data = makeGeoJSON(events);
	const name = filename(sequences.find(i => i.sequence == sequence), ".geojson");

	return { name, data, mimetype: "application/geo+json" };
}

module.exports = geojson;
