"use client"; import { useMemo } from "react"; import { AlertTriangle, BellRing, CloudSun, RefreshCw } from "lucide-react"; import { EmptyState } from "@/components/states/empty-state"; import { LoadingSkeleton } from "@/components/states/loading-skeleton"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { useForecast } from "@/hooks/use-forecast"; import { useWarnings } from "@/hooks/use-warnings"; import { DEFAULT_STATION_ID } from "@/lib/constants"; import { useI18n } from "@/lib/i18n"; import { getProvinceForSelection } from "@/lib/provinces"; import { useWeatherStore } from "@/lib/store"; import { buildTomorrowWeatherBrief, buildWeatherBrief } from "@/lib/weather-brief"; import type { WeatherRegion } from "@/types/weather-region"; export function WeatherBriefCard({ latitude, longitude, region = "PL", locationName }: { latitude?: number; longitude?: number; region?: WeatherRegion; locationName: string }) { const { language, t } = useI18n(); const { data: forecast, isPending, isError, refetch } = useForecast(latitude, longitude, region); const { data: warnings = [] } = useWarnings(); const selectedStationId = useWeatherStore((state) => state.selectedStationId); const selectedLocation = useWeatherStore((state) => state.selectedLocation); const temperatureUnit = useWeatherStore((state) => state.temperatureUnit); const windSpeedUnit = useWeatherStore((state) => state.windSpeedUnit); const isGlobalLocation = region === "GLOBAL"; const province = isGlobalLocation ? null : getProvinceForSelection(selectedLocation?.province, selectedStationId ?? DEFAULT_STATION_ID); const relevantWarnings = useMemo(() => isGlobalLocation ? [] : warnings, [isGlobalLocation, warnings]); const brief = useMemo(() => { if (!forecast) return null; return buildWeatherBrief({ forecast, warnings: relevantWarnings, province, countyTeryt: isGlobalLocation ? null : selectedLocation?.countyTeryt, locationName, language, temperatureUnit, windSpeedUnit }); }, [forecast, isGlobalLocation, language, locationName, province, relevantWarnings, selectedLocation?.countyTeryt, temperatureUnit, windSpeedUnit]); const tomorrowBrief = useMemo(() => { if (!forecast) return null; return buildTomorrowWeatherBrief({ forecast, warnings: relevantWarnings, province, countyTeryt: isGlobalLocation ? null : selectedLocation?.countyTeryt, locationName, language, temperatureUnit, windSpeedUnit }); }, [forecast, isGlobalLocation, language, locationName, province, relevantWarnings, selectedLocation?.countyTeryt, temperatureUnit, windSpeedUnit]); if (!Number.isFinite(latitude) || !Number.isFinite(longitude) || isPending) { return ; } if (isError || !forecast) { return (

{t("brief.error")}

); } if (!brief) { return ; } const isWarning = brief.severity === "warning"; return (
{isWarning ?

{t("brief.label")}

{brief.headline}

{t("brief.description", { location: locationName })}

    {brief.body.slice(0, 4).map((line) => (
  • {line}
  • ))}
{tomorrowBrief && (

{t("brief.tomorrowLabel")}

{tomorrowBrief.headline}

    {tomorrowBrief.body.slice(0, 3).map((line) => (
  • {line}
  • ))}
)}

); }