"use strict";
/*
 * @adonisjs/env
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.number = exports.castToNumber = void 0;
const utils_1 = require("@poppinss/utils");
const helpers_1 = require("./helpers");
/**
 * Casts the string to a number and ensures it is no NaN
 */
function castToNumber(key, value, message) {
    const castedValue = Number(value);
    if (isNaN(castedValue)) {
        throw new utils_1.Exception(message ||
            `Value for environment variable "${key}" must be numeric, instead received "${value}"`, 500, 'E_INVALID_ENV_VALUE');
    }
    return castedValue;
}
exports.castToNumber = castToNumber;
/**
 * Enforces the value to be of valid number type and the
 * value will also be casted to a number
 */
function number(options) {
    return function validate(key, value) {
        (0, helpers_1.ensureValue)(key, value, options?.message);
        return castToNumber(key, value, options?.message);
    };
}
exports.number = number;
/**
 * Similar to the number rule, but also allows optional
 * values
 */
number.optional = function optionalNumber(options) {
    return function validate(key, value) {
        if (!value) {
            return undefined;
        }
        return castToNumber(key, value, options?.message);
    };
};
