/**
 * Runtime feature flag parsing.
 *
 * Env vars are the single source of truth for launch-stage feature gating.
 * Types/defaults live in ./feature-flags.ts so the web bundle can share them.
 *
 * Flags are parsed ONCE at module load and cached. To pick up env changes in tests,
 * call `refreshFeatureFlags()`.
 */
import { DEFAULT_FEATURE_FLAGS, FEATURE_ENV_VARS, } from './feature-flags.js';
export { DEFAULT_FEATURE_FLAGS, FEATURE_ENV_VARS };
const TRUE_VALUES = new Set(['1', 'true', 'yes', 'on', 'enabled']);
const FALSE_VALUES = new Set(['0', 'false', 'no', 'off', 'disabled']);
export function parseFeatureFlag(value, fallback, envVarName) {
    if (value === undefined)
        return fallback;
    const normalized = value.trim().toLowerCase();
    if (TRUE_VALUES.has(normalized))
        return true;
    if (FALSE_VALUES.has(normalized))
        return false;
    if (envVarName) {
        console.warn(`[features] ${envVarName}="${value}" is not a recognized boolean value; using default ${fallback}. ` +
            `Accepted: ${[...TRUE_VALUES].join('/')} or ${[...FALSE_VALUES].join('/')}.`);
    }
    return fallback;
}
function computeFeatureFlags(env) {
    const flags = {};
    for (const name of Object.keys(FEATURE_ENV_VARS)) {
        const envVarName = FEATURE_ENV_VARS[name];
        flags[name] = parseFeatureFlag(env[envVarName], DEFAULT_FEATURE_FLAGS[name], envVarName);
    }
    return flags;
}
let cachedFlags = computeFeatureFlags(process.env);
/** Re-read env vars. Intended for tests; production reads once at startup. */
export function refreshFeatureFlags(env = process.env) {
    cachedFlags = computeFeatureFlags(env);
    return cachedFlags;
}
export function getFeatureFlags() {
    return cachedFlags;
}
export function isFeatureEnabled(feature) {
    return cachedFlags[feature];
}
//# sourceMappingURL=features.js.map