import type { HistoryPoint } from '../../../shared/assets/history'; /** Accessible period buttons and chart clicks share one selection model. */ export function defaultHistoryPeriod(points: HistoryPoint[]): string | null { return points.find(p => p.state === 'LIVE')?.period || [...points].reverse().find(p => p.state === 'ARCHIVED' || p.state === 'RECONSTRUCTED')?.period || points.at(-1)?.period || null; } export function updateLivePoint(points: HistoryPoint[], live: HistoryPoint): HistoryPoint[] { return points.map((point, index) => { if (point.state !== 'LIVE' || point.period !== live.period) return point; if (points[index - 1]?.state === 'RECONSTRUCTED') return { ...live, difference: null, comparisonLabel: '推算与实时或正式快照口径不同,暂无可比数据' }; const baseline = points[index - 1]?.counts || (point.counts && point.difference ? { total: point.counts.total - point.difference.total, operating: point.counts.operating - point.difference.operating, inventory: point.counts.inventory - point.difference.inventory, } : null); return { ...live, difference: baseline && live.counts ? { total: live.counts.total - baseline.total, operating: live.counts.operating - baseline.operating, inventory: live.counts.inventory - baseline.inventory, } : null, comparisonLabel: baseline ? `截至当前较${live.period.length === 4 ? '上年末' : live.period.length === 10 ? '昨日' : '上月末'}` : '暂无可比数据' }; }); } export const signedCount = (value: number) => value > 0 ? `+${value.toLocaleString()}` : value.toLocaleString(); export function displayHistoryCounts(point: HistoryPoint) { return point.reconstruction ? { total: null, operating: point.reconstruction.operating, inventory: point.reconstruction.inventory } : point.counts; } export function displayHistoryDifference(point: HistoryPoint, key: 'total' | 'operating' | 'inventory'): number | null { return point.reconstruction ? key === 'total' ? null : point.reconstruction.difference?.[key] ?? null : point.difference?.[key] ?? null; }