<!--
Resolver for the bare `lobby` AppScreen literal. F8-04b split the
old single-page Overview into per-panel screens (`games-active-past`,
`games-recruitment`, `games-invitations`, `games-private-games`); this
component is what the dispatcher renders when the active screen is
the historical `lobby` alias (e.g. a snapshot persisted before the
split, or programmatic `appScreen.go("lobby")` from non-shell code).

On mount the resolver awaits the lobby fan-out + account fetch, then
hands off to `firstVisibleGamesScreen()` — the same helper the shell
sidebar uses, so the auto-expanded sub-page picked here is the same
one the sidebar will highlight (e.g. `games-active-past` when the
player has games, `games-invitations` when there are pending invites
but no games, etc.). While the data is in flight the user sees the
lobby chrome with the i18n list-loading placeholder.
-->
<script lang="ts">
	import { onMount } from "svelte";
	import { appScreen } from "$lib/app-nav.svelte";
	import { i18n } from "$lib/i18n/index.svelte";
	import { lobbyData } from "$lib/lobby-data.svelte";
	import { account } from "$lib/account-store.svelte";
	import {
		firstVisibleGamesScreen,
		type LobbyNavState,
	} from "$lib/lobby-nav";
	import LobbyShell from "./lobby-shell.svelte";

	const DEV_AFFORDANCES =
		import.meta.env.VITE_GALAXY_DEV_AFFORDANCES === "true";

	onMount(async () => {
		// Bootstrap the lobby client + fan-out; share the in-flight
		// promise the games-* screens would otherwise kick off.
		const client = await lobbyData.ensure();
		// `account.ensure` is required for the paid-tier predicate.
		// Failures (e.g. transient network blip) fall back to the
		// non-paid path quietly — `account.current` simply stays null.
		if (client !== null) {
			try {
				await account.ensure(client);
			} catch {
				/* not fatal — first-visible falls back to recruitment. */
			}
		}
		const navState: LobbyNavState = {
			hasMyGames: lobbyData.myGames.length > 0,
			hasInvitations: lobbyData.invitations.length > 0,
			isPaidOrDev:
				DEV_AFFORDANCES || account.current?.entitlement.isPaid === true,
		};
		// Guard against the user navigating away mid-await (signOut,
		// browser Back). If the screen state has changed since mount,
		// leave it alone.
		if (appScreen.screen === "lobby") {
			appScreen.go(firstVisibleGamesScreen(navState));
		}
	});
</script>

<LobbyShell>
	<p role="status">{i18n.t("lobby.list_loading")}</p>
</LobbyShell>
