import { cloneDeep } from './cloneDeep.ts';
import { defaults } from './defaults.ts';

/**
 * Creates a new object based on the provided `object`, applying default values from the `sources` to ensure that no properties are left `undefined`.
 * It assigns default values to properties that are either `undefined` or come from `Object.prototype`.
 *
 * You can provide multiple source objects to set these default values,
 * and they will be applied in the order they are given, from left to right.
 * Once a property has been set, any later values for that property will be ignored.
 *
 * Note: This function creates a new object. If you want to modify the `object`, use the `defaults` function instead.
 *
 * Note: This function creates a new object. If you want to modify the `object`, use the `defaults` function instead.
 *
 * @template T - The type of the object being processed.
 * @param object - The target object.
 * @returns The cloned object.
 */
export function toDefaulted<T extends object>(object: T): T;

/**
 * Creates a new object based on the provided `object`, applying default values from the `sources` to ensure that no properties are left `undefined`.
 * It assigns default values to properties that are either `undefined` or come from `Object.prototype`.
 *
 * You can provide multiple source objects to set these default values,
 * and they will be applied in the order they are given, from left to right.
 * Once a property has been set, any later values for that property will be ignored.
 *
 * Note: This function creates a new object. If you want to modify the `object`, use the `defaults` function instead.
 *
 * @template T - The type of the object being processed.
 * @template S - The type of the object that provides default values.
 * @param object - The target object that will receive default values.
 * @param source - The object that specifies the default values to apply.
 * @returns A new object that combines the target and default values, ensuring no properties are left undefined.
 */
export function toDefaulted<T extends object, S extends object>(object: T, source: S): NonNullable<T & S>;

/**
 * Creates a new object based on the provided `object`, applying default values from the `sources` to ensure that no properties are left `undefined`.
 * It assigns default values to properties that are either `undefined` or come from `Object.prototype`.
 *
 * You can provide multiple source objects to set these default values,
 * and they will be applied in the order they are given, from left to right.
 * Once a property has been set, any later values for that property will be ignored.
 *
 * Note: This function creates a new object. If you want to modify the `object`, use the `defaults` function instead.
 *
 * @template T - The type of the object being processed.
 * @template S1 - The type of the first object that provides default values.
 * @template S2 - The type of the second object that provides default values.
 * @param object - The target object that will receive default values.
 * @param source1 - The first object that specifies the default values to apply.
 * @param source2 - The second object that specifies the default values to apply.
 * @returns A new object that combines the target and default values, ensuring no properties are left undefined.
 */
export function toDefaulted<T extends object, S1 extends object, S2 extends object>(
  object: T,
  source1: S1,
  source2: S2
): NonNullable<T & S1 & S2>;

/**
 * Creates a new object based on the provided `object`, applying default values from the `sources` to ensure that no properties are left `undefined`.
 * It assigns default values to properties that are either `undefined` or come from `Object.prototype`.
 *
 * You can provide multiple source objects to set these default values,
 * and they will be applied in the order they are given, from left to right.
 * Once a property has been set, any later values for that property will be ignored.
 *
 * Note: This function creates a new object. If you want to modify the `object`, use the `defaults` function instead.
 *
 * @template T - The type of the object being processed.
 * @template S1 - The type of the first object that provides default values.
 * @template S2 - The type of the second object that provides default values.
 * @template S3 - The type of the third object that provides default values.
 * @param object - The target object that will receive default values.
 * @param source1 - The first object that specifies the default values to apply.
 * @param source2 - The second object that specifies the default values to apply.
 * @param source3 - The third object that specifies the default values to apply.
 * @returns A new object that combines the target and default values, ensuring no properties are left undefined.
 */
export function toDefaulted<T extends object, S1 extends object, S2 extends object, S3 extends object>(
  object: T,
  source1: S1,
  source2: S2,
  source3: S3
): NonNullable<T & S1 & S2 & S3>;

/**
 * Creates a new object based on the provided `object`, applying default values from the `sources` to ensure that no properties are left `undefined`.
 * It assigns default values to properties that are either `undefined` or come from `Object.prototype`.
 *
 * You can provide multiple source objects to set these default values,
 * and they will be applied in the order they are given, from left to right.
 * Once a property has been set, any later values for that property will be ignored.
 *
 * Note: This function creates a new object. If you want to modify the `object`, use the `defaults` function instead.
 *
 * @template T - The type of the object being processed.
 * @template S1 - The type of the first object that provides default values.
 * @template S2 - The type of the second object that provides default values.
 * @template S3 - The type of the third object that provides default values.
 * @template S4 - The type of the fourth object that provides default values.
 * @param object - The target object that will receive default values.
 * @param source1 - The first object that specifies the default values to apply.
 * @param source2 - The second object that specifies the default values to apply.
 * @param source3 - The third object that specifies the default values to apply.
 * @param source4 - The fourth object that specifies the default values to apply.
 * @returns A new object that combines the target and default values, ensuring no properties are left undefined.
 */
export function toDefaulted<
  T extends object,
  S1 extends object,
  S2 extends object,
  S3 extends object,
  S4 extends object,
>(object: T, source1: S1, source2: S2, source3: S3, source4: S4): NonNullable<T & S1 & S2 & S3 & S4>;

/**
 * Creates a new object based on the provided `object`, applying default values from the `sources` to ensure that no properties are left `undefined`.
 * It assigns default values to properties that are either `undefined` or come from `Object.prototype`.
 *
 * You can provide multiple source objects to set these default values,
 * and they will be applied in the order they are given, from left to right.
 * Once a property has been set, any later values for that property will be ignored.
 *
 * Note: This function creates a new object. If you want to modify the `object`, use the `defaults` function instead.
 *
 * @template T - The type of the object being processed.
 * @template S - The type of the objects that provides default values.
 * @param object - The target object that will receive default values.
 * @param sources - The objects that specifies the default values to apply.
 * @returns A new object that combines the target and default values, ensuring no properties are left undefined.
 *
 * @example
 * toDefaulted({ a: 1 }, { a: 2, b: 2 }, { c: 3 }); // { a: 1, b: 2, c: 3 }
 * toDefaulted({ a: 1, b: 2 }, { b: 3 }, { c: 3 }); // { a: 1, b: 2, c: 3 }
 * toDefaulted({ a: null }, { a: 1 }); // { a: null }
 * toDefaulted({ a: undefined }, { a: 1 }); // { a: 1 }
 */
export function toDefaulted<T extends object, S extends object>(object: T, ...sources: S[]): object;

/**
 * Creates a new object based on the provided `object`, applying default values from the `sources` to ensure that no properties are left `undefined`.
 * It assigns default values to properties that are either `undefined` or come from `Object.prototype`.
 *
 * You can provide multiple source objects to set these default values,
 * and they will be applied in the order they are given, from left to right.
 * Once a property has been set, any later values for that property will be ignored.
 *
 * Note: This function creates a new object. If you want to modify the `object`, use the `defaults` function instead.
 *
 * @template T - The type of the object being processed.
 * @template S - The type of the objects that provides default values.
 * @param object - The target object that will receive default values.
 * @param sources - The objects that specifies the default values to apply.
 * @returns A new object that combines the target and default values, ensuring no properties are left undefined.
 *
 * @example
 * toDefaulted({ a: 1 }, { a: 2, b: 2 }, { c: 3 }); // { a: 1, b: 2, c: 3 }
 * toDefaulted({ a: 1, b: 2 }, { b: 3 }, { c: 3 }); // { a: 1, b: 2, c: 3 }
 * toDefaulted({ a: null }, { a: 1 }); // { a: null }
 * toDefaulted({ a: undefined }, { a: 1 }); // { a: 1 }
 */
export function toDefaulted<T extends object, S extends object>(object: T, ...sources: S[]): object {
  const cloned = cloneDeep(object);

  return defaults(cloned, ...sources);
}
