/*
This file is part of »dali«.

Copyright 2025 'kcf' <fenris@folksprak.org>

»dali« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

»dali« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with »dali«. If not, see <http://www.gnu.org/licenses/>.
 */


namespace _dali.widgets
{
	
	/**
	 */
	export class class_widget_login implements lib_plankton.zoo_widget.interface_widget
	{
		
		/**
		 * [data]
		 */
		private initial_name : string;
		
		
		/**
		 * [hook]
		 */
		private action_cancel : (null | (() => void));
		
		
		/**
		 * [hook]
		 */
		private action_success : (null | (() => void));
		
		
		/**
		 */
		public constructor(
			{
				"initial_name": initial_name = "",
				"action_cancel": action_cancel = null,
				"action_success": action_success = null,
			}
			:
			{
				initial_name ?: string;
				action_cancel ?: (null | (() => void));
				action_success ?: (null | (() => void));
			}
			=
			{
			}
		)
		{
			this.initial_name = initial_name;
			this.action_cancel = action_cancel;
			this.action_success = action_success;
		}
		
		
		/**
		 * [implementation]
		 */
		public async load(
			target_element : HTMLElement
		)
		: Promise<void>
		{
			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":
				{
					const sub_widget : lib_plankton.zoo_widget.interface_widget = (
						new class_widget_login_internal(
							preparation.data,
							{
								"initial_name": this.initial_name,
								"action_cancel": this.action_cancel,
								"action_success": this.action_success,
							}
						)
					);
					await sub_widget.load(target_element);
					break;
				}
				case "oidc":
				{
					const sub_widget : lib_plankton.zoo_widget.interface_widget = (
						new class_widget_login_oidc(
							preparation.data,
							{
								"action_cancel": this.action_cancel,
								"action_success": this.action_success,
							}
						)
					);
					await sub_widget.load(target_element);
					break;
				}
				default:
				{
					throw (new Error("unhandled login kind: " + preparation.kind));
					break;
				}
			}
		}
		
	}
	
}
