
namespace _zeitbild.api
{
	
	/**
	 */
	export function register_export_ical(
		rest_subject : lib_plankton.rest_caldav.type_rest
	) : void
	{
		register<
			null,
			(
				lib_plankton.ical.type_vcalendar
				|
				string
			)
		>(
			rest_subject,
			lib_plankton.http.enum_method.get,
			"/export/ical",
			{
				"description": "trägt Veranstaltungen aus verschiedenen Kalendern zusammen im ical-Format",
				"query_parameters": () => ([
					{
						"name": "from",
						"required": false,
						"description": "UNIX timestamp",
					},
					{
						"name": "to",
						"required": false,
						"description": "UNIX timestamp",
					},
					{
						"name": "calendar_ids",
						"required": false,
						"description": "comma separated",
					},
					{
						"name": "auth",
						"required": true,
						"description": "",
					},
				]),
				"output_schema": () => ({
					"nullable": false,
					"type": "string",
				}),
				"response_body_mimetype": "text/calendar",
				"response_body_encode": (output) => Buffer.from(
					(typeof(output) === "string")
					?
					output
					:
					lib_plankton.ical.ics_encode(output)
				),
				"restriction": restriction_none,
				"execution": async (stuff) => {
					const user_id : (null | _zeitbild.type_user_id) = await (
						session_from_stuff(stuff)
						.then(
							(session : {key : string; value : lib_plankton.session.type_session;}) => (
								_zeitbild.service.user.identify(session.value.name)
								.catch(x => Promise.resolve(null))
							)
						)
						.catch(x => Promise.resolve(null))
					);
					
					const from : lib_plankton.pit.type_pit = (
						("from" in stuff.query_parameters)
						?
						parseInt(stuff.query_parameters["from"])
						:
						lib_plankton.pit.shift_week(
							lib_plankton.pit.now(),
							-2
						)
					);
					const to : lib_plankton.pit.type_pit = (
						("to" in stuff.query_parameters)
						?
						parseInt(stuff.query_parameters["to"])
						:
						lib_plankton.pit.shift_week(
							lib_plankton.pit.now(),
							+6
						)
					);
					const calendar_ids_wanted : (null | Array<_zeitbild.type_calendar_id>) = (
						(
							("calendar_ids" in stuff.query_parameters)
							&&
							(stuff.query_parameters["calendar_ids"] !== null)
						)
						?
						lib_plankton.call.convey(
							stuff.query_parameters["calendar_ids"],
							[
								(x : string) => x.split(","),
								(x : Array<string>) => x.map(parseInt),
								(x : Array<int>) => x.filter(y => (! isNaN(y)))
							]
						)
						:
						null
					);
					
					const auth_hash_shall : string = lib_plankton.sha256.get(
						(stuff.query_parameters["calendar_ids"] ?? ""),
						_zeitbild.conf.get()["misc"]["auth_salt"]
					);
					const auth_hash_is : string = stuff.query_parameters["auth"];
					/**
					 * @todo remove
					 */
					lib_plankton.log.info(
						"auth_hashes",
						{
							"shall": auth_hash_shall,
							"is": auth_hash_is,
						}
					);
					if (! (auth_hash_is === auth_hash_shall)) {
						return Promise.resolve(
							{
								"status_code": 403,
								"data": "not authorized",
							}
						);
					}
					else {
						return (
							_zeitbild.service.calendar.gather_events(
								calendar_ids_wanted,
								from,
								to,
								user_id
							)
							.then(
								(data) => Promise.resolve(
									{
										"status_code": 200,
										"data": _zeitbild.helpers.ical_vcalendar_from_own_event_list(
											data.map(entry => entry.event_object)
										)
									}
								)
							)
							.catch(
								(reason) => Promise.resolve(
									{
										"status_code": 403,
										"data": String(reason),
									}
								)
							)
						);
					}
				}
			}
		);
	}
	
}
