// Navigation helpers for the in-game report view. The two entry
// points (planet inspector + map) used to scroll-into-view the
// bombing row in slightly different ways; F8-12 / #30 consolidated
// the path so the bombing CirclePrim could go away.

/**
 * scrollToBombingRow polls a bounded number of animation frames for
 * the matching `[data-testid="report-bombing-row"][data-planet="N"]`
 * row to mount and, once it does, scrolls it into the centre of the
 * viewport. The poll is bounded (no infinite loop) because the report
 * view is rendered reactively after the active-view switch and the
 * row is not in the DOM on the same frame the caller requested it.
 */
export function scrollToBombingRow(planet: number): void {
	if (typeof document === "undefined") return;
	let attempts = 60;
	const tick = (): void => {
		const row = document.querySelector(
			`[data-testid="report-bombing-row"][data-planet="${planet}"]`,
		);
		if (row instanceof HTMLElement) {
			row.scrollIntoView({ behavior: "smooth", block: "center" });
			return;
		}
		attempts -= 1;
		if (attempts <= 0) return;
		requestAnimationFrame(tick);
	};
	requestAnimationFrame(tick);
}
