import { useMemo } from "react";
import type { ChartVariable } from "../../telemetry/types/telemetryCatalog";
import { useMeteoModuleStream } from "./useMeteoModuleStream";

export function useMeteoChartCatalog() {
    const { sensors, connected } = useMeteoModuleStream();

    const chartableVariables = useMemo<ChartVariable[]>(
        () =>
            sensors.map((sensor) => ({
                sensorId: sensor.sensorId,
                key: sensor.key,
                label: sensor.name,
                value:
                    typeof sensor.value === "number" ||
                        typeof sensor.value === "string" ||
                        typeof sensor.value === "boolean" ||
                        sensor.value === null
                        ? sensor.value
                        : null,
                unit: sensor.unit ?? "",
                timestamp: sensor.timestamp,
                category: "Meteo",
                group: "Meteorologia",
                chartable: true,
            })),
        [sensors],
    );

    return {
        chartableVariables,
        connected,
        sensorCount: sensors.length,
    };
}