namespace _espe.api
{
	
	/**
	 * @todo types
	 */
	export function register_member_register(
		rest_subject : lib_plankton.rest.type_rest
	) : void
	{
		lib_plankton.rest.register<
			{
				name_login : string;
				name_display : string;
				salutation : string;
				email_mode : ("none" | "number" | "number_and_name");
				email_redirect : boolean;
				password : string;
			},
			null
		>(
			rest_subject,
			lib_plankton.http.enum_method.post,
			"/member/register/:id",
			{
				"description": "nimmt zusätzliche Angaben eines Mitglieds entgegen",
				"query_parameters": [
					{
						"name": "verification",
						"required": true,
						"description": "Prüfwert",
					},
				],
				"restriction": restriction_verification(stuff => parseInt(stuff.path_parameters["id"])),
				"execution": async ({"path_parameters": path_parameters, "query_parameters": query_parameters, "input": input}) => {
					const id : _espe.service.member.type_id = parseInt(path_parameters["id"]);
					let successful : boolean = false;
					try {
						await _espe.service.member.register(
							id,
							{
								"name_login": input.name_login,
								"name_display": input.name_display,
								"salutation": input.salutation,
								"email_address_numberbased_use": ["number", "number_and_name"].includes(input.email_mode),
								"email_address_namebased_use": ["number_and_name"].includes(input.email_mode),
								"email_redirect_to_private": input.email_redirect,
								"password": input.password,
							}
						);
						successful = true;
					}
					catch (error) {
						successful = false;
					}
					if (! successful) {
						return Promise.resolve({
							"status_code": 409,
							"data": "bereits registriert",
						});
					}
					else {
						await _espe.helpers.email_send(
							_espe.conf.get().admins.map(admin => admin.email_address).filter(x => (x !== null)),
							"Eingegangene Registrierung",
							("Member-ID: " + id.toFixed(0))
						);
						return Promise.resolve({
							"status_code": 200,
							"data": null,
						});
					}
				},
			}
		)
	}
	
}
