<script lang="ts">
	import { onMount } from 'svelte';
	import { authStore } from '$lib/stores/auth.svelte';
	import { gameStore } from '$lib/stores/game.svelte';
	import {
		loadFromServer,
		startAutoSave,
		stopAutoSave,
		setupVisibilitySync,
	} from '$lib/save-sync';

	onMount(async () => {
		// Init auth
		await authStore.init();

		// Load save or init guest
		if (authStore.user) {
			const loaded = await loadFromServer();
			if (!loaded && !gameStore.ready) {
				gameStore.initGuest();
			}
			startAutoSave();
			setupVisibilitySync();
		} else {
			gameStore.initGuest();
		}
	});
</script>
