<!--
Phase 23 turn-report route. The orchestrator renders the table of
contents and the twenty sections; scroll save/restore is wired
through SvelteKit's `Snapshot` API on this route file.
`window.scrollY` is captured before navigating away and restored
after `afterNavigate` re-mounts the route. The in-game shell
layout expands the active-view-host to fit content rather than
constraining its own height, so the document body is what scrolls
— hence `window.scroll` rather than a host-element scrollTop.

A short `requestAnimationFrame` poll waits for the body to grow
tall enough to honour the saved offset, because the captured
position usually exceeds the viewport height before the sections
mount on return navigation.
-->
<script lang="ts">
	import type { Snapshot } from "@sveltejs/kit";

	import ReportView from "$lib/active-view/report.svelte";

	function restoreScroll(target: number): void {
		if (target <= 0) return;
		let attempts = 60;
		const tick = (): void => {
			const need = target + window.innerHeight;
			const have = document.documentElement.scrollHeight;
			if (have >= need || attempts === 0) {
				window.scrollTo(0, target);
				return;
			}
			attempts -= 1;
			requestAnimationFrame(tick);
		};
		requestAnimationFrame(tick);
	}

	export const snapshot: Snapshot<{ scrollY: number }> = {
		capture() {
			return { scrollY: window.scrollY };
		},
		restore(value) {
			restoreScroll(value.scrollY);
		},
	};
</script>

<ReportView />
