/*
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 _zackeneule.logic
{
	
	/**
	 */
	let _cache : lib_plankton.cache.type_subject<any>;
	
	
	/**
	 */
	export async function init(
	) : Promise<void>
	{
		_cache = lib_plankton.cache.make<int>(
			{
				// "chest": lib_plankton.storage.memory.implementation_chest<int>({}),
			}
		);
		await lib_plankton.cache.init(
			_cache
		);
	}
	
	
	/**
	 */
	export function group_data(
	) : Promise<
		{
			pool : Map<
				int,
				{
					name : string;
					label : string;
				}
			>;
			order : Array<int>;
		}
	>
	{
		return lib_plankton.cache.get<
			{
				pool : Map<
					int,
					{
						name : string;
						label : string;
					}
				>;
				order : Array<int>;
			}
		>(
			_cache,
			"group_data",
			120,
			async () => {
				const array : Array<
					{
						id : int;
						preview : {
							name : string;
							label : string;
						};
					}
				> = await _zackeneule.backend.group_list();
				const pool : Map<int, {name : string; label : string;}> = new Map<int, {name : string; label : string;}>();
				const order : Array<int> = new Array<int>();
				for (const entry of array)
				{
					pool.set(
						entry.id,
						{
							"name": entry.preview.name,
							"label": entry.preview.label,
						}
					);
					order.push(entry.id);
				}
				return {
					"pool": pool,
					"order": order,
				};
			}
		);
	}
	
}
