/*
This file is part of »zeitbild«.

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

»zeitbild« 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.

»zeitbild« 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 »zeitbild«. If not, see <http://www.gnu.org/licenses/>.
 */


namespace _zeitbild.repository.auth_internal
{
	
	/**
	 */
	var _chest : (
		null
		|
		lib_plankton.storage.type_chest<
			Array<string>,
			Record<string, any>,
			lib_plankton.database.type_description_create_table,
			lib_plankton.storage.sql_table_common.type_sql_table_common_search_term,
			Record<string, any>
		>
	) = null;
	
	
	/**
	 */
	function get_chest(
	) : lib_plankton.storage.type_chest<
		Array<string>,
		Record<string, any>,
		lib_plankton.database.type_description_create_table,
		lib_plankton.storage.sql_table_common.type_sql_table_common_search_term,
		Record<string, any>
	>
	{
		if (_chest === null) {
			_chest = lib_plankton.storage.sql_table_common.chest(
				{
					"database_implementation": _zeitbild.database.get_implementation(),
					"table_name": "auth_internal",
					"key_names": ["name"],
				}
			);
		}
		else {
			// do nothing
		}	
		return _chest;
	}
	
	
	/**
	 */
	export function read(
		name : string
	) : Promise<string>
	{
		return (
			get_chest().read([name])
			.then(
				(row) => Promise.resolve<string>(row["password_image"])
			)
		);
	}
	
	
	/**
	 */
	export function write(
		name : string,
		password_image : string
	) : Promise<boolean>
	{
		return get_chest().write([name], {"password_image": password_image});
	}
	
	
	/**
	 */
	export function delete_(
		name : string
	) : Promise<void>
	{
		return get_chest().delete([name]);
	}
	
}
