import type { GeoPoint } from "../model/mapTypes";

export function simplifyGeometry(points: GeoPoint[], minPixelDistance: number, project: (point: GeoPoint) => { x: number; y: number }): GeoPoint[] {
  if (points.length <= 2 || minPixelDistance <= 0) return points;

  const result: GeoPoint[] = [];
  let previousScreenPoint = project(points[0]!);
  result.push(points[0]!);

  for (let index = 1; index < points.length - 1; index += 1) {
    const point = points[index]!;
    const screenPoint = project(point);
    if (Math.hypot(screenPoint.x - previousScreenPoint.x, screenPoint.y - previousScreenPoint.y) >= minPixelDistance) {
      result.push(point);
      previousScreenPoint = screenPoint;
    }
  }

  result.push(points[points.length - 1]!);
  return result;
}

