import { UNEQUAL_CURRENCIES_MESSAGE } from '../checks' ; import { assert } from '../helpers' ; import type { Calculator , Dinero } from '../types' ; import { minimum as min } from '../utils' ; import { haveSameCurrency } from './haveSameCurrency' ; import { normalizeScale } from './normalizeScale' ; export type MinimumParams < TAmount > = readonly [ dineroObjects : ReadonlyArray < Dinero < TAmount > > ] ; function unsafeMinimum < TAmount > ( calculator ) { const minFn = min ( calculator ) ; return function minimum ( ... [ dineroObjects ] ) { const [ firstDinero ] = dineroObjects ; const { currency , scale } = firstDinero . toJSON ( ) ; const amount = minFn ( dineroObjects . map ( ( subject ) => { const { amount : subjectAmount } = subject . toJSON ( ) ; return subjectAmount ; } ) ) ; return firstDinero . create ( { amount , currency , scale , } ) ; } ; } export function safeMinimum < TAmount > ( calculator ) { const normalizeFn = normalizeScale ( calculator ) ; const minFn = unsafeMinimum ( calculator ) ; return function maximum ( ... [ dineroObjects ] ) { const condition = haveSameCurrency ( dineroObjects ) ; assert ( condition , UNEQUAL_CURRENCIES_MESSAGE ) ; const normalizedDineroObjects = normalizeFn ( dineroObjects ) ; return minFn ( normalizedDineroObjects ) ; } ; }