
namespace _zeitbild.api
{
	
	/**
	 */
	export function register_caldav_projects(
		rest_subject : lib_plankton.rest_caldav.type_rest
	) : void
	{
		register<
			lib_plankton.xml.type_node_data,
			(
				null
				|
				lib_plankton.xml.type_node_data
			)
		>(
			rest_subject,
			lib_plankton.caldav.enum_method.propfind,
			"/caldav/project",
			{
				"output_schema": () => ({
					"nullable": false,
					"type": "string",
				}),
				"request_body_mimetype": () => "text/xml",
				"request_body_decode": () => async (body, header_content_type) => (
					(
						(header_content_type !== null)
						&&
						(
							(header_content_type.startsWith("text/xml"))
							||
							(header_content_type.startsWith("application/xml"))
						)
					)
					?
					lib_plankton.xml.parse(body.toString())
					:
					Promise.resolve<any>(null)
				),
				"response_body_mimetype": "text/xml",
				"response_body_encode": output => Promise.resolve<Buffer>(
					Buffer.from(
						lib_plankton.xml.get_node_logic(output).compile(0)
					)
				),
				// "restriction": restriction_basic_auth,
				"restriction": restriction_none,
				/**
				 * @todo heed stuff.input
				 */
				"execution": async (stuff) => {
					const user_id : (null | type_user_id) = await _zeitbild.api.web_auth(
						stuff.headers["Authorization"]
						??
						stuff.headers["authorization"]
						??
						null
					);
					if (user_id === null) {
						return Promise.resolve(
							{
								"status_code": 401,
								"data": {
									"kind": "text",
									"data": ""
								},
								"extra_headers": {
									"WWW-Authenticate": "Basic realm=Restricted",
								}
							}
						);
					}
					else {
						if (stuff.input === null) {
							return Promise.resolve(
								{
									"status_code": 400,
									"data": null,
								}
							);
						}
						else {
							return (
								_zeitbild.service.caldav.projects(
									stuff.input,
									user_id
								)
								.then(
									(output) => Promise.resolve(
										{
											"status_code": 207,
											"data": output,
										}
									)
								)
							);
						}
					}
				}
			}
		);
	}
	
}
