// Number formatters and lookup helpers reused across the report-section
// components. The numeric formatters delegate to the project-wide
// `lib/util/number-format` so inspector, report tables, and the
// calculator panel all render numbers identically.

import type { ReportPlanet } from "../../../api/game-state";
import {
	formatFloat as formatFloatBase,
	formatInt,
	formatPercent as formatPercentBase,
} from "$lib/util/number-format";

/**
 * formatPercent renders a `[0, 1]` fraction as a one-decimal percent
 * (without a `%` suffix — the column header carries the unit).
 * Re-exported from the shared util for backwards-compatible imports
 * across the report sections.
 */
export const formatPercent = formatPercentBase;

/**
 * formatCount renders an integer-ish value (population, industry,
 * planet count, …) with zero fractional digits and no thousand
 * separators. Alias of the shared `formatInt`.
 */
export const formatCount = formatInt;

/**
 * formatFloat renders an engine `Float` (Fixed3-quantised) with three
 * fractional digits and no thousand separators. Used for stockpiles,
 * distances, cost, mass, tech levels — every report payload that is
 * neither an integer count nor a `[0, 1]` fraction.
 */
export const formatFloat = formatFloatBase;

/**
 * formatVotes renders a vote weight. Votes travel as the same `Float`
 * shape as every other float field, so this is a semantic alias of
 * `formatFloat` kept for readability at the call site.
 */
export const formatVotes = formatFloatBase;

/**
 * planetLabel renders a planet reference as `#<number> (<name>)` if
 * the planet is known in the report, or just `#<number>` if the
 * lookup fails (visibility lost between turns, foreign-only data).
 * Sections that show planet numbers without a name column —
 * Ships in Production, Bombings — rely on this resolver to keep
 * cell width tight.
 */
export function planetLabel(
	number: number,
	planets: readonly ReportPlanet[],
): string {
	const p = planets.find((row) => row.number === number);
	if (p === undefined || p.name === "") return `#${number}`;
	return `#${number} (${p.name})`;
}
