// 첫눈 지도 계산 (웹 frontend/src/b/firstSnow.ts 가 쓴다). 지점·평년 날짜·눈 예보는 API(/v1/season/first-snow, backend/services/first_snow.json)에서 받는다. // 지도 그림·배치 계산은 단풍 지도(shared/lib/foliage.ts)와 같이 쓴다 import type { FirstSnowSpot } from '../api/client/types.gen' import { foliageInSeason, foliageToday, md, toXY } from './foliage' /** soon: 앞으로 7일 안에 눈 예보가 있음, before: 평년 첫눈 날짜가 아직 안 옴, passed: 평년 첫눈 날짜가 지남(눈 예보 없음) */ export type SnowStage = 'soon' | 'before' | 'passed' /** x·y 는 지도 그림 안 위치(0~1), normal 은 평년 첫눈 날짜('YYYY-MM-DD', 이번 겨울 기준), d 는 오늘부터 그날까지 남은 날 수, snowDate 는 눈 예보가 있는 첫 날 */ export type SnowSpot = { name: string; region: string; top: boolean; x: number; y: number; normal: string; d: number; stage: SnowStage; snowDate: string | null; snowCm: number | null } export const SNOW_STAGE: Record = { soon: '눈 예보', before: '평년 첫눈 전', passed: '평년 첫눈 지남' } // 첫눈 지도는 단풍 지도가 끝난 다음 날부터(단풍철이 아니면 SEASON_START 부터) SEASON_END 까지 보인다. 그다음 날부터 해돋이 지도(shared/lib/sunrise.ts) // 평년 첫눈이 가장 이른 대관령이 11/5, 가장 늦은 창원·통영이 12/27 이지만, 해돋이 지도가 새해 전에 충분히 보이도록 12/20 에 끝낸다 const SEASON_START = '11-01' const SEASON_END = '12-20' const DAY = 86_400_000 const days = (from: string, to: string) => Math.round((Date.parse(to) - Date.parse(from)) / DAY) /** 오늘이 첫눈 지도철인지 (오늘 아이콘 줄의 첫눈 지도 칸·피드 첫눈 카드를 보일지) */ export function firstSnowInSeason(today: string = foliageToday()) { const monthDay = today.slice(5) return monthDay >= SEASON_START && monthDay <= SEASON_END && !foliageInSeason(today) } /** today 기준 지점별 단계, 평년 첫눈이 이른 순(같으면 이름 순). 1~6월에 보면 지난겨울 날짜로 본다 */ export function firstSnowOn(today: string, raw: FirstSnowSpot[]): SnowSpot[] { const year = Number(today.slice(0, 4)) - (Number(today.slice(5, 7)) <= 6 ? 1 : 0) return raw .map((s) => { const normal = `${year}-${s.normal}` const d = days(today, normal) const snowDate = s.snow_date ?? null const stage: SnowStage = snowDate ? 'soon' : d >= 0 ? 'before' : 'passed' return { name: s.name, region: s.region, top: !!s.top, ...toXY(s.lat, s.lon), normal, d, stage, snowDate, snowCm: s.snow_cm ?? null } }) .sort((a, b) => a.normal.localeCompare(b.normal) || a.name.localeCompare(b.name)) } /** 눈 예보가 있는 곳: 예보 날짜가 가까운 순, 같으면 눈이 많이 오는 순 */ export const snowSoon = (spots: SnowSpot[]) => spots.filter((m) => m.stage === 'soon').sort((a, b) => (a.snowDate ?? '').localeCompare(b.snowDate ?? '') || (b.snowCm ?? 0) - (a.snowCm ?? 0) || a.name.localeCompare(b.name)) /** '눈 예보: 전북·충남·강원'(눈 예보가 있는 지점이 많은 시·도 3곳). 없으면 아직 없다고, 예보를 못 받았으면 평년 날짜라고 */ export function firstSnowHeadline(spots: SnowSpot[], forecast: boolean) { if (!forecast) return '지역별 평년 첫눈 날짜예요' const soon = spots.filter((m) => m.stage === 'soon') if (!soon.length) return '이번 주 눈 예보는 아직 없어요' const count = new Map() for (const m of soon) count.set(m.region, (count.get(m.region) ?? 0) + 1) return `눈 예보: ${[...count].sort((a, b) => b[1] - a[1]).slice(0, 3).map(([p]) => p).join('·')}` } /** 지점 옆에 적는 한 줄: 눈 예보가 있으면 '11/27 눈 예보', 없으면 '평년 11/29' */ export const snowNote = (m: SnowSpot) => (m.snowDate ? `${md(m.snowDate)} 눈 예보` : `평년 ${md(m.normal)}`)