import type {NonRecursiveType, SimplifyObject} from './internal/index.d.ts';
import type {ApplyDefaultOptions} from './apply-default-options.d.ts';
import type {IsUnknown} from './is-unknown.d.ts';
import type {OmitIndexSignature} from './omit-index-signature.d.ts';
import type {PartialOnUndefined} from './partial-on-undefined.d.ts'; // For `@link`

/**
@see {@link PartialOnUndefinedDeep}
*/
export type PartialOnUndefinedDeepOptions = {
	/**
	Whether to affect the individual elements of arrays and tuples.

	When `false`, arrays and tuples are kept as is.

	@default true
	*/
	readonly recurseIntoArrays?: boolean;
};

type DefaultPartialOnUndefinedDeepOptions = {
	recurseIntoArrays: true;
};

/**
Create a deep version of another type where all keys accepting `undefined` type are set to optional.

This utility type is recursive, transforming at any level deep, including the elements of arrays and tuples. You can keep arrays and tuples as is by passing `{recurseIntoArrays: false}` as the second type argument.

Use {@link PartialOnUndefined} if you only need one level deep.

Use-cases:
- Make all properties of a type that can be undefined optional to not have to specify keys with undefined value.

@example
```
import type {PartialOnUndefinedDeep} from 'type-fest';

type Settings = {
	optionA: string;
	optionB: number | undefined;
	subOption: {
		subOptionA: boolean;
		subOptionB: boolean | undefined;
	};
};

const testSettings: PartialOnUndefinedDeep<Settings> = {
	optionA: 'foo',
	// 👉 optionB is now optional and can be omitted
	subOption: {
		subOptionA: true,
		// 👉 subOptionB is now optional as well and can be omitted
	},
};
```

The elements of arrays and tuples are transformed too. Pass `{recurseIntoArrays: false}` to keep arrays and tuples as is:

```
import type {PartialOnUndefinedDeep} from 'type-fest';

type Shape = {
	layers: Array<{name: string; opacity: number | undefined}>;
};

const shape: PartialOnUndefinedDeep<Shape> = {
	layers: [{name: 'Background'}], // OK
};

const exactShape: PartialOnUndefinedDeep<Shape, {recurseIntoArrays: false}> = {
	// @ts-expect-error
	layers: [{name: 'Background'}], // The array elements are kept as is
};
```

@category Object
*/
export type PartialOnUndefinedDeep<T, Options extends PartialOnUndefinedDeepOptions = {}> =
	PartialOnUndefinedDeepValue<T, ApplyDefaultOptions<PartialOnUndefinedDeepOptions, DefaultPartialOnUndefinedDeepOptions, Options>>;

// The value dispatcher has already checked that this is an object.
type _PartialOnUndefinedDeep<T, Options extends Required<PartialOnUndefinedDeepOptions>> =
	{[KeyType in keyof T as undefined extends T[KeyType] ? IsUnknown<T[KeyType]> extends true ? never : KeyType : never]?: PartialOnUndefinedDeepValue<T[KeyType], Options>} extends infer U // Make a partial type with all value types accepting undefined (and set them optional)
		// The transformed parts can be intersected directly; a merge's equality comparison eagerly expands recursive values.
		? SimplifyObject<{[KeyType in keyof T as KeyType extends keyof OmitIndexSignature<U> ? never : KeyType]: PartialOnUndefinedDeepValue<T[KeyType], Options>} & U> // Join all remaining keys not treated in U
		: never; // Should not happen

/**
Utility type to get the value type by key and recursively call `PartialOnUndefinedDeep` to transform sub-objects.
*/
type PartialOnUndefinedDeepValue<T, Options extends Required<PartialOnUndefinedDeepOptions>> = T extends NonRecursiveType
	? T
	: T extends ReadonlyArray<infer U> // Test if type is array or tuple
		? Options['recurseIntoArrays'] extends false
			? T // If they opt out of array recursion, just use the original type
			: U[] extends T // Check if array not tuple
				? readonly U[] extends T
					? ReadonlyArray<PartialOnUndefinedDeepValue<U, Options>> // Readonly array treatment
					: Array<PartialOnUndefinedDeepValue<U, Options>> // Mutable array treatment
				: {[Key in keyof T]: PartialOnUndefinedDeepValue<T[Key], Options>} // Tuple treatment
		// A map or set is a container, so its value or item type is converted. Rebuilding it member by member instead would leave the value type untouched, defeating the point of the type.
		: T extends Map<infer KeyType, infer ValueType>
			? Map<KeyType, PartialOnUndefinedDeepValue<ValueType, Options>>
			: T extends ReadonlyMap<infer KeyType, infer ValueType>
				? ReadonlyMap<KeyType, PartialOnUndefinedDeepValue<ValueType, Options>>
				: T extends WeakMap<infer KeyType extends WeakKey, infer ValueType>
					? WeakMap<KeyType, PartialOnUndefinedDeepValue<ValueType, Options>>
					: T extends Set<infer ItemType>
						? Set<PartialOnUndefinedDeepValue<ItemType, Options>>
						: T extends ReadonlySet<infer ItemType>
							? ReadonlySet<PartialOnUndefinedDeepValue<ItemType, Options>>
							: T extends WeakSet<infer ItemType extends WeakKey>
								? WeakSet<PartialOnUndefinedDeepValue<ItemType, Options>>
								: T extends Record<any, any> | undefined
									? _PartialOnUndefinedDeep<T, Options>
									: unknown;

export {};
