namespace _dali.pages
{
	
	/**
	 */
	lib_plankton.zoo_page.register(
		"calendar_edit",
		async (parameters, target_element) => {
			const read_only : boolean = ((parameters["read_only"] ?? "yes") === "yes");
			const calendar_id : int = parseInt(parameters["calendar_id"]);
			target_element.innerHTML = "";
			target_element.innerHTML = await _dali.helpers.template_coin(
				"calendar_edit",
				"default",
				{
					"label": lib_plankton.translate.get("page.calendar_edit.title.regular")
				}
			);
			const form : lib_plankton.zoo_form.class_form<
				_dali.type.calendar_object,
				{
					name : string;
					access : {
						public : boolean;
						default_level : _dali.type.enum_access_level;
						attributed : lib_plankton.map.type_map<
							_dali.type.user_id,
							_dali.type.enum_access_level
						>;
					};
				}
			> = new lib_plankton.zoo_form.class_form<
				_dali.type.calendar_object,
				{
					name : string;
					access : {
						public : boolean;
						default_level : _dali.type.enum_access_level;
						attributed : lib_plankton.map.type_map<
							_dali.type.user_id,
							_dali.type.enum_access_level
						>;
					};
				}
			>(
				(calendar_object) => ({
					"name": calendar_object.name,
					"access": calendar_object.access,
				}),
				(raw) => ({
					"name": raw.name,
					"access": raw.access,
					"resource": {
						"kind": "local",
						"data": {
							"events": [],
						}
					},
				}),
				new lib_plankton.zoo_input.class_input_group<any>(
					[
						{
							"name": "name",
							"input": new lib_plankton.zoo_input.class_input_text(),
							"label": lib_plankton.translate.get("calendar.name")
						},
						{
							"name": "access",
							"input": new lib_plankton.zoo_input.class_input_group(
								[
									{
										"name": "public",
										"input": new lib_plankton.zoo_input.class_input_checkbox(),
										"label": lib_plankton.translate.get("calendar.access.public"),
									},
									{
										"name": "default_level",
										"input": _dali.helpers.input_access_level(),
										"label": lib_plankton.translate.get("calendar.access.default_level"),
									},
									{
										"name": "attributed",
										"input": await _dali.helpers.input_attributed_access(),
										"label": lib_plankton.translate.get("calendar.access.attributed"),
									},
								]
							),
							"label": lib_plankton.translate.get("calendar.access.access"),
						},
					]
				),
				(
					read_only
					?
					[
					]
					:
					[
						{
							"label": lib_plankton.translate.get("page.calendar_edit.actions.change"),
							"target": "submit",
							"procedure": async (get_value, get_representation) => {
								const value : any = await get_value();
								try {
									await _dali.backend.calendar_change(
										calendar_id,
										value
									);
									lib_plankton.zoo_page.set(
										{
											"name": "overview",
											"parameters": {}
										}
									);
								}
								catch (error) {
									// do nothing
									/*
									lib_plankton.zoo_page.set(
										{
											"name": "event_add",
											"parameters": {
											}
										}
									);
									 */
								}
							}
						},
						{
							"label": lib_plankton.translate.get("page.calendar_edit.actions.remove"),
							"target": "submit",
							"procedure": async (get_value, get_representation) => {
								try {
									await _dali.backend.calendar_remove(
										calendar_id
									);
									lib_plankton.zoo_page.set(
										{
											"name": "overview",
											"parameters": {}
										}
									);
								}
								catch (error) {
									// do nothing
									/*
									lib_plankton.zoo_page.set(
										{
											"name": "event_add",
											"parameters": {
											}
										}
									);
									 */
								}
							}
						},
					]
				)
			);
			await form.setup(document.querySelector("#calendar_edit_form"));
			const calendar_object : _dali.type.calendar_object = await _dali.backend.calendar_get(
				calendar_id
			);
			await form.input_write(calendar_object);
			return Promise.resolve<void>(undefined);
		}
	);

}
