import { useState } from 'react';
import {
  Box, CircularProgress, Alert, Table, TableBody, TableCell, TableContainer,
  TableHead, TableRow, Paper, Typography, Dialog, DialogTitle,
  DialogContent, IconButton
} from '@mui/material';
import CloseIcon from '@mui/icons-material/Close';
import ArrowUpwardIcon from '@mui/icons-material/ArrowUpward';
import ArrowDownwardIcon from '@mui/icons-material/ArrowDownward';
import RemoveIcon from '@mui/icons-material/Remove';
import { formatFullDateTime, formatCurrency } from '../utils/formatters';
import type { CotizacionBolsa } from '../models/mercadoModels';
import { useApiData } from '../hooks/useApiData';
import { HistoricalChartWidget } from './HistoricalChartWidget';
import { PiChartLineUpBold } from 'react-icons/pi';
const Variacion = ({ value }: { value: number }) => {
  const color = value > 0 ? 'success.main' : value < 0 ? 'error.main' : 'text.secondary';
  const Icon = value > 0 ? ArrowUpwardIcon : value < 0 ? ArrowDownwardIcon : RemoveIcon;
  return (
    
      
      {value.toFixed(2)}%
    
  );
};
export const BolsaUsaWidget = () => {
  const { data, loading, error } = useApiData('/mercados/bolsa/eeuu');
  const [selectedTicker, setSelectedTicker] = useState(null);
  const handleRowClick = (ticker: string) => setSelectedTicker(ticker);
  const handleCloseDialog = () => setSelectedTicker(null);
  const otherStocks = data?.filter(d => d.ticker !== '^GSPC') || [];
  if (loading) {
    return ;
  }
  if (error) {
    return {error};
  }
  if (!data || data.length === 0) {
    return No hay datos disponibles para el mercado de EEUU.;
  }
  return (
    <>
      {/* Renderizamos la tabla solo si hay otras acciones */}
      {otherStocks.length > 0 && (
        
          
            
              Última actualización de acciones: {formatFullDateTime(otherStocks[0].fechaRegistro)}
            
          
          
            
              
                Símbolo
                Precio Actual
                
                Apertura
                Cierre Anterior
                % Cambio
                Historial
              
            
            
              {otherStocks.map((row) => (
                
                  {row.ticker}
                  {formatCurrency(row.precioActual, 'USD')}
                  
                  {formatCurrency(row.apertura, 'USD')}
                  {formatCurrency(row.cierreAnterior, 'USD')}
                  
                  
                  
                     handleRowClick(row.ticker)}
                      sx={{ boxShadow: '0 1px 3px rgba(0,0,0,0.1)', transition: 'all 0.2s ease-in-out', '&:hover': { transform: 'scale(1.1)', boxShadow: '0 2px 6px rgba(0,0,0,0.2)' } }}
                    >
                  
                
              ))}
            
          
        
      )}
      
    >
  );
};