import { generateRFArc } from '../pathGenerators/generateRFArc.ts';
import { computeSpeed } from '../utils/computeSpeed.ts';
import { getCourseAndFixForIntercepts } from '../utils/getCourseAndFixForIntercepts.ts';

export const TerminatorsRF = (
  leg: RFTerminalEntry,
  nextLeg: TerminalEntry,
  previousFix: NavFix,
  lastCourse: number,
  waypoint?: Waypoint
): [NavFix?, LineSegment[]?] => {
  const targetFix: NavFix = {
    latitude: leg.WptLat,
    longitude: leg.WptLon,
    name: waypoint?.Ident ?? undefined,
    isFlyOver: leg.IsFlyOver,
    altitude: leg.Alt ? leg.Alt.parseAltitude() : previousFix.altitude,
    speed: computeSpeed(leg, previousFix),
    speedConstraint: leg.SpeedLimit,
    altitudeConstraint: leg.Alt,
  };

  const [crs] = getCourseAndFixForIntercepts(nextLeg, previousFix);

  const line = generateRFArc(
    crs,
    lastCourse,
    previousFix,
    { latitude: leg.CenterLat, longitude: leg.CenterLon },
    leg.TurnDir
  );

  line.push([targetFix.longitude, targetFix.latitude]);

  return [targetFix, line];
};
