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

export const TerminatorsCF = (
  leg: CFTerminalEntry,
  previousFix: NavFix,
  lastCourse: number,
  waypoint?: Waypoint
): [NavFix?, LineSegment[]?] => {
  const speed = computeSpeed(leg, previousFix);

  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: speed,
    speedConstraint: leg.SpeedLimit,
    altitudeConstraint: leg.Alt,
  };

  // Compute arc
  const line = handleTurnAtFix(
    leg.Course.toTrue(previousFix),
    leg.Course.toTrue(previousFix),
    lastCourse,
    previousFix,
    targetFix,
    speed,
    leg.TurnDir
  );

  return [targetFix, line];
};
