export const DIMCODE_ENV_PREFIX = "DIMCODE_";
function parseBool(value) {
    const s = value.toLowerCase();
    if (!s)
        return undefined;
    if (s === "1" || s === "true" || s === "yes" || s === "on")
        return true;
    if (s === "0" || s === "false" || s === "no" || s === "off")
        return false;
    return undefined;
}
function parseIntValue(value) {
    if (!value)
        return undefined;
    const n = Number.parseInt(value, 10);
    return Number.isFinite(n) ? n : undefined;
}
function parseFloatValue(value) {
    if (!value)
        return undefined;
    const n = Number.parseFloat(value);
    return Number.isFinite(n) ? n : undefined;
}
function runtimeGlobals() {
    return globalThis;
}
export function readProcessEnv() {
    return runtimeGlobals().process?.env ?? {};
}
export function resolveEnv(primary, fallback) {
    return { ...fallback, ...primary };
}
export function prefixedEnvKeys(name) {
    return [`${DIMCODE_ENV_PREFIX}${name}`];
}
export function createEnvReader(env, fallback) {
    const merged = resolveEnv(env, fallback);
    const strAny = (keys) => {
        for (const key of keys) {
            const raw = String(merged?.[key] ?? "").trim();
            if (raw)
                return raw;
        }
        return "";
    };
    const str = (key, ...aliases) => {
        const raw = String(merged?.[key] ?? "").trim();
        if (raw)
            return raw;
        if (aliases.length)
            return strAny(aliases);
        return "";
    };
    const boolAny = (keys) => {
        const raw = strAny(keys);
        return parseBool(raw);
    };
    const bool = (key, ...aliases) => {
        const raw = str(key, ...aliases);
        return parseBool(raw);
    };
    const intAny = (keys) => {
        const raw = strAny(keys);
        return parseIntValue(raw);
    };
    const int = (key, ...aliases) => {
        const raw = str(key, ...aliases);
        return parseIntValue(raw);
    };
    const floatAny = (keys) => {
        const raw = strAny(keys);
        return parseFloatValue(raw);
    };
    const float = (key, ...aliases) => {
        const raw = str(key, ...aliases);
        return parseFloatValue(raw);
    };
    const prefixedStr = (name, ...aliases) => {
        return strAny([...prefixedEnvKeys(name), ...aliases]);
    };
    const prefixedBool = (name, ...aliases) => {
        return boolAny([...prefixedEnvKeys(name), ...aliases]);
    };
    const prefixedInt = (name, ...aliases) => {
        return intAny([...prefixedEnvKeys(name), ...aliases]);
    };
    const prefixedFloat = (name, ...aliases) => {
        return floatAny([...prefixedEnvKeys(name), ...aliases]);
    };
    return {
        env: merged,
        str,
        strAny,
        bool,
        boolAny,
        int,
        intAny,
        float,
        floatAny,
        prefixedStr,
        prefixedBool,
        prefixedInt,
        prefixedFloat,
    };
}
export function readPrefixedEnv(env, name) {
    const [primary] = prefixedEnvKeys(name);
    const source = env ?? readProcessEnv();
    return String(source?.[primary] ?? "").trim();
}
export function readPrefixedEnvFlag(env, name) {
    return readPrefixedEnv(env, name).toLowerCase();
}
export function setPrefixedEnvValue(env, name, value) {
    const [primary] = prefixedEnvKeys(name);
    const raw = String(value ?? "").trim();
    if (!raw) {
        delete env[primary];
        return;
    }
    env[primary] = raw;
}
export function ensurePrefixedEnvValue(env, name, fallback) {
    const [primary] = prefixedEnvKeys(name);
    const value = String(env[primary] ?? fallback ?? "").trim();
    if (!env[primary] && value)
        env[primary] = value;
    return value;
}
//# sourceMappingURL=env.js.map