import { type ClassType, isDefined, isNumber } from '@rnw-community/shared'; import { isValidDecimalMonetaryValue } from './is-valid-decimal-monetary-value/is-valid-decimal-monetary-value.util'; import type { PaymentItem } from '../@standard/w3c/payment-item'; import type { AmountValue } from '../type/amount-value.type'; const isNegative = (amountValue: AmountValue): boolean => isNumber(amountValue) ? amountValue < 0 : amountValue.startsWith('-'); export const validateTotal = (total: PaymentItem, ErrorType: ClassType): void => { if (!isDefined(total)) { throw new ErrorType(`required member total is undefined.`); } if (!isDefined(total.amount) || !isDefined(total.amount.value)) { throw new ErrorType(`Missing required member(s): amount, label.`); } const totalAmountValue = total.amount.value; if (!isValidDecimalMonetaryValue(totalAmountValue)) { throw new ErrorType(`'${totalAmountValue}' is not a valid amount format for total`); } if (isNegative(totalAmountValue)) { throw new ErrorType(`Total amount value should be non-negative`); } };