import * as geolib from "geolib";
import { handleTurnAtFix } from "../pathGenerators/handleTurnAtFix.ts";
import Parser from "../parser.ts";

// NOTE: No wind adjustments to be made, no clue how *that* would draw
export const TerminatorsVD = (
  leg: VDTerminalEntry,
  previousFix: NavFix,
  lastCourse: number
): [NavFix?, LineSegment[]?] => {
  const targetFix: NavFix = {
    ...geolib.computeDestinationPoint(
      previousFix,
      leg.Distance.toMetre(),
      leg.Course.toTrue(previousFix)
    ),
    name: leg.Distance.toString(),
    isFlyOver: true,
    altitude: leg.Alt ? leg.Alt.parseAltitude() : previousFix.altitude,
    speed: leg.SpeedLimit
      ? leg.SpeedLimit > Parser.AC_SPEED
        ? Parser.AC_SPEED
        : leg.SpeedLimit
      : previousFix.speed
      ? previousFix.speed
      : Parser.AC_SPEED,
    speedConstraint: leg.SpeedLimit,
    altitudeConstraint: leg.Alt,
  };

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

  return [targetFix, line];
};
