/**
 * 风向角度 -> 风向代码（N/NE/E/SE/S/SW/W/NW）
 */
export function windDirectionCode(deg: number | null | string): string | null {
  if (deg == null || deg === "" || isNaN(Number(deg))) return null;
  const d = Number(deg);
  if (d < 0 || d > 360) return null;
  return ["N", "NE", "E", "SE", "S", "SW", "W", "NW"][Math.floor((d + 22.5) / 45) % 8];
}

/**
 * 风向角度 -> 风向中文标签（通过字典 wind_direction_type 映射）
 * @param deg 风向角度
 * @param dicts 字典列表
 */
export function windDirectionLabel(deg: number | null | string, dicts: any[]): string {
  const code = windDirectionCode(deg);
  if (!code) return "—"; // em dash
  const type = dicts.find((d: any) => d.dictType === "wind_direction_type");
  const entry = type?.dataList?.find((e: any) => e.dictValue === code);
  return entry?.dictLabel || code;
}
