
/**
 */
namespace _dali
{
	
	/**
	 */
	function nav_groups(
		logged_in : boolean
	) : Array<string>
	{
		return (
			logged_in
			?
			["logged_in"]
			:
			["logged_out"]
		);
	}
	
	
	/**
	 * @todo reload page when switching to "logged_out"
	 */
	async function update(
	) : Promise<void>
	{
		lib_plankton.log.debug(
			"dali.update"
		);
		const logged_in : boolean = await _dali.backend.is_logged_in();
		lib_plankton.zoo_page.nav_set_groups(nav_groups(logged_in));
		// lib_plankton.zoo_page.reload();
	}
	
	
	/**
	 */
	export async function main(
	) : Promise<void>
	{
		// conf
		await _dali.conf.init(
			"conf.json"
		);
		
		// init
		lib_plankton.log.set_main_logger(
			[
				{"kind": "console", "data": {"threshold": "info"}},
			]
		);
		await _dali.backend.init(
		);
		await lib_plankton.translate.initialize(
			{
				"verbosity": 1,
				"packages": [
					JSON.parse(await lib_plankton.file.read("data/localization/deu.loc.json")),
					JSON.parse(await lib_plankton.file.read("data/localization/eng.loc.json")),
				],
				"order": ["deu", "eng"],
				"autopromote": false,
			}
		);
		lib_plankton.zoo_page.init(
			document.querySelector("main"),
			{
				"fallback": {
					"name": "overview",
					"parameters": {}
				},
				"nav_entries": [
					{
						"location": {"name": "login", "parameters": {}},
						"label": lib_plankton.translate.get("page.login.title"),
						"groups": ["logged_out"],
					},
					{
						"location": {"name": "overview", "parameters": {}},
						"label": lib_plankton.translate.get("page.overview.title"),
						"groups": ["logged_out", "logged_in"],
					},
					{
						"location": {"name": "calendar_add", "parameters": {}},
						"label": lib_plankton.translate.get("page.calendar_add.title"),
						"groups": ["logged_in"],
					},
					{
						"location": {"name": "caldav", "parameters": {}},
						"label": lib_plankton.translate.get("page.caldav.title"),
						"groups": ["logged_in"],
					},
					/*
					{
						"location": {"name": "event_add", "parameters": {}},
						"label": lib_plankton.translate.get("page.event_add.title"),
						"groups": ["logged_in"],
					}
					 */
					{
						"location": {"name": "logout", "parameters": {}},
						"label": lib_plankton.translate.get("page.logout.title"),
						"groups": ["logged_in"],
					},
				],
				"nav_initial_groups": [],
			}
		);
		await update();
		lib_plankton.call.loop(
			() => {
				update();
			},
			_dali.conf.get().misc.update_interval
		);
		
		// exec
		lib_plankton.zoo_page.start();
		
		return Promise.resolve<void>(undefined);
	}
	
}

