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

// NOTE: No wind adjustments to be made, no clue how *that* would draw
export const TerminatorsVR = (
  leg: VRTerminalEntry,
  previousFix: NavFix,
  lastCourse: number
): [NavFix?, LineSegment[]?] => {
  const navaid = {
    latitude: leg.NavLat,
    longitude: leg.NavLon,
  };
  const crsFromEndpoint = leg.Course.toTrue(previousFix);
  const crsIntoEndpoint = leg.NavBear.toTrue(navaid);
  const speed = computeSpeed(leg, previousFix);

  // Compute overfly
  const [line, arcEnd, _lastCourse] = generateOverflyArc(crsFromEndpoint, lastCourse, previousFix, speed, leg.TurnDir);
  lastCourse = _lastCourse;

  // Compute intercept of crs from arc end and radial
  const interceptFix: NavFix = {
    ...computeIntersection(arcEnd, crsFromEndpoint, navaid, crsIntoEndpoint)!,
    isFlyOver: leg.IsFlyOver,
    altitude: leg.Alt ? leg.Alt.parseAltitude() : previousFix.altitude,
    speed: speed,
    speedConstraint: leg.SpeedLimit,
    altitudeConstraint: leg.Alt,
  };

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

  return [interceptFix, line];
};
