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;
        return (value.amount ??
            undefined);
    }
    return value;
};
export const validateAmount = ({ type, required, minValue, maxValue, allowNegative, units, }) => (value) => {
    const amount = getAmount(value, type);
    if (value === "")
        return true;
    if (amount?.toString() === "") {
        if (required) {
            return "This field is required";
        }
        return true;
    }
    if (amount === undefined) {
        if (required) {
            return "This field is required";
        }
        return true;
    }
    if (!isValidNumber(amount) && type !== "AmountCurrency") {
        return "Value is not a valid number";
    }
    if (!allowNegative && amount < 0) {
        return "Value must be positive";
    }
    if (type === "AmountCrypto") {
        if (!isValidBigInt(amount.toString())) {
            return "Value is not an bigint";
        }
        return true;
    }
    if (type === "AmountCurrency") {
        if (!isValidNumber(amount)) {
            return "Value is not a valid number";
        }
        if (Math.abs(Number(amount)) > Number.MAX_SAFE_INTEGER) {
            const amountStr = amount.toString();
            if (!/^\d+$/.test(amountStr)) {
                return "Value is not a valid bigint";
            }
            return true;
        }
    }
    if (maxValue) {
        if (amount > maxValue) {
            return `This field must be less than ${maxValue}`;
        }
    }
    if (minValue) {
        if (amount < minValue) {
            return `This field must be more than ${minValue}`;
        }
    }
    if (Math.abs(Number(amount)) > Number.MAX_SAFE_INTEGER &&
        (type === "AmountFiat" ||
            type === "AmountPercentage" ||
            type === "Amount")) {
        return "Value is too large for number";
    }
    return true;
};
//# sourceMappingURL=amount-field-validations.js.map