import Link from 'next/link' import { TerritorySortableHead } from '@/components/campaign/municipality/TerritorySortableHead' import { CampaignTransitionAnchor } from '@/components/campaign/shared/CampaignListPending' import type { CampaignTableColumn } from '@/components/campaign/shared/CampaignTable' import { Badge } from '@/components/ui/Badge' import { formatElectionNumber } from '@/lib/electionFormat' import { formatGoalCoverageDeficitShortLabel, formatGoalCoverageRatioLabel, formatRatioAsPercentLabel, } from '@/utilities/municipality/goalCoverage' import { formatTerritorialClassWhy, territorialClassBadgeVariant, territorialClassLabels, } from '@/utilities/municipality/municipalityLabels' import type { TerritoryFilterOption } from '@/utilities/territory/territoryListFilters' import { territoryColumnDescriptions } from '@/utilities/territory/territoryListLabels' import { territoryListSortLabels, type TerritoryListState, } from '@/utilities/territory/territoryListUrl' import type { TerritoryTableRow } from '@/utilities/territory/territoryOverview' const percentFormatter = new Intl.NumberFormat('pt-BR', { minimumFractionDigits: 1, maximumFractionDigits: 1, }) const formatPercent = (ratio: number): string => `${percentFormatter.format(ratio * 100)}%` const YearSeriesCell = ({ votesByYear }: { votesByYear: Record }) => { const prior = [ { year: '2014', value: votesByYear['2014'] ?? 0 }, { year: '2018', value: votesByYear['2018'] ?? 0 }, ] return (
{formatElectionNumber(votesByYear['2022'] ?? 0)} {prior.map(({ year, value }) => `${year}: ${formatElectionNumber(value)}`).join(' · ')}
) } const CoverageCell = ({ withAdvisorCount, total, muted, }: { withAdvisorCount: number total: number muted: boolean }) => { if (total === 0) return — if (withAdvisorCount === 0) { return 0 de {total} } return ( {withAdvisorCount} de {total} ) } const TerritorialClassCell = ({ row }: { row: TerritoryTableRow }) => { const territorialClass = row.territorialClass if (!territorialClass) { return — } const why = formatTerritorialClassWhy(territorialClass.factors) if (territorialClass.class === 'sem_base') { return ( {why} ) } return ( <> {territorialClassLabels[territorialClass.class]} {why} ) } const captureTooltipContent = (row: TerritoryTableRow) => { const parts: string[] = [] if (row.medianCapture != null) { parts.push(`Mediana no território: ${formatRatioAsPercentLabel(row.medianCapture)}`) } if (row.captureMin != null && row.captureMax != null) { parts.push( `Amplitude: ${formatRatioAsPercentLabel(row.captureMin)} – ${formatRatioAsPercentLabel(row.captureMax)}`, ) } if (row.criticalMunicipality && row.criticalMunicipality.deficit > 0) { parts.push( `Município crítico (maior déficit de meta): ${row.criticalMunicipality.name} (faltam ${formatElectionNumber(row.criticalMunicipality.deficit)} votos)`, ) } if (row.captureBeacon) { parts.push( `Referência de captura no território: ${row.captureBeacon.name} (${formatRatioAsPercentLabel(row.captureBeacon.captureRate)})`, ) } if (parts.length === 0) { return 'Sem dados de captura 2022 neste recorte.' } return (
{parts.map((line) => (

{line}

))} {row.criticalMunicipality && row.criticalMunicipality.deficit > 0 ? ( Abrir município crítico ) : null}

A captura do território é a soma dos votos próprios ÷ soma dos tetos — não a média das capturas por município.

) } export const territoryListColumns = ({ state, regionOptions, }: { state: TerritoryListState regionOptions: TerritoryFilterOption[] }): Array> => [ { id: 'region', mandatory: true, label: territoryListSortLabels.region, head: ( ), cell: (row) => row.variant === 'parent' ? ( {row.region} ) : ( {row.label} ), cellClassName: (row) => `sticky left-0 z-[5] min-w-56 ${row.variant === 'sub' ? 'bg-muted' : 'bg-background'}`, }, { id: 'municipalities', label: territoryListSortLabels.municipalities, head: ( ), cell: (row) => row.municipalityCount, cellClassName: 'text-right tabular-nums', }, { id: 'votes2022', label: territoryListSortLabels.votes2022, head: ( ), cell: (row) => , cellClassName: 'text-right tabular-nums', }, { id: 'pct', label: territoryListSortLabels.pct, head: ( ), cell: (row) => formatPercent(row.pctPropriaVotacao), cellClassName: (row) => `text-right tabular-nums ${row.variant === 'parent' ? 'font-medium' : 'text-muted-foreground'}`, }, { id: 'validVotes2022', label: territoryListSortLabels.validVotes2022, head: ( ), cell: (row) => formatElectionNumber(row.validVotes2022), cellClassName: 'text-right tabular-nums text-muted-foreground', }, { id: 'estimate2026', label: territoryListSortLabels.estimate2026, head: ( ), cell: (row) => formatElectionNumber(row.estimate2026), cellClassName: (row) => `text-right tabular-nums ${row.variant === 'sub' ? 'text-muted-foreground' : ''}`, }, { id: 'cobertura', label: territoryListSortLabels.cobertura, head: ( ), cell: (row) => (
{formatGoalCoverageRatioLabel(row.goalCoverage)} {formatGoalCoverageDeficitShortLabel(row.goalCoverage)}
), cellClassName: 'text-right', }, { id: 'captura', label: territoryListSortLabels.captura, head: ( ), cell: (row) => formatRatioAsPercentLabel(row.captureRate), cellClassName: 'text-right tabular-nums', cellTooltip: (row) => captureTooltipContent(row), }, { id: 'classe', label: territoryListSortLabels.classe, head: ( ), cell: (row) => , cellClassName: 'text-right', cellTooltip: (row) => row.territorialClass ? formatTerritorialClassWhy(row.territorialClass.factors) : undefined, }, { id: 'coverage', label: territoryListSortLabels.coverage, head: ( ), cell: (row) => ( ), cellClassName: 'text-right', }, ]