
namespace _zeitbild.api
{
	
	/**
	 */
	export function register_caldav_probe(
		rest_subject : lib_plankton.rest_caldav.type_rest
	) : void
	{
		register<
			(
				null
				|
				lib_plankton.xml.type_node_data
			),
			lib_plankton.xml.type_node_data
		>(
			rest_subject,
			lib_plankton.caldav.enum_method.propfind,
			"/caldav",
			{
				"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)
				),
				"output_schema": () => ({
					"nullable": false,
					"type": "string",
				}),
				"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,
				"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 {
						const output : (null | lib_plankton.xml.type_node_data) = _zeitbild.service.caldav.probe(
							stuff.input
						);
						if (output !== null) {
							return Promise.resolve(
								{
									"status_code": 207,
									"data": output
								}
							);
						}
						else {
							return Promise.resolve(
								{
									"status_code": 501,
									"data": {
										"kind": "root",
										"data": {
											"version": "1.0",
											"encoding": "utf-8",
											"content": {
												"kind": "text",
												"data": ""
											}
										}
									}
								}
							);
						}
					}
				}
			}
		);
	}
	
}
