
/**
 */
namespace _zeitbild.helpers
{
	
	/**
	 * @todo timezone
	 */
	function ical_datetime_to_own_datetime(
		ical_datetime : lib_plankton.ical.type_datetime
	) : lib_plankton.pit.type_datetime
	{
		return {
			"timezone_shift": 0,
			"date": {
				"year": ical_datetime.date.year,
				"month": ical_datetime.date.month,
				"day": ical_datetime.date.day,
			},
			"time": (
				(ical_datetime.time === null)
				?
				null
				:
				{
					"hour": ical_datetime.time.hour,
					"minute": ical_datetime.time.minute,
					"second": ical_datetime.time.second,
				}
			)
		};
	}
	
	
	/**
	 * @todo timezone
	 */
	export function ical_dt_to_own_datetime(
		ical_dt: lib_plankton.ical.type_dt
	) : lib_plankton.pit.type_datetime
	{
		return {
			"timezone_shift": 0,
			"date": ical_dt.value.date,
			"time": (
				(ical_dt.value.time === null)
				?
				null
				:
				{
					"hour": ical_dt.value.time.hour,
					"minute": ical_dt.value.time.minute,
					"second": ical_dt.value.time.second,
				}
			)
		};
	}
	
	
	/**
	 * @todo timezone
	 */
	export function ical_dt_from_own_datetime(
		datetime : lib_plankton.pit.type_datetime
	) : lib_plankton.ical.type_dt
	{
		return {
			"tzid": "Europe/Berlin",
			"value": {
				"date": {
					"year": datetime.date.year,
					"month": datetime.date.month,
					"day": datetime.date.day,
				},
				"time": (
					(datetime.time === null)
					?
					null
					:
					{
						"utc": true,
						"hour": datetime.time.hour,
						"minute": datetime.time.minute,
						"second": datetime.time.second,
					}
				)
			},
		};
	}
	
	
	/**
	 */
	export function ical_vevent_from_own_event(
		event_object : _zeitbild.type_event_object,
		uid : string
	) : lib_plankton.ical.type_vevent
	{
		return {
			"uid": uid,
			"dtstamp": ical_dt_from_own_datetime(event_object.begin).value,
			"dtstart": ical_dt_from_own_datetime(event_object.begin),
			"dtend": (
				(event_object.end === null)
				?
				undefined
				:
				ical_dt_from_own_datetime(event_object.end)
			),
			"location": (event_object.location ?? undefined),
			"summary": event_object.name,
			"url": (event_object.link ?? undefined),
			"description": (event_object.description ?? undefined),
		};
	}
	
	
	/**
	 * @todo assign better uids
	 */
	export function ical_vcalendar_from_own_event_list(
		events : Array<_zeitbild.type_event_object>
	) : lib_plankton.ical.type_vcalendar
	{
		const pit_now : lib_plankton.pit.type_pit = lib_plankton.pit.now();
		const datetime_now : lib_plankton.pit.type_datetime = lib_plankton.pit.to_datetime(pit_now);
		const stamp : string = lib_plankton.string.coin(
			"{{year}}{{month}}{{day}}",
			{
				"year": datetime_now.date.year.toFixed(0).padStart(4, "0"),
				"month": datetime_now.date.month.toFixed(0).padStart(2, "0"),
				"day": datetime_now.date.day.toFixed(0).padStart(2, "0"),
			}
		);
		return {
			"version": "2.0",
			"prodid": "",
			"vevents": (
				events
				.map<lib_plankton.ical.type_vevent>(
					(entry, index) => ical_vevent_from_own_event(
						entry,
						lib_plankton.string.coin(
							"zeitbild_{{stamp}}_{{index}}",
							{
								"stamp": stamp,
								"index": index.toFixed(0)
							}
						)
					),
				)
			),
			"method": "PUBLISH",
			"vtimezone": {
				"tzid": "Europe/Berlin",
			},
		};
	}
	
	
	/**
	 */
	var _template_cache : Record<string, string> = {};
	
	
	/**
	 * @todo caching
	 */
	export async function template_coin(
		name : string,
		data : Record<string, string>
	) : Promise<string>
	{
		let content : string;
		if (! (name in _template_cache)) {
			content = (
				(
					await lib_plankton.file.read(
						lib_plankton.string.coin(
							"templates/{{name}}.html.tpl",
							{
								"name": name,
							}
						)
					)
				)
				.toString()
			);
			_template_cache[name] = content;
		}
		else {
			content = _template_cache[name];
		}
		return Promise.resolve<string>(
			lib_plankton.string.coin(
				content,
				data
			)
		);
	}
	
	
	/**
	 * @todo outsource
	 */
	export async function promise_row<type_result>(
		members : Array<
			() => Promise<type_result>
		>
	) : Promise<
		Array<
			type_result
		>
	>
	{
		let results : Array<type_result> = [];
		for await (const member of members) {
			results.push(await member());
		}
		return Promise.resolve<Array<type_result>>(results);
	}
	
}
