import * as geolib from 'geolib';

/**
 * @param leg Leg to examine
 * @param origin Origin of current leg
 * @returns Adjusted course and fix
 */
export const getCourseAndFixForIntercepts = (leg: TerminalEntry, origin: NavFix): [number, NavFix] => {
  switch (leg.TrackCode) {
    case 'CF': {
      const _leg = leg as CFTerminalEntry;
      const fix = { latitude: _leg.WptLat, longitude: _leg.WptLon };
      return [_leg.Course.reciprocalCourse().toTrue(fix), fix];
    }
    case 'FM': {
      const _leg = leg as FMTerminalEntry;
      const fix = { latitude: _leg.WptLat, longitude: _leg.WptLon };
      return [_leg.Course.toTrue(fix), fix];
    }
    case 'TF': {
      const _leg = leg as FMTerminalEntry;
      return [
        geolib.getGreatCircleBearing(origin, {
          latitude: _leg.WptLat,
          longitude: _leg.WptLon,
        }),
        { latitude: _leg.WptLat, longitude: _leg.WptLon },
      ];
    }
    case 'AF': {
      const _leg = leg as AFTerminalEntry;
      const fix = { latitude: _leg.WptLat, longitude: _leg.WptLon };
      return [_leg.Course.reciprocalCourse().toTrue(fix), fix];
    }
    default: {
      return [-1, origin];
    }
  }
};
