namespace _dali.pages
{
	
	/**
	 */
	lib_plankton.zoo_page.register(
		"login",
		async (parameters, target_element) => {
			target_element.innerHTML = "";
			const preparation : {kind : string; data : any;} = await _dali.backend.session_prepare(
				{
					"oidc_redirect_uri_template": _dali.conf.get()["misc"]["oidc_redirect_uri_template"],
				}
			);
			switch (preparation.kind)
			{
				case "internal":
				{
					target_element.innerHTML = await _dali.helpers.template_coin(
						"login",
						"default",
						{
						}
					);
					const form : lib_plankton.zoo_form.class_form<
						{name : string; password : string;},
						{name : string; password : string;}
					> = new lib_plankton.zoo_form.class_form<
						{name : string; password : string;},
						{name : string; password : string;}
					>(
						x => x,
						x => x,
						new lib_plankton.zoo_input.class_input_group<
							{name : string; password : string;}
						>(
							[
								{
									"name": "name",
									"input": new lib_plankton.zoo_input.class_input_text(),
									"label": lib_plankton.translate.get("page.login.internal.name"),
								},
								{
									"name": "password",
									"input": new lib_plankton.zoo_input.class_input_password(),
									"label": lib_plankton.translate.get("page.login.internal.password"),
								},
							]
						),
						[
							{
								"label": lib_plankton.translate.get("page.login.internal.do"),
								"target": "submit",
								"procedure": async (get_value, get_representation) => {
									const value : any = await get_value();
									try
									{
										await _dali.backend.session_begin(
											value.name,
											value.password
										);
										lib_plankton.zoo_page.nav_set_groups(["logged_in"]);
										lib_plankton.zoo_page.set(
											{
												"name": "overview",
												"parameters": {}
											}
										);
									}
									catch (error)
									{
										lib_plankton.zoo_page.set(
											{
												"name": "login",
												"parameters": {
													"name": value.name,
												}
											}
										);
									}
								}
							},
						]
					);
					await form.setup(document.querySelector("#login"));
					await form.input_write(
						{
							"name": (parameters.name ?? ""),
							"password": "",
						}
					);
					break;
				}
				case "oidc":
				{
					let element_a : HTMLElement = document.createElement("a");;
					element_a.textContent = lib_plankton.string.coin(
						lib_plankton.translate.get("page.login.oidc.via"),
						{
							"title": preparation.data.label,
						}
					);
					element_a.setAttribute("href", preparation.data.url);
					target_element.innerHTML = "";
					target_element.appendChild(element_a);
					break;
				}
				default:
				{
					break;
				}
			}
			return Promise.resolve<void>(undefined);
		}
	);

}

