import type { PaymentDto } from '@gruperly/shared' import type { BadgeVariant } from '../../components/ui' import { i18n } from '../../i18n/instance' export const PAYMENT_STATUS_BADGE: Record = { PENDING: 'neutral', PAID: 'success', OVERDUE: 'danger', CANCELLED: 'warning', } export function paymentPeriodLabel(payment: PaymentDto): string { if (payment.description) return payment.description if (payment.periodKey) return periodKeyLabel(payment.periodKey) return payment.groupName } /* "2026-12" → "Diciembre 2026", en el idioma de la UI. */ export function periodKeyLabel(periodKey: string): string { const [year, month] = periodKey.split('-').map(Number) const text = new Date(Date.UTC(year ?? 2000, (month ?? 1) - 1, 15)).toLocaleDateString(i18n.language, { month: 'long', year: 'numeric', timeZone: 'UTC', }) return text.charAt(0).toUpperCase() + text.slice(1) } export function paymentDateLabel(payment: PaymentDto): string { if (payment.status === 'PAID' && payment.paidAt) { return i18n.t('payments:row.paidOn', { date: formatDay(payment.paidAt) }) } return i18n.t('payments:row.dueOn', { date: formatDay(payment.dueDate) }) } /* Fechas sin hora: el vencimiento es un día, no un instante. */ function formatDay(iso: string): string { const date = new Date(iso) const today = new Date() const sameYear = date.getFullYear() === today.getFullYear() return date.toLocaleDateString(i18n.language, { day: 'numeric', month: 'short', ...(sameYear ? {} : { year: 'numeric' }), }) }