import { PieChart, Pie, Cell, Tooltip, Legend, ResponsiveContainer } from 'recharts'
import { useChartTheme } from '../../hooks/useChartTheme'
const COLORES = {
Urbana: '#252C61',
Rural: '#E8881A',
'Sin datos': '#94A3B8',
}
const RADIAN = Math.PI / 180
function normalizarZona(valor) {
const texto = String(valor ?? '').trim().toLowerCase()
if (!texto) return 'Sin datos'
if (texto.includes('urb')) return 'Urbana'
if (texto.includes('rur')) return 'Rural'
return 'Sin datos'
}
export default function ZonaOcurrencia({ siniestros }) {
const { tooltipBg, tooltipBorder, tooltipLabel, tickColor } = useChartTheme()
const conteo = { Urbana: 0, Rural: 0, 'Sin datos': 0 }
siniestros.forEach((s) => {
const zona = normalizarZona(s.zona_ocurrencia)
conteo[zona] += 1
})
const data = Object.entries(conteo)
.map(([name, value]) => ({ name, value }))
.filter((item) => item.value > 0)
const total = data.reduce((acc, d) => acc + d.value, 0)
const renderLabel = ({ cx, cy, midAngle, innerRadius, outerRadius, percent }) => {
if (percent < 0.04) return null
const r = innerRadius + (outerRadius - innerRadius) * 0.5
const x = cx + r * Math.cos(-midAngle * RADIAN)
const y = cy + r * Math.sin(-midAngle * RADIAN)
return (