/*
Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Frontend
Copyright (C) 2024 Christian Fraß

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

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see 
<https://www.gnu.org/licenses/>. 
 */

namespace _espe.conf
{
	
	/**
	 */
	export type type_data = {
		backend : {
			scheme : string;
			host : string;
			port : int;
			path_base : string;
		};
		settings : {
			title : string;
			test_mode : boolean;
			registration_defaults : {
				email_address : ("none" | "only_veiled" | "both");
				email_redirect : boolean;
			};
		};
	};
	
	
	/**
	 */
	var _data : (null | type_data) = null;
	
	
	/**
	 */
	export async function load(
	) : Promise<void>
	{
		let data_raw;
		try {
			data_raw = lib_plankton.json.decode(await lib_plankton.file.read("conf.json"));
		}
		catch (error) {
			data_raw = {};
			console.warn("could not read configuration file");
		}
		_data = {
			"backend": (
				(node_backend => ({
					"scheme": (node_backend["scheme"] ?? "http"),
					"host": (node_backend["host"] ?? "localhost"),
					"port": (node_backend["port"] ?? 4916),
					"path_base": (node_backend["path_base"] ?? ""),
				})) (data_raw["backend"] ?? {})
			),
			"settings": (
				(node_settings => ({
					"title": (node_settings["title"] ?? "Espe"),
					"test_mode": (node_settings["test_mode"] ?? false),
					"registration_defaults": (
						(node_settings_registration_defaults => ({
							"email_address": (node_settings_registration_defaults["email_address"] ?? "both"),
							"email_redirect": (node_settings_registration_defaults["email_redirect"] ?? true),
						})) (node_settings["registration_defaults"] ?? {})
					),
				})) (data_raw["settings"] ?? {})
			),
		};
	}
	
	
	/**
	 */
	export function get(
	) : type_data
	{
		if (_data === null) {
			throw (new Error("conf not loaded yet"));
		}
		else {
			return _data;
		}
	}
	
}
