/*
This file is part of »zeitbild«.

Copyright 2025 'kcf' <fenris@folksprak.org>

»zeitbild« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

»zeitbild« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with »zeitbild«. If not, see <http://www.gnu.org/licenses/>.
 */


namespace _zeitbild.service.resource
{
	
	/**
	 */
	export function add(
		resource_object : _zeitbild.type_resource_object
	) : Promise<_zeitbild.type_resource_id>
	{
		return _zeitbild.repository.resource.create(resource_object);
	}
	
	
	/**
	 */
	export async function event_get(
		resource_id : _zeitbild.type_resource_id,
		local_resource_event_id : _zeitbild.type_local_resource_event_id
	) : Promise<_zeitbild.type_event_object>
	{
		const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(
			resource_id
		);
		switch (resource_object.kind) {
			case "local": {
				const event_object : _zeitbild.type_event_object = await _zeitbild.repository.resource.local_resource_event_read(
					resource_id,
					local_resource_event_id
				);
				return Promise.resolve<_zeitbild.type_event_object>(event_object);
				break;
			}
			case "ics_feed": {
				// TODO
				return Promise.reject(new Error("not implemented"));
				break;
			}
			default: {
				// @ts-ignore
				throw (new Error("unhandled resource kind: " + resource_object.kind));
			}
		}
	}
	
	
	/**
	 */
	export async function event_add(
		resource_id : _zeitbild.type_resource_id,
		event_object : _zeitbild.type_event_object
	) : Promise<_zeitbild.type_local_resource_event_id>
	{
		const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(
			resource_id
		);
		switch (resource_object.kind) {
			case "local": {
				const local_resource_event_id : _zeitbild.type_local_resource_event_id = await _zeitbild.repository.resource.local_resource_event_create(
					resource_id,
					event_object
				);
				return Promise.resolve<_zeitbild.type_local_resource_event_id>(local_resource_event_id);
				break;
			}
			case "ics_feed": {
				return Promise.reject(new Error("unavailable"));
				break;
			}
			default: {
				// @ts-ignore
				throw (new Error("unhandled resource kind: " + resource_object.kind));
			}
		}
	}
	
	
	/**
	 */
	export async function event_change(
		resource_id : _zeitbild.type_resource_id,
		local_resource_event_id : _zeitbild.type_local_resource_event_id,
		event_object : _zeitbild.type_event_object
	) : Promise<void>
	{
		const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(
			resource_id
		);
		switch (resource_object.kind) {
			case "local": {
				await _zeitbild.repository.resource.local_resource_event_update(
					resource_id,
					local_resource_event_id,
					event_object
				);
				return Promise.resolve<void>(undefined);
				break;
			}
			case "ics_feed": {
				return Promise.reject(new Error("unavailable"));
				break;
			}
			default: {
				// @ts-ignore
				throw (new Error("unhandled resource kind: " + resource_object.kind));
			}
		}
	}
	
	
	/**
	 */
	export async function event_remove(
		resource_id : _zeitbild.type_resource_id,
		local_resource_event_id : _zeitbild.type_local_resource_event_id
	) : Promise<void>
	{
		const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(
			resource_id
		);
		switch (resource_object.kind) {
			case "local": {
				await _zeitbild.repository.resource.local_resource_event_delete(
					resource_id,
					local_resource_event_id
				);
				return Promise.resolve<void>(undefined);
				break;
			}
			case "ics_feed": {
				return Promise.reject(new Error("unavailable"));
				break;
			}
			default: {
				// @ts-ignore
				throw (new Error("unhandled resource kind: " + resource_object.kind));
			}
		}
	}
		
}
