import { useMemo, useState } from 'react' import { SummaryCard } from './SummaryCard' import { WeatherData } from '@/features/weather' import { GreenhouseForecastAlerts, getForecastAlertsCount } from './GreenhouseForecastAlerts' import { Dialog } from '@/components/common/Dialog' import { WeatherAlertBanner } from '@/components/alerts' import { Loader2 } from 'lucide-react' type SummaryTabProps = { temperature: number[] humidity: number[] soil: number[] gas: number[] lux: number[] forecastWeather?: WeatherData | null forecastWeatherLoading?: boolean onCardClick?: (param: string) => void } export function SummaryTab({ temperature, humidity, soil, gas, lux, forecastWeather, forecastWeatherLoading = false, onCardClick }: SummaryTabProps) { const [isAlertsDialogOpen, setIsAlertsDialogOpen] = useState(false) const alertsCount = useMemo(() => { return getForecastAlertsCount(forecastWeather ?? null) }, [forecastWeather]) // Memoized summary statistics for each parameter const temperatureSummary = useMemo(() => { if (temperature.length === 0) return { current: 0, min: 0, max: 0 } return { current: temperature.at(-1) ?? 0, min: Math.min(...temperature), max: Math.max(...temperature), } }, [temperature]) const humiditySummary = useMemo(() => { if (humidity.length === 0) return { current: 0, min: 0, max: 0 } return { current: humidity.at(-1) ?? 0, min: Math.min(...humidity), max: Math.max(...humidity), } }, [humidity]) const soilSummary = useMemo(() => { if (soil.length === 0) return { current: 0, min: 0, max: 0 } return { current: soil.at(-1) ?? 0, min: Math.min(...soil), max: Math.max(...soil), } }, [soil]) const gasSummary = useMemo(() => { if (gas.length === 0) return { current: 0, min: 0, max: 0 } return { current: gas.at(-1) ?? 0, min: Math.min(...gas), max: Math.max(...gas), } }, [gas]) const luxSummary = useMemo(() => { if (lux.length === 0) return { current: 0, min: 0, max: 0 } return { current: lux.at(-1) ?? 0, min: Math.min(...lux), max: Math.max(...lux), } }, [lux]) return ( <> {/* Greenhouse Forecast Alerts Section */} {forecastWeatherLoading ? (

در حال بارگذاری هشدارهای آب و هوایی...

لطفاً صبر کنید

) : alertsCount > 0 && forecastWeather ? ( setIsAlertsDialogOpen(true)} /> ) : null} {/* Summary Cards Grid */}
onCardClick?.('temperature')} /> onCardClick?.('humidity')} /> onCardClick?.('gas')} /> onCardClick?.('soil')} /> onCardClick?.('lux')} />
{/* Alerts Dialog */} {forecastWeather && ( setIsAlertsDialogOpen(false)} title="هشدارهای پیش‌بینی آب و هوا" > )} ) }