import * as geolib from 'geolib';
import { generatePerformanceArc } from './generatePerformanceArc.ts';

/**
 * @param crsIntoEndpoint Course into arc endpoint
 * @param crsFromOrigin Course from arc origin point
 * @param start Arc origin point
 * @param speed Speed within arc
 * @param turnDir Turn direction
 * @returns Line segments, arc endpoint, course into arc endpoint
 */
export const generateOverflyArc = (
  crsIntoEndpoint: number,
  crsFromOrigin: number,
  start: NavFix,
  speed: number,
  turnDir?: TurnDirection,
  force360?: boolean
): [LineSegment[], NavFix, number] => {
  let line: LineSegment[] = [];

  // Compute overfly arc
  if (start.isFlyOver) {
    line = generatePerformanceArc(crsIntoEndpoint, crsFromOrigin, start, speed, turnDir, force360);
  }
  // Compute procedural arc
  else {
    line.push([start.longitude, start.latitude]);
  }

  // Get arc endpoint and crs into arc endpoint
  const arcEnd = { latitude: line.at(-1)![1], longitude: line.at(-1)![0] };
  if (line.length > 1) {
    crsFromOrigin = geolib.getGreatCircleBearing(
      {
        latitude: line.at(-2)![1],
        longitude: line.at(-2)![0],
      },
      arcEnd
    );
  }

  return [line, arcEnd, crsFromOrigin];
};
