<script lang="ts">
	import '../app.css';
	import { browser } from '$app/environment';
	import { QueryClient, QueryClientProvider } from '@tanstack/svelte-query';
	import { onLogout } from '$lib/auth/auth.svelte';
	import { resetRoleContext } from '$features/roles/roleContext.svelte';

	let { children } = $props();

	const queryClient = $state(
		new QueryClient({
			defaultOptions: {
				queries: {
					enabled: browser,
					staleTime: 1000 * 60 * 5, // 5 minutes
					retry: 1
				}
			}
		})
	);

	// Clear user-specific caches on logout to prevent cross-account data leakage
	onLogout(() => {
		queryClient.removeQueries({ queryKey: ['sessions'] });
		resetRoleContext();
	});
</script>

<QueryClientProvider client={queryClient}>
	{@render children()}
</QueryClientProvider>
