'use client' import { useState } from 'react' import { MUNICIPALITY_PLEDGE_ESTIMATED_VOTES_ENDPOINT, type MunicipalityPledgeEstimatedVotesResponse, } from '@/app/(campaign)/campanha/(app)/municipios/pledge-estimated-votes/types' import { CampaignCellEditOverlay } from '@/components/campaign/shared/CampaignCellEditOverlay' import { useCampaignCellAutosave } from '@/components/campaign/shared/useCampaignCellAutosave' import { VoteEstimateScenarioInputs } from '@/components/campaign/votePledge/VoteEstimateScenarioInputs' import { Alert, AlertDescription } from '@/components/ui/Alert' import { Spinner } from '@/components/ui/Spinner' import { formatElectionNumber } from '@/lib/electionFormat' import { DEFAULT_VOTE_ESTIMATE_SCENARIO, effectivePledgeVotesForScenario, getVoteEstimateForScenario, voteEstimateScenarioLabels, voteEstimatesEqual, type VoteEstimateScenario, type VoteEstimateScenarioViewModel, } from '@/lib/voteEstimate' const AUTOSAVE_MS = 600 const SAVE_ERROR_MESSAGE = 'Não foi possível salvar a estimativa. Verifique seu acesso e tente novamente.' type MunicipalityV2EstimatedVotesCellProps = { pledgeID: number | null leadershipName: string declaredVotes: number | null estimatedVotes: VoteEstimateScenarioViewModel variant: 'popover' | 'sheet' /** C142 — read-only presentation (advisor with Edição `somente_leitura`): the estimate renders with no editor overlay. */ readOnly?: boolean } type EditableProps = Omit & { pledgeID: number } const MunicipalityV2EstimatedVotesCellEditable = ({ pledgeID, leadershipName, declaredVotes, estimatedVotes, variant, readOnly = false, }: EditableProps) => { const declared = declaredVotes ?? 0 const centralEstimate = getVoteEstimateForScenario(estimatedVotes, DEFAULT_VOTE_ESTIMATE_SCENARIO) const effectiveCentral = effectivePledgeVotesForScenario( declared, estimatedVotes, DEFAULT_VOTE_ESTIMATE_SCENARIO, ) const { open, onOpenChange, value, change, isPending, errorMessage, statusMessage } = useCampaignCellAutosave< VoteEstimateScenarioViewModel, MunicipalityPledgeEstimatedVotesResponse >({ value: estimatedVotes, equals: voteEstimatesEqual, endpoint: MUNICIPALITY_PLEDGE_ESTIMATED_VOTES_ENDPOINT, buildBody: (votes) => ({ pledgeId: pledgeID, estimatedVotes: votes }), readSaved: (payload) => payload.savedEstimatedVotes, errorMessage: SAVE_ERROR_MESSAGE, pendingMessage: 'Salvando estimativa.', }) const [focusedScenario, setFocusedScenario] = useState('central') const triggerLabel = [ 'Editar estimativa de ', leadershipName, ' — ', voteEstimateScenarioLabels[DEFAULT_VOTE_ESTIMATE_SCENARIO], ': ', centralEstimate == null ? formatElectionNumber(effectiveCentral) : formatElectionNumber(centralEstimate), ].join('') const display = centralEstimate == null ? ( {formatElectionNumber(effectiveCentral)} ) : ( {formatElectionNumber(centralEstimate)} ) // C142 — read-only: the estimate renders with no editor overlay. if (readOnly) { return display } return (
{isPending ? ( ) : null} change(values, AUTOSAVE_MS)} /> {errorMessage ? ( {errorMessage} ) : null}
) } export const MunicipalityV2EstimatedVotesCell = ({ pledgeID, leadershipName, declaredVotes, estimatedVotes, variant, readOnly = false, }: MunicipalityV2EstimatedVotesCellProps) => { if (pledgeID === null) { return — } return ( ) }