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

// NOTE: Direct entry into an RF does not calculate a usable line, given inbound course is unknown.
export const TerminatorsRF = (
  leg: RFTerminalEntry,
  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,
    IsFAF: leg.IsFAF,
    IsMAP: leg.IsMAP,
  };

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

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

  return [targetFix, line];
};
