import { identity } from '../../function/identity.ts';
import { toArray } from '../_internal/toArray.ts';
import { ValueIteratee } from '../_internal/ValueIteratee.ts';
import { iteratee as iterateeToolkit } from '../util/iteratee.ts';

/**
 * Finds the element in an array that has the maximum value when applying
 * the `iteratee` to each element.
 *
 * @template T - The type of elements in the array.
 * @param items The array of elements to search.
 * @param [iteratee=identity]
 * The criteria used to determine the maximum value.
 *  - If a **function** is provided, it extracts a numeric value from each element.
 *  - If a **string** is provided, it is treated as a key to extract values from the objects.
 *  - If a **[key, value]** pair is provided, it matches elements with the specified key-value pair.
 *  - If an **object** is provided, it matches elements that contain the specified properties.
 * @returns The element with the maximum value as determined by the `iteratee`.
 * @example
 * maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }
 * maxBy([], x => x.a); // Returns: undefined
 * maxBy(
 *   [
 *     { name: 'john', age: 30 },
 *     { name: 'jane', age: 28 },
 *     { name: 'joe', age: 26 },
 *   ],
 *   x => x.age
 * ); // Returns: { name: 'john', age: 30 }
 * maxBy([{ a: 1 }, { a: 2 }], 'a'); // Returns: { a: 2 }
 * maxBy([{ a: 1 }, { a: 2 }], ['a', 1]); // Returns: { a: 1 }
 * maxBy([{ a: 1 }, { a: 2 }], { a: 1 }); // Returns: { a: 1 }
 */
export function maxBy<T>(items: ArrayLike<T> | null | undefined, iteratee: ValueIteratee<T> = identity): T | undefined {
  if (items == null) {
    return undefined;
  }

  const array = toArray(items);

  if (array.length === 0) {
    return undefined;
  }

  const getValue = iterateeToolkit(iteratee);

  let maxElement: T | undefined;
  let max: unknown;

  for (let i = 0; i < array.length; i++) {
    const element = array[i];
    const current = getValue(element, i, array);

    if (current == null || Number.isNaN(current) || typeof current === 'symbol') {
      continue;
    }

    if (max === undefined || current > (max as number)) {
      max = current;
      maxElement = element;
    }
  }

  return maxElement;
}
