import type { Point } from '../types/index.ts';

import { xyObjectCheck } from './xyObjectCheck.ts';

/**
 * Finds the max y value and return a {x,y,index} point
 * @param points - object that contains property x (an ordered increasing array) and y (an array).
 * @returns max point.
 */
export function xyObjectMaxYPoint(points: Point[] = []): Point {
  xyObjectCheck(points);

  if (points.length === 0) return { x: 0, y: 0 };

  let current: Point = {
    x: points[0].x,
    y: points[0].y,
    index: 0,
  };

  for (let i = 1; i < points.length; i++) {
    if (points[i].y > current.y) {
      current = {
        x: points[i].x,
        y: points[i].y,
        index: i,
      };
    }
  }

  return current;
}
