// composables/useWeatherConstants.ts
// 集中管理气象页面的所有阈值、等级定义和筛选字段配置

/** 降雨等级定义（统一用于筛选、图例、分布计算） */
export const RAIN_LEVELS = [
  { label: "无雨", min: -Infinity, max: 0.1 },
  { label: "小雨", min: 0.1, max: 10 },
  { label: "中雨", min: 10, max: 25 },
  { label: "大雨", min: 25, max: 50 },
  { label: "暴雨", min: 50, max: 100 },
  { label: "大暴雨", min: 100, max: 250 },
  { label: "特大暴雨", min: 250, max: Infinity }
] as const;

/** 可筛选的降雨等级（排除"无雨"，因为无雨通常单独判断） */
export const RAIN_FILTER_LEVELS = RAIN_LEVELS.filter((l) => l.label !== "无雨");

/** 格式化数值：null/undefined → "—"，否则原样输出 */
export function fmtVal(v: number | null | undefined, fixed?: number): string {
  if (v == null || isNaN(v)) return "—";
  return fixed != null ? v.toFixed(fixed) : String(v);
}

/** 根据降雨量返回等级标签 */
export function rainLevelLabel(v: number | null | undefined | string): string {
  if (v == null || v === "") return "—";
  const n = Number(v);
  if (isNaN(n)) return "—";
  for (const level of RAIN_LEVELS) {
    if (n >= level.min && n < level.max) return level.label;
  }
  return "—";
}

// ---------- 温度阈值 ----------

export const TEMP_THRESHOLDS = {
  tmax: { extreme: 38, danger: 35, warn: 30 },
  tmin: { extreme: 0, cold: 5 }
} as const;

/** 最高温 CSS 类名（null 返回空字符串，不标色） */
export function tmaxValClass(v: number | null | undefined): string {
  if (v == null || isNaN(v)) return "";
  if (v >= TEMP_THRESHOLDS.tmax.extreme) return "val-extreme";
  if (v >= TEMP_THRESHOLDS.tmax.danger) return "val-danger";
  if (v >= TEMP_THRESHOLDS.tmax.warn) return "val-warn";
  return "";
}

/** 最低温 CSS 类名 */
export function tminValClass(v: number | null | undefined): string {
  if (v == null || isNaN(v)) return "";
  if (v <= TEMP_THRESHOLDS.tmin.extreme) return "val-cold-x";
  if (v <= TEMP_THRESHOLDS.tmin.cold) return "val-cold";
  return "";
}

/** 降雨量 CSS 类名 */
export function rainValClass(v: number | null | undefined): string {
  if (v == null || isNaN(v)) return "";
  if (v >= 100) return "val-extreme";
  if (v >= 50) return "val-danger";
  if (v >= 25) return "val-warn";
  return "";
}

// ---------- 筛选字段定义 ----------

export interface TempFilterField {
  key: string;
  label: string;
  range: boolean;
}

export const TEMP_FILTER_FIELDS: TempFilterField[] = [
  { key: "tmaxF", label: "最高气温", range: true },
  { key: "tminF", label: "最低气温", range: true },
  { key: "tavgF", label: "平均气温", range: false }
];

export const FILTER_KEYS = ["rainfall", "tmaxF", "tminF", "tavgF"] as const;

// ---------- 其他常量 ----------

export const YEAR_PRESETS = [10, 20, 30, 50, 60];
export const PREVIEW_COUNT = 8;
export const MIN_YEAR = 1960;
export const MAX_YEAR_SPAN = 100;
