import * as turf from '@turf/turf';
import { decode, encode } from "@googlemaps/polyline-codec";
import IGCParser from 'igc-parser';
import { simplify } from './douglaspeucker';
/*
const arrvol = [
  { lat: 45.5, lon: 5.5 },
  { lat: 45.6, lon: 5.7 },
  { lat: 45.7, lon: 5.9 }
];

// conversion en GeoJSON LineString
const line = turf.lineString(
  arrvol.map(p => [p.lon, p.lat])
);

// buffer en kilomètres (ex: 2 km)
const buffered = turf.buffer(line, 2, { units: 'kilometers' });

console.log(buffered);
if (buffered) {
  const encoded = encodePolygon(buffered);
  console.log(encoded);
  const decoded = decodePolygon(encoded);
  console.log(decoded);
}
*/
function loadIGC(fileContent) {
    let result = IGCParser.parse(fileContent, { lenient: true });
    if (result.errors.length > 0) {
        console.error("Errors while parsing IGC file:", result.errors);
        return null;
    }
    return result;
}
function simplifyIGC(igcfile, dpepsilon = 0.018) {
    const s = simplify(igcfile.fixes.map(pt => pt.longitude), igcfile.fixes.map(pt => pt.latitude), dpepsilon);
    return s[0].reduce((acc, cur, i) => { acc.push({ lat: s[1][i], lon: cur }); return acc; }, []);
}
function getPolygon(coordinates, distance, steps = 8) {
    const line = turf.lineString(coordinates.map(p => [p.lon, p.lat]));
    const buffered = turf.buffer(line, distance, { units: 'kilometers', steps: steps });
    return buffered;
}
function intersect(poly1, poly2) {
    return turf.intersect(turf.featureCollection([poly1, poly2]));
}
function encodePolygon(polygon) {
    const coordinates = polygon.geometry.type === 'Polygon' ? polygon.geometry.coordinates : polygon.geometry.coordinates[0];
    return coordinates.map(c => {
        return encode(c.map(coord => [coord[1], coord[0]]), 5); // Inverser les coordonnées pour l'encodage
    }).join('¤'); // Utiliser un séparateur pour différencier les anneaux du polygone
}
function decodePolygon(encoded) {
    const coordinates = encoded.split('¤').map(ring => {
        return decode(ring, 5).map(coord => [coord[1], coord[0]]); // Inverser les coordonnées
    });
    return turf.polygon(coordinates);
}
export default { loadIGC, simplifyIGC, getPolygon, intersect, encodePolygon, decodePolygon };
