import type { TelemetrySnapshot } from "../../../types/telemetry";

export function findTelemetryByKey(
    snapshots: TelemetrySnapshot[],
    key: string,
): TelemetrySnapshot | null {
    return snapshots.find((snapshot) => snapshot.key === key) ?? null;
}

export function formatTelemetryValue(
    snapshot: TelemetrySnapshot | null,
    fallback = "--",
): string {
    if (!snapshot || snapshot.value === null || snapshot.value === undefined) {
        return fallback;
    }

    if (typeof snapshot.value === "boolean") {
        return snapshot.value ? "Sim" : "Não";
    }

    if (typeof snapshot.value === "number") {
        return Number.isInteger(snapshot.value)
            ? snapshot.value.toString()
            : snapshot.value.toFixed(1);
    }

    return String(snapshot.value);
}