import { isValidBigInt } from '../../../ui/components/data-entry/amount-input/utils.js';
import { isValidNumber } from '../number-field/number-field-validations.js';
const isAmountCurrencyFiat = (type) => type === 'AmountFiat';
const isAmountCurrencyCrypto = (type) => type === 'AmountCrypto';
const isAmountCurrencyUniversal = (type) => type === 'AmountCurrency';
const isAmount = (type) => type === 'Amount';
const getAmount = (value, type) => {
    if (isAmountCurrencyFiat(type) || isAmountCurrencyCrypto(type) || isAmountCurrencyUniversal(type) || isAmount(type)) {
        if (!value)
            return undefined;
        const amount = value.value ?? undefined;
        return amount;
    }
    return value;
};
export const validateAmount = ({ type, minValue, maxValue, allowNegative, required }) => (value) => {
    const amount = getAmount(value, type);
    if (amount === '' || amount === undefined) {
        return required ? 'Please enter a valid number' : true;
    }
    // This case are types that are strings but hndle as bigint
    if (type === 'AmountCurrency' || type === 'AmountCrypto') {
        if (!isValidBigInt(amount.toString())) {
            return 'Value is not an bigint';
        }
        if (!allowNegative && BigInt(amount) < 0) {
            return 'Value must be positive';
        }
        if (maxValue) {
            if (BigInt(amount) > BigInt(maxValue)) {
                return `This field must be less than ${maxValue}`;
            }
        }
        if (minValue) {
            if (BigInt(amount) < BigInt(minValue)) {
                return `This field must be more than ${minValue}`;
            }
        }
        return true;
    }
    if (Math.abs(Number(amount.toString())) > Number.MAX_SAFE_INTEGER) {
        return 'Value is too large for number';
    }
    if (!isValidNumber(amount)) {
        return 'Value is not a valid number';
    }
    if (!allowNegative && Number(amount) < 0) {
        return 'Value must be positive';
    }
    if (maxValue) {
        if (Number(amount) > maxValue) {
            return `This field must be less than ${maxValue}`;
        }
    }
    if (minValue) {
        if (Number(amount) < minValue) {
            return `This field must be more than ${minValue}`;
        }
    }
    return true;
};
//# sourceMappingURL=amount-field-validations.js.map