import GeoJsonToGpx from '@dwayneparton/geojson-to-gpx';
import { saveAs } from 'file-saver';
import { Waypoint } from '../shared/types';

export const exportMarkersToGpx = (waypoints: Waypoint[]) => {
    const gpx = GeoJsonToGpx({
        type: 'Feature',
        geometry: {
            type: 'LineString',
            coordinates: waypoints.map(({ coordinates }) => [
                coordinates.lng,
                coordinates.lat,
            ]),
        },
        properties: {},
    });
    const gpxBlob = new Blob([new XMLSerializer().serializeToString(gpx)], {
        type: 'application/gpx+xml',
    });
    saveAs(gpxBlob, 'route.gpx');
};
