'use server'; import { revalidatePath } from 'next/cache'; import type { GraphicLibrary, LibraryGraphic } from '@/types/visualisierung/library'; import type { GrafikGeneratorDaten } from '@/types/visualisierung/graphic'; const url = 'http://localhost:3000/visualisierung/grafik-bibliothek'; async function request(path = '', init?: RequestInit): Promise { const response = await fetch(url + path, { cache: 'no-store', ...init }); const result = await response.json(); if (!response.ok) throw new Error(Array.isArray(result.message) ? result.message.join(', ') : result.message ?? 'Grafikbibliothek nicht verfügbar'); return result; } export async function api_graphicLibrary() { return request(); } export async function api_updateLibraryGraphic(id: string, input: { anlageId: string; version: number; generator: GrafikGeneratorDaten; variant: string }) { const body = JSON.stringify({ ...input, generator: { grafikTyp: input.generator.grafikTyp, config: input.generator.config, options: input.generator.options } }); if (new TextEncoder().encode(body).length > 9 * 1024 * 1024) throw new Error('Grafikparameter sind zu groß'); const asset = await request('/' + encodeURIComponent(id), { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body }); revalidatePath('/', 'layout'); return asset; } export async function api_createLibraryGraphic(input: { anlageId: string; name: string; generator: GrafikGeneratorDaten; variant: string }) { const body = JSON.stringify({ ...input, generator: { grafikTyp: input.generator.grafikTyp, config: input.generator.config, options: input.generator.options } }); if (new TextEncoder().encode(body).length > 9 * 1024 * 1024) throw new Error('Grafikparameter sind zu groß'); const asset = await request('', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body }); revalidatePath('/visualisierung/bibliothek'); return asset; } export async function api_deleteLibraryGraphic(id: string, anlageId: string) { await request('/' + encodeURIComponent(id), { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ anlageId }) }); revalidatePath('/visualisierung/bibliothek'); } export async function api_exportLibrary(anlageId: string) { return request<{ count: number; directory: string }>('/xaml', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ anlageId }) }); }