import { normalizeTargetCelsius } from './fan1SetpointResolve.ts'; export type CoolerSettingSnapshot = { lv1?: { target_on?: number | string | null }; lv2?: { target_on?: number | string | null }; lv3?: { target_on?: number | string | null }; }; type CoolerLevel = { id: 'lv1' | 'lv2' | 'lv3'; thresholdC: number }; function coolerLevelsDescending(cooler: CoolerSettingSnapshot | undefined): CoolerLevel[] { if (!cooler) return []; const entries: CoolerLevel[] = []; for (const id of ['lv3', 'lv2', 'lv1'] as const) { const thresholdC = normalizeTargetCelsius(cooler[id]?.target_on); if (thresholdC !== null) entries.push({ id, thresholdC }); } entries.sort((a, b) => b.thresholdC - a.thresholdC); return entries; } /** * When cooler is on: pick the highest level whose `target_on` is still below average inside temp. * If the cooler is active but temp is below lv1, keep showing lv1 instead of a blank setpoint. */ export function resolveCoolerDisplayTargetCelsius( avgInsideTemp: number | null, cooler: CoolerSettingSnapshot | undefined ): number | null { const levels = coolerLevelsDescending(cooler); if (levels.length === 0) return null; if (avgInsideTemp !== null && Number.isFinite(avgInsideTemp)) { for (const level of levels) { if (avgInsideTemp > level.thresholdC) return level.thresholdC; } } return levels[levels.length - 1]?.thresholdC ?? null; }