import { last } from './last.ts';
import { difference as differenceToolkit } from '../../array/difference.ts';
import { differenceWith as differenceWithToolkit } from '../../array/differenceWith.ts';
import { flattenArrayLike } from '../_internal/flattenArrayLike.ts';
import { isArrayLikeObject } from '../predicate/isArrayLikeObject.ts';

/**
 * Computes the difference between the primary array and another array using a comparator function.
 *
 * @template T1, T2
 * @param array - The primary array to compare elements against.
 * @param values - The array containing elements to compare with the primary array.
 * @param comparator - A function to determine if two elements are considered equal.
 * @returns A new array containing the elements from the primary array that do not match any elements in `values` based on the comparator.
 *
 * @example
 * const array = [{ id: 1 }, { id: 2 }, { id: 3 }];
 * const values = [{ id: 2 }];
 * const comparator = (a, b) => a.id === b.id;
 *
 * const result = differenceWith(array, values, comparator);
 * // result will be [{ id: 1 }, { id: 3 }]
 */
export function differenceWith<T1, T2>(
  array: ArrayLike<T1> | null | undefined,
  values: ArrayLike<T2>,
  comparator: (a: T1, b: T2) => boolean
): T1[];

/**
 * Computes the difference between the primary array and two arrays using a comparator function.
 *
 * @template T1, T2, T3
 * @param array - The primary array to compare elements against.
 * @param values1 - The first array containing elements to compare with the primary array.
 * @param values2 - The second array containing elements to compare with the primary array.
 * @param comparator - A function to determine if two elements are considered equal.
 * @returns A new array containing the elements from the primary array that do not match any elements in `values1` or `values2` based on the comparator.
 *
 * @example
 * const array = [{ id: 1 }, { id: 2 }, { id: 3 }];
 * const values1 = [{ id: 2 }];
 * const values2 = [{ id: 3 }];
 * const comparator = (a, b) => a.id === b.id;
 *
 * const result = differenceWith(array, values1, values2, comparator);
 * // result will be [{ id: 1 }]
 */
export function differenceWith<T1, T2, T3>(
  array: ArrayLike<T1> | null | undefined,
  values1: ArrayLike<T2>,
  values2: ArrayLike<T3>,
  comparator: (a: T1, b: T2 | T3) => boolean
): T1[];

/**
 * Computes the difference between the primary array and multiple arrays using a comparator function.
 *
 * @template T1, T2, T3, T4
 * @param array - The primary array to compare elements against.
 * @param values1 - The first array containing elements to compare with the primary array.
 * @param values2 - The second array containing elements to compare with the primary array.
 * @param values - Additional arrays and an optional comparator function to determine if two elements are considered equal.
 * @returns A new array containing the elements from the primary array that do not match any elements
 * in `values1`, `values2`, or subsequent arrays. If a comparator function is provided, it will be used to compare elements;
 * otherwise, [SameValueZero](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-samevaluezero) algorithm will be used.
 *
 * @example
 * // Example with comparator function
 * const array = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];
 * const values1 = [{ id: 2 }];
 * const values2 = [{ id: 3 }];
 * const values3 = [{ id: 4 }];
 * const comparator = (a, b) => a.id === b.id;
 *
 * const result = differenceWith(array, values1, values2, values3, comparator);
 * // result will be [{ id: 1 }]
 *
 * @example
 * // Example without comparator function (behaves like `difference`)
 * const array = [1, 2, 3, 4];
 * const values1 = [2];
 * const values2 = [3];
 * const values3 = [4];
 *
 * const result = differenceWith(array, values1, values2, values3);
 * // result will be [1]
 */
export function differenceWith<T1, T2, T3, T4>(
  array: ArrayLike<T1> | null | undefined,
  values1: ArrayLike<T2>,
  values2: ArrayLike<T3>,
  ...values: Array<ArrayLike<T4> | ((a: T1, b: T2 | T3 | T4) => boolean)>
): T1[];

/**
 * Computes the difference between the primary array and one or more arrays without using a comparator function.
 *
 * @template T
 * @param array - The primary array to compare elements against.
 * @param values - One or more arrays containing elements to compare with the primary array.
 * @returns A new array containing the elements from the primary array that do not match any elements in the provided arrays.
 *
 * @example
 * const array = [1, 2, 3];
 * const values1 = [2];
 * const values2 = [3];
 *
 * const result = differenceWith(array, values1, values2);
 * // result will be [1]
 */
export function differenceWith<T>(array: ArrayLike<T> | null | undefined, ...values: Array<ArrayLike<T>>): T[];

/**
 * Computes the difference between the primary array and one or more arrays using an optional comparator function.
 *
 * @template T
 * @param array - The primary array to compare elements against.
 * @param values - One or more arrays to compare with the primary array, and an optional comparator function to determine if two elements are considered equal.
 * @returns A new array containing the elements from the primary array that do not match any elements in the provided arrays or those compared using the comparator function.
 *
 * @example
 * // Example with a comparator function
 * const array = [{ id: 1 }, { id: 2 }, { id: 3 }];
 * const values1 = [{ id: 2 }];
 * const values2 = [{ id: 3 }];
 * const comparator = (a, b) => a.id === b.id;
 *
 * const result = differenceWith(array, values1, values2, comparator);
 * // result will be [{ id: 1 }]
 *
 * @example
 * // Example without a comparator function
 * const array = [1, 2, 3];
 * const values1 = [2];
 * const values2 = [3];
 *
 * const result = differenceWith(array, values1, values2);
 * // result will be [1]
 */
export function differenceWith<T>(
  array: ArrayLike<T> | null | undefined,
  ...values: Array<ArrayLike<unknown> | ((a: unknown, b: unknown) => boolean)>
): T[] {
  if (!isArrayLikeObject(array)) {
    return [];
  }

  const comparator = last(values);
  const flattenedValues = flattenArrayLike(values as Array<ArrayLike<T>>);

  if (typeof comparator === 'function') {
    return differenceWithToolkit(Array.from(array), flattenedValues, comparator);
  }

  return differenceToolkit(Array.from(array), flattenedValues);
}
