import Link from 'next/link' import type { ReactElement } from 'react' import { TerritorySortableHead } from '@/components/campaign/municipality/TerritorySortableHead' import { CampaignTransitionAnchor } from '@/components/campaign/shared/CampaignListPending' import { CampaignTableHead, type CampaignTableColumn, } from '@/components/campaign/shared/CampaignTable' import type { MunicipalityRelationEntry } from '@/components/campaign/shared/MunicipalityRelationAvatarStack' import { MunicipalityRelationAvatarStack } from '@/components/campaign/shared/MunicipalityRelationAvatarStack' import { VoteEstimateScenarioStrip } from '@/components/campaign/votePledge/VoteEstimateScenarioStrip' import { Badge } from '@/components/ui/Badge' import { formatElectionNumber, formatVoteSharePercent } from '@/lib/electionFormat' import { stateDeputyDisplayName } from '@/lib/stateDeputyNameParty' import { formatVoteEstimateScenarioAriaLabel } from '@/lib/voteEstimate' import { formatGoalCoverageDeficitShortLabel, formatGoalCoverageRatioLabel, formatRatioAsPercentLabel, } from '@/utilities/municipality/goalCoverage' import { formatTerritorialClassWhy, territorialClassBadgeVariant, territorialClassLabels, } from '@/utilities/municipality/municipalityLabels' import type { TerritoryNetworkReferences, TerritoryStateDeputyReference, } from '@/utilities/territory/loadTerritoryOverview' 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 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.

) } /** B175 — 2022 cell mirroring the municípios pattern: % first, votes as the meta line. */ const TerritoryVoteReadout = ({ row }: { row: TerritoryTableRow }) => { const votes = row.votesByYear['2022'] ?? 0 const share = row.pctPropriaVotacao return (
{formatVoteSharePercent(share)} {formatElectionNumber(votes)} {/* The valid votes live only in the hover — keep them as sr-only text so the tooltip repeats, never invents. */} Votos válidos de deputado federal em 2022 no território:{' '} {formatElectionNumber(row.validVotes2022)}
) } /** B175 — 2026 read-only cell: central-scenario total + % as a secondary line; scenarios on hover. */ const TerritoryEstimateReadout = ({ row }: { row: TerritoryTableRow }) => { if (!row.hasEstimate) { return — } return (
{formatElectionNumber(row.estimateByScenario.central)} {formatVoteSharePercent(row.pctPropriaVotacao)} {/* Same redundancy contract as the cell's hover strip (municípios precedent). */} {formatVoteEstimateScenarioAriaLabel(row.estimateByScenario)}
) } // --------------------------------------------------------------------------- // B175 — read-only network columns (Assessor / Liderança / Dobradinha) // --------------------------------------------------------------------------- const territoryNetworkColumnDescriptions = { advisor: 'Assessores de staff atribuídos nos municípios do território. Nomes no detalhe da célula.', leadership: 'Lideranças vinculadas aos municípios do território. Nomes no detalhe da célula.', stateDeputy: 'Deputados estaduais em dobradinha nos municípios do território. Nomes no detalhe da célula.', } as const const territoryNetworkColumnLabels = { advisor: 'Assessor', leadership: 'Liderança', stateDeputy: 'Dobradinha', } as const const advisorEntries = ( row: TerritoryTableRow, advisorNamesById: TerritoryNetworkReferences['advisorNamesById'], ): MunicipalityRelationEntry[] => row.advisorIDs.flatMap((id) => { const advisor = advisorNamesById.get(id) return advisor ? [{ id: advisor.id, label: advisor.name }] : [] }) const leadershipEntries = ( row: TerritoryTableRow, leadershipNamesById: TerritoryNetworkReferences['leadershipNamesById'], ): MunicipalityRelationEntry[] => row.leadershipIDs.flatMap((id) => { const leadership = leadershipNamesById.get(id) return leadership ? [ { id: leadership.id, label: leadership.name, href: `/campanha/liderancas/${leadership.id}`, }, ] : [] }) const stateDeputyEntries = ( row: TerritoryTableRow, stateDeputyById: TerritoryNetworkReferences['stateDeputyById'], ): MunicipalityRelationEntry[] => row.stateDeputyIDs.flatMap((id) => { const deputy: TerritoryStateDeputyReference | undefined = stateDeputyById.get(id) if (!deputy) return [] return [ { id: deputy.id, label: stateDeputyDisplayName(deputy.plainName, deputy.party), initialsLabel: deputy.plainName, href: deputy.href, }, ] }) const namesTooltip = (entries: MunicipalityRelationEntry[]): ReactElement | null => entries.length === 0 ? null : (
{entries.map((entry) => ( {entry.label} ))}
) const NetworkCell = ({ entries }: { entries: MunicipalityRelationEntry[] }) => ( —} maxVisible={3} /> ) /** * B175 — column widths keep the density ladder: P0 core is always on, one * column joins per panel-width rung, never a horizontal scrollbar. */ const responsiveColumnClassName = { captura: 'hidden @min-[44rem]/territory-list:table-cell', classe: 'hidden @min-[50rem]/territory-list:table-cell', coverage: 'hidden @min-[54rem]/territory-list:table-cell', advisor: 'hidden @min-[60rem]/territory-list:table-cell', leadership: 'hidden @min-[66rem]/territory-list:table-cell', stateDeputy: 'hidden @min-[72rem]/territory-list:table-cell', cobertura: 'hidden @min-[78rem]/territory-list:table-cell', } as const type TerritoryListColumnsProps = { state: TerritoryListState regionOptions: TerritoryFilterOption[] references: TerritoryNetworkReferences } export const territoryListColumns = ({ state, regionOptions, references, }: TerritoryListColumnsProps): Array> => { const { advisorNamesById, leadershipNamesById, stateDeputyById } = references return [ { id: 'region', mandatory: true, label: territoryListSortLabels.region, head: ( ), cell: (row) => row.variant === 'parent' ? ( {row.region}{' '} ({row.municipalityCount}) ) : ( // The Metropolitano sub-labels already carry a count group ("Salvador (19 zonas)"), // so the count rides the name only when the label has none — never stacking "(19 zonas) (19)". {row.label}{' '} {/\(\d[^)]*\)$/.test(row.label) ? null : ( ({row.municipalityCount}) )} ), cellClassName: (row) => `sticky left-0 z-[5] min-w-56 ${row.variant === 'sub' ? 'bg-muted' : 'bg-background'}`, }, { id: 'votes2022', label: territoryListSortLabels.votes2022, head: ( ), cell: (row) => , cellClassName: 'text-right tabular-nums', // The valid-votes figure is the cell's exclusive hover content (B175). cellTooltip: (row) => `Votos válidos de deputado federal em 2022 no território: ${formatElectionNumber(row.validVotes2022)}`, }, { id: 'captura', label: territoryListSortLabels.captura, head: ( ), cell: (row) => formatRatioAsPercentLabel(row.captureRate), cellClassName: (row) => `${responsiveColumnClassName.captura} text-right tabular-nums ${ row.variant !== 'parent' ? 'text-muted-foreground' : '' }`, cellTooltip: (row) => captureTooltipContent(row), }, { id: 'estimate2026', label: territoryListSortLabels.estimate2026, head: ( ), cell: (row) => , cellClassName: (row) => `text-right tabular-nums ${row.variant === 'sub' ? 'text-muted-foreground' : ''}`, cellTooltip: (row) => row.hasEstimate ? (
) : null, }, { id: 'advisor', label: territoryNetworkColumnLabels.advisor, head: ( {territoryNetworkColumnLabels.advisor} ), cellClassName: responsiveColumnClassName.advisor, cell: (row) => , cellTooltip: (row) => namesTooltip(advisorEntries(row, advisorNamesById)), }, { id: 'leadership', label: territoryNetworkColumnLabels.leadership, head: ( {territoryNetworkColumnLabels.leadership} ), cellClassName: responsiveColumnClassName.leadership, cell: (row) => , cellTooltip: (row) => namesTooltip(leadershipEntries(row, leadershipNamesById)), }, { id: 'stateDeputy', label: territoryNetworkColumnLabels.stateDeputy, head: ( {territoryNetworkColumnLabels.stateDeputy} ), cellClassName: responsiveColumnClassName.stateDeputy, cell: (row) => , cellTooltip: (row) => namesTooltip(stateDeputyEntries(row, stateDeputyById)), }, { id: 'classe', label: territoryListSortLabels.classe, head: ( ), cell: (row) => , cellClassName: (row) => `${responsiveColumnClassName.classe} text-right ${ row.variant !== 'parent' ? 'text-muted-foreground' : '' }`, cellTooltip: (row) => row.territorialClass ? formatTerritorialClassWhy(row.territorialClass.factors) : undefined, }, { id: 'coverage', label: territoryListSortLabels.coverage, head: ( ), cell: (row) => ( ), cellClassName: `${responsiveColumnClassName.coverage} text-right`, }, { id: 'cobertura', label: territoryListSortLabels.cobertura, head: ( ), cell: (row) => (
{formatGoalCoverageRatioLabel(row.goalCoverage)} {formatGoalCoverageDeficitShortLabel(row.goalCoverage)}
), cellClassName: `${responsiveColumnClassName.cobertura} text-right`, }, ] } /** B17 — the picker list; `region` is mandatory, the rest follow the visual order. */ export const territoryListPickerColumns = () => [ { id: 'region', label: territoryListSortLabels.region, mandatory: true }, { id: 'votes2022', label: territoryListSortLabels.votes2022 }, { id: 'captura', label: territoryListSortLabels.captura }, { id: 'estimate2026', label: territoryListSortLabels.estimate2026 }, { id: 'advisor', label: territoryNetworkColumnLabels.advisor }, { id: 'leadership', label: territoryNetworkColumnLabels.leadership }, { id: 'stateDeputy', label: territoryNetworkColumnLabels.stateDeputy }, { id: 'classe', label: territoryListSortLabels.classe }, { id: 'coverage', label: territoryListSortLabels.coverage }, { id: 'cobertura', label: territoryListSortLabels.cobertura }, ]