import type { MileageMarkerSpec } from '../types/timeline';

const SELECT_THRESHOLD_PX = 12;

/** Derives active (passed) and selected (near cursor) flags for mileage markers. */
export function enrichMileageMarkers(
  markers: MileageMarkerSpec[],
  posPx: number,
): MileageMarkerSpec[] {
  let selectedId: string | null = null;
  let minDist = Infinity;

  for (const marker of markers) {
    const dist = Math.abs(posPx - marker.left);
    if (dist < minDist) {
      minDist = dist;
      selectedId = marker.id;
    }
  }

  return markers.map((marker) => ({
    ...marker,
    active: posPx >= marker.left,
    selected: marker.id === selectedId && minDist <= SELECT_THRESHOLD_PX,
  }));
}
