export interface ChartRangeOption {
	value: number;
	label: string;
}

/** Shared range choices used by every time-series chart. */
export const DEFAULT_CHART_RANGES: ChartRangeOption[] = [
	{ value: 900, label: "15 minutes" },
	{ value: 1800, label: "30 minutes" },
	{ value: 3600, label: "1 hour" },
	{ value: 21600, label: "6 hours" },
	{ value: 86400, label: "24 hours" },
	{ value: 604800, label: "7 days" },
];

function formatRangeLabel(seconds: number): string {
	if (seconds % 604800 === 0) return `${seconds / 604800} days`;
	if (seconds % 3600 === 0) return `${seconds / 3600} hours`;
	if (seconds % 60 === 0) return `${seconds / 60} minutes`;
	return `${seconds} seconds`;
}

export function chartRangesThrough(maxSeconds: number): ChartRangeOption[] {
	if (!Number.isFinite(maxSeconds) || maxSeconds <= 0) {
		return [DEFAULT_CHART_RANGES[0]];
	}
	const ranges = DEFAULT_CHART_RANGES.filter(
		(range) => range.value < maxSeconds,
	);
	const exact = DEFAULT_CHART_RANGES.find(
		(range) => range.value === maxSeconds,
	);
	return exact
		? [...ranges, exact]
		: [...ranges, { value: maxSeconds, label: formatRangeLabel(maxSeconds) }];
}

export function rangeSecondsFromWindow(window: unknown): number {
	const values: Record<string, number> = {
		"15m": 900,
		"30m": 1800,
		"1h": 3600,
		"6h": 21600,
		"24h": 86400,
		"7d": 604800,
	};
	return values[String(window)] ?? 3600;
}
