/**
 * The Diagnostics search parameters and the API filter they build.
 *
 * The route, the page and the two infinite queries all have to agree on what
 * the URL asked for — the page renders the same window the loader prefetched —
 * so the projection lives in one place rather than being spelled out at each.
 */

import type { DiagnosticSeverity, DiagnosticState, DiagnosticsFilter } from "@/lib/types";

export interface DiagnosticsSearch {
	state?: DiagnosticState;
	severity?: DiagnosticSeverity;
	component?: string;
	/** Unix seconds, inclusive. An episode qualifies when its interval overlaps. */
	since?: number;
	until?: number;
}

/** Field by field, so an unset filter is an absent key rather than `undefined`. */
export function diagnosticsFilterOf(search: DiagnosticsSearch): DiagnosticsFilter {
	const filter: DiagnosticsFilter = {};
	if (search.severity !== undefined) filter.severity = search.severity;
	if (search.component !== undefined) filter.component = search.component;
	if (search.since !== undefined) filter.since = search.since;
	if (search.until !== undefined) filter.until = search.until;
	return filter;
}
