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 TerminatorsVA = (
  leg: VATerminalEntry,
  previousFix: NavFix,
  lastCourse: number
): [NavFix?, LineSegment[]?] => {
  const targetFix: NavFix = {
    ...geolib.computeDestinationPoint(
      previousFix,
      (
        ((leg.Alt.parseAltitude() - (previousFix.altitude ?? 0)) /
          Parser.AC_VS) *
        (Parser.AC_SPEED / 60)
      ).toMetre(),
      leg.Course.toTrue(previousFix)
    ),
    name: leg.Alt,
    isFlyOver: true,
    altitude: leg.Alt.parseAltitude(),
    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];
};
