import { form, getRequestEvent, query } from '$app/server';
import { libraryCreateSchema, libraryQuerySchema } from '$lib/schema/library';
import { createQueryParams } from '$lib/utils';
import { error } from '@sveltejs/kit';

export const listLibraries = query(libraryQuerySchema, async (data) => {
	const { locals } = getRequestEvent();

	const params = createQueryParams(data);

	const response = await locals.api.get(`/libraries?${params.toString()}`);

	if (!response.ok) error(500, 'An unkown error occurred');

	return await response.json();
});

export const createLibrary = form(libraryCreateSchema, async (data) => {
	const { locals } = getRequestEvent();

	const response = await locals.api.post(`/libraries`, data);

	if (!response.ok) error(500, 'An unknown error occurred');

	return await response.json();
});

export const deleteLibrary = query('unchecked', async (_data) => {
	throw new Error('Not implemented');
});
