import { progressStream } from '$lib/stores';
import { render } from 'svelte/server';

export function GET() {
	const stream = new ReadableStream({
		start(controller) {
			const unsubscribe = progressStream.subscribe((value) => {
				controller.enqueue(`data: ${JSON.stringify({ progress: value })}\n\n`);
			});

			return () => {
				unsubscribe();
			};
		}
	});

	return new Response(stream, {
		headers: {
			'Content-Type': 'text/event-stream',
			'Cache-Control': 'no-cache',
			Connection: 'keep-alive'
		}
	});
}
