import { useCallback, useState } from 'react'; import { formatScaledAmount } from '../../utils/money'; export type Unit = 'руб.' | 'тыс. руб.' | 'млн руб.' | 'млрд руб.'; /** Единицы показа сумм сводной ОСВ и степень десяти (суммы API — в рублях). */ export const UNIT_SCALE: Record = { 'руб.': 0, 'тыс. руб.': 3, 'млн руб.': 6, 'млрд руб.': 9, }; export const UNITS: { value: Unit; label: string }[] = (Object.keys(UNIT_SCALE) as Unit[]).map((u) => ({ value: u, label: u })); export const PRECISIONS = [0, 1, 2, 3] as const; /** «Единицы» и «Знаков после запятой» сводной ОСВ [30]: только показ, суммы считаются на строках (`utils/money.ts`). */ export function useNumberDisplay(initial?: { unit?: Unit; precision?: number }) { const [unit, setUnit] = useState(initial?.unit ?? 'руб.'); const [precision, setPrecision] = useState(initial?.precision ?? 2); const format = useCallback( (raw: string | null) => formatScaledAmount(raw, { scale: UNIT_SCALE[unit], precision, zeroDash: true }), [unit, precision], ); return { unit, setUnit, precision, setPrecision, format }; }