<script lang="ts">
	import '../app.css';
	import { page } from '$app/stores';
	import { wsConnect, wsDisconnect } from '$lib/realtime';
	import { browser } from '$app/environment';
	import SystemAnnouncements from '$lib/components/SystemAnnouncements.svelte';

	let { children } = $props();

	// Koble til portvokterens WebSocket når autentisert og i browser
	$effect(() => {
		const session = $page.data.session as Record<string, unknown> | undefined;
		const accessToken = session?.accessToken as string | undefined;
		if (browser && accessToken) {
			wsConnect(accessToken);
		}
		return () => {
			if (browser) wsDisconnect();
		};
	});
</script>

<SystemAnnouncements />
{@render children()}
