import { resolveDate } from './date.ts' import { buildWeatherResponse } from './format.ts' import { fetchWeatherData } from './open-meteo.ts' import { resolveWeatherLocation } from './weather-geocode.ts' export type WeatherLookupParams = { location?: string latitude?: number longitude?: number label?: string date?: string hour?: number | null hourlyLimit?: number units?: 'metric' | 'imperial' } /** Daily/hourly weather lookup with cached fallback when live Open-Meteo is degraded. */ export async function weatherLookup(params: WeatherLookupParams = {}) { const input = typeof params === 'object' && params ? params : {} const units = input.units === 'metric' ? 'metric' : 'imperial' const date = resolveDate(input.date, new Date()) const hour = input.hour === null || input.hour === undefined ? null : Number(input.hour) const resolvedLocation = await resolveWeatherLocation(input) const weather = await fetchWeatherData({ latitude: resolvedLocation.latitude, longitude: resolvedLocation.longitude, date, units, }) return buildWeatherResponse({ data: weather.data, date, displayName: resolvedLocation.displayName, latitude: resolvedLocation.latitude, longitude: resolvedLocation.longitude, locationSource: resolvedLocation.source, hour, hourlyLimit: input.hourlyLimit, units, isHistorical: weather.isHistorical, isForecast: weather.isForecast, source: weather.source, stale: weather.stale, attempts: weather.attempts, recordedAt: weather.recordedAt, fallbackError: weather.fallbackError, }) } export default weatherLookup