/** Seasonal factor chart renderers. */
import { html, svg, nothing } from "lit";
import { t, formatNumber } from "../styles";
import { px } from "./chart-utils";
import { haToday } from "../helpers/ha-time";
import type { MaintenanceTask, AdvancedFeatures } from "../types";
const MONTH_KEYS = [
"month_jan", "month_feb", "month_mar", "month_apr",
"month_may", "month_jun", "month_jul", "month_aug",
"month_sep", "month_oct", "month_nov", "month_dec",
];
/** HA's current month (0 = Jan) — the one the backend applies the factor of. */
function haMonth(): number {
return Number(haToday().slice(5, 7)) - 1;
}
export function renderSeasonalCardCompact(task: MaintenanceTask, lang: string, features: AdvancedFeatures) {
if (!features.seasonal || !task.seasonal_factor || task.seasonal_factor === 1.0) {
return nothing;
}
const months = MONTH_KEYS.map(k => t(k, lang));
const currentMonth = haMonth();
const realFactors = task.seasonal_factors || task.interval_analysis?.seasonal_factors || null;
const seasonalData = realFactors && realFactors.length === 12
? realFactors
: months.map((_, i) => {
const base = task.seasonal_factor || 1.0;
const variation = Math.sin((i - 6) * Math.PI / 6) * 0.3;
return Math.max(0.7, Math.min(1.3, base + variation));
});
return html`
${t("seasonal_awareness", lang)}
${seasonalData.map((factor, i) => {
const height = factor * 40;
const colorClass = factor < 0.9 ? 'low' : factor > 1.1 ? 'high' : 'normal';
const isCurrentMonth = i === currentMonth;
return html`
`;
})}
${t("shorter", lang)}
${t("normal", lang)}
${t("longer", lang)}
`;
}
export function renderSeasonalCardExpanded(task: MaintenanceTask, lang: string) {
return renderSeasonalChart(task, lang);
}
// Module-private — only the `renderSeasonalCardExpanded` wrapper above is part
// of the renderers/ public surface. Drop the keyword so esbuild sees the
// symbol as internal and tree-shakes it cleanly when the wrapper changes.
function renderSeasonalChart(task: MaintenanceTask, lang: string) {
const factors = task.seasonal_factors
?? task.interval_analysis?.seasonal_factors;
if (!factors || factors.length !== 12) return nothing;
const reason = task.interval_analysis?.seasonal_reason;
const currentMonth = haMonth();
const W = 300, H = 100;
const PAD_T = 8, PAD_B = 4;
const chartH = H - PAD_T - PAD_B;
const maxFactor = Math.max(...factors, 1.5);
const barW = W / 12;
const barInner = barW * 0.65;
const baselineY = PAD_T + chartH - (1.0 / maxFactor) * chartH;
return html`
${t("seasonal_chart_title", lang)}
${reason ? html`${reason === "learned" ? t("seasonal_learned", lang) : t("seasonal_manual", lang)}` : nothing}
${MONTH_KEYS.map((key, i) =>
html`${t(key, lang)}`
)}
`;
}