<!--
Account-menu popover with Account / Settings / Sessions / Theme /
Language / Logout. Phase 10 only wires Language (via the existing
i18n primitive) and Logout (`session.signOut("user")`); the rest are
stub buttons that later phases (35 polish, dedicated phases for
Sessions and Theme) take over.
-->
<script lang="ts">
	import { onMount } from "svelte";
	import { i18n, SUPPORTED_LOCALES, type Locale } from "$lib/i18n/index.svelte";
	import { session } from "$lib/session-store.svelte";

	let open = $state(false);
	let rootEl: HTMLDivElement | null = $state(null);

	function toggleOpen(): void {
		open = !open;
	}

	async function logout(): Promise<void> {
		open = false;
		await session.signOut("user");
	}

	function selectLocale(event: Event): void {
		const value = (event.target as HTMLSelectElement).value as Locale;
		i18n.setLocale(value);
	}

	function onKeyDown(event: KeyboardEvent): void {
		if (event.key === "Escape" && open) {
			open = false;
		}
	}

	onMount(() => {
		const handleClick = (event: MouseEvent): void => {
			if (!open || rootEl === null) return;
			const target = event.target;
			if (target instanceof Node && rootEl.contains(target)) return;
			open = false;
		};
		document.addEventListener("click", handleClick, true);
		document.addEventListener("keydown", onKeyDown);
		return () => {
			document.removeEventListener("click", handleClick, true);
			document.removeEventListener("keydown", onKeyDown);
		};
	});
</script>

<div class="account-menu" bind:this={rootEl}>
	<button
		type="button"
		class="trigger"
		data-testid="account-menu-trigger"
		aria-haspopup="menu"
		aria-expanded={open}
		aria-label={i18n.t("game.shell.menu.account")}
		onclick={toggleOpen}
	>
		⚙
	</button>
	{#if open}
		<div class="surface" role="menu" data-testid="account-menu-list">
			<button type="button" role="menuitem" data-testid="account-menu-settings" disabled>
				{i18n.t("game.shell.menu.settings")}
			</button>
			<button type="button" role="menuitem" data-testid="account-menu-sessions" disabled>
				{i18n.t("game.shell.menu.sessions")}
			</button>
			<button type="button" role="menuitem" data-testid="account-menu-theme" disabled>
				{i18n.t("game.shell.menu.theme")}
			</button>
			<label class="locale" data-testid="account-menu-language">
				<span>{i18n.t("game.shell.menu.language")}</span>
				<select
					data-testid="account-menu-language-select"
					value={i18n.locale}
					onchange={selectLocale}
				>
					{#each SUPPORTED_LOCALES as entry (entry.code)}
						<option value={entry.code}>{entry.nativeName}</option>
					{/each}
				</select>
			</label>
			<button
				type="button"
				role="menuitem"
				data-testid="account-menu-logout"
				onclick={logout}
			>
				{i18n.t("game.shell.menu.logout")}
			</button>
		</div>
	{/if}
</div>

<style>
	.account-menu {
		position: relative;
	}
	.trigger {
		font: inherit;
		font-size: 1.1rem;
		padding: 0.25rem 0.6rem;
		background: transparent;
		color: inherit;
		border: 1px solid #2a3150;
		border-radius: 4px;
		cursor: pointer;
	}
	.trigger:hover {
		background: #1c2238;
	}
	.surface {
		position: absolute;
		top: calc(100% + 0.25rem);
		right: 0;
		min-width: 12rem;
		display: flex;
		flex-direction: column;
		background: #14182a;
		border: 1px solid #2a3150;
		border-radius: 6px;
		box-shadow: 0 6px 18px rgba(0, 0, 0, 0.4);
		z-index: 50;
	}
	.surface > button,
	.surface > label {
		text-align: left;
		font: inherit;
		padding: 0.45rem 0.75rem;
		background: transparent;
		color: inherit;
		border: 0;
		cursor: pointer;
	}
	.surface > button:hover:not(:disabled) {
		background: #1c2238;
	}
	.surface > button:disabled {
		opacity: 0.5;
		cursor: not-allowed;
	}
	.locale {
		display: flex;
		align-items: center;
		gap: 0.5rem;
	}
	.locale select {
		font: inherit;
		background: #1c2238;
		color: inherit;
		border: 1px solid #2a3150;
		border-radius: 4px;
		padding: 0.15rem 0.35rem;
	}
</style>
