import 'package:latlong2/latlong.dart'; import 'package:manager_api_new/api.dart'; class StepShape { final String type; // 'LineString' | 'Polygon' final List positions; const StepShape(this.type, this.positions); } /// Géométrie des GuidedStep : les coordonnées sont stockées en `[lat, lng]` /// (et NON en GeoJSON `[lng, lat]`), en miroir du `map_geometry_picker` de /// manager-app et de visitapp-web (voir `visitapp-web/src/lib/geo.ts`). class GuidedPathGeo { static List _pairs(dynamic points) { if (points is! List) return const []; final result = []; for (final p in points) { if (p is List && p.length >= 2 && p[0] is num && p[1] is num) { result.add(LatLng((p[0] as num).toDouble(), (p[1] as num).toDouble())); } } return result; } static LatLng? _average(List pts) { if (pts.isEmpty) return null; final lat = pts.map((p) => p.latitude).reduce((a, b) => a + b) / pts.length; final lng = pts.map((p) => p.longitude).reduce((a, b) => a + b) / pts.length; return LatLng(lat, lng); } /// Point représentatif : Point → lui-même ; LineString/Polygon → centroïde. static LatLng? center(GeometryDTO? geometry) { final coords = geometry?.coordinates; if (coords is! List || coords.isEmpty) return null; if (geometry?.type == 'LineString') return _average(_pairs(coords)); if (geometry?.type == 'Polygon') return _average(_pairs(coords[0])); if (coords[0] is num && coords[1] is num) { return LatLng((coords[0] as num).toDouble(), (coords[1] as num).toDouble()); } return null; } /// Tracé réel d'un LineString/Polygon (pour dessiner sur la carte) ; /// null pour un Point. static StepShape? shape(GeometryDTO? geometry) { final coords = geometry?.coordinates; if (coords is! List) return null; if (geometry?.type == 'LineString') { final pos = _pairs(coords); return pos.length > 1 ? StepShape('LineString', pos) : null; } if (geometry?.type == 'Polygon') { final pos = _pairs(coords.isNotEmpty ? coords[0] : null); return pos.length > 2 ? StepShape('Polygon', pos) : null; } return null; } static bool hasPoint(GeometryDTO? geometry) => center(geometry) != null; }