/**
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).

@category Type
*/
type Primitive = null | undefined | string | number | boolean | symbol | bigint;

declare global {
	// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
	interface SymbolConstructor {
		readonly observable: symbol;
	}
}

/**
Matches any primitive, `void`, `Date`, or `RegExp` value.
*/
type BuiltIns = Primitive | void | Date | RegExp;

/**
@see PartialDeep
*/
type PartialDeepOptions = {
	/**
	Whether to affect the individual elements of arrays and tuples.

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

/**
Create a type from another type with all keys and nested keys set to optional.

Use-cases:
- Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
- Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.

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

const settings: Settings = {
	textEditor: {
		fontSize: 14;
		fontColor: '#000000';
		fontWeight: 400;
	}
	autocomplete: false;
	autosave: true;
};

const applySavedSettings = (savedSettings: PartialDeep<Settings>) => {
	return {...settings, ...savedSettings};
}

settings = applySavedSettings({textEditor: {fontWeight: 500}});
```

By default, this does not affect elements in array and tuple types. You can change this by passing `{recurseIntoArrays: true}` as the second type argument:

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

interface Settings {
	languages: string[];
}

const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
	languages: [undefined]
};
```

@category Object
@category Array
@category Set
@category Map
*/
type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends
	BuiltIns | (((...arguments_: any[]) => unknown)) | (new(...arguments_: any[]) => unknown) ? T
	: T extends Map<infer KeyType, infer ValueType> ? PartialMapDeep<KeyType, ValueType, Options>
	: T extends Set<infer ItemType> ? PartialSetDeep<ItemType, Options>
	: T extends ReadonlyMap<infer KeyType, infer ValueType> ? PartialReadonlyMapDeep<KeyType, ValueType, Options>
	: T extends ReadonlySet<infer ItemType> ? PartialReadonlySetDeep<ItemType, Options>
	: T extends object ? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
			? Options["recurseIntoArrays"] extends true ? ItemType[] extends T // Test for arrays (non-tuples) specifically
					? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
						? ReadonlyArray<PartialDeep<ItemType | undefined, Options>>
					: Array<PartialDeep<ItemType | undefined, Options>>
				: PartialObjectDeep<T, Options> // Tuples behave properly
			: T // If they don't opt into array testing, just use the original type
		: PartialObjectDeep<T, Options>
	: unknown;

/**
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
*/
type PartialMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> =
	& {}
	& Map<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;

/**
Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
*/
type PartialSetDeep<T, Options extends PartialDeepOptions> = {} & Set<PartialDeep<T, Options>>;

/**
Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
*/
type PartialReadonlyMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> =
	& {}
	& ReadonlyMap<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;

/**
Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
*/
type PartialReadonlySetDeep<T, Options extends PartialDeepOptions> = {} & ReadonlySet<PartialDeep<T, Options>>;

/**
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
*/
type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOptions> = {
	[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType], Options>;
};

type TerminationResponse = {
	/** Termination states. */
	code: "TIMED_OUT" | "SERVER_ERROR" | "TERMINATED" | "INTERNAL_ERROR";
	/** Whether or not the server was successfully closed. */
	success: boolean;
	/** Termination or error message. */
	message: string;
	/** If termination fails, the error that caused it. */
	error?: Error;
};
/** Options for the server to use while mocking. */
type ServerOptions = {
	/**
	 * The port to listen on. If not provided, attempts to use a set of default ports, and falls back to a random port if unavailable.
	 *
	 * @default 63142 | 63143 | 63144
	 */
	port: number;
	/**
	 * The hostname to listen on.
	 *
	 * @default "localhost"
	 */
	hostname: string;
	/**
	 * The authentication type and token to use.
	 *
	 * @default { type: "bearer", value: "SecretToken" }
	 */
	token: {
		/**
		 * The type of authentication to use.
		 *
		 * @default "bearer"
		 */
		type: "bearer" | "basic";
		/**
		 * The token to use for authentication.
		 *
		 * @default "SecretToken"
		 */
		value: string;
	};
	/**
	 * Information about the mocked package. Determines the route of the server.
	 *
	 * @default { name: "@mockscope/foobar", version: "1.0.0" }
	 */
	package: {
		/**
		 * The name of the mocked package. Determines the route of the server.
		 *
		 * Names are soft encoded, preserving `@`s but escaping all other special characters via {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent `encodeURIComponent`} (i.e. `/` becomes `%2F`).
		 *
		 * @default "@mockscope/foobar"
		 */
		name: string;
		/**
		 * The version of the mocked package.
		 *
		 * @default "1.0.0"
		 */
		version: string;
	};
};
type CloseFunction = () => Promise<TerminationResponse>;

/** Options for the server to use while mocking. */
type Options = PartialDeep<ServerOptions>;
/** Computed server options. */
type Response = ServerOptions & {
	/** Gracefully closes the server. */
	close: CloseFunction;
};
/** Starts a server and exposes an endpoint for the given package name, returning a JSON object with a mock of the {@link https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md package's metadata} from the npm registry. */
declare function mockPrivateRegistry(options?: Options): Promise<Response>;
declare function mockPrivateRegistry(packageName: string): Promise<Response>;

export { mockPrivateRegistry as default, type Options, type Response };
