"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFeatureFlags = getFeatureFlags;
const FeatureFlagService_1 = require("../services/FeatureFlagService");
const flags_1 = require("./flags");
/**
 * Evaluate all feature flags for the selected user.
 * Server-side only — do not import from frontend code.
 */
async function getFeatureFlags(options = {}) {
    const service = (0, FeatureFlagService_1.getFeatureFlagService)();
    const keys = Object.keys(flags_1.FLAG_DEFINITIONS);
    // Single network request for all flags
    const allFlags = await service.getAllFlags(options.userKey);
    const entries = await Promise.all(keys.map(async (key) => {
        // Env-var override takes precedence. Convert camelCase + kebab-case
        // flag keys to UPPER_SNAKE_CASE so `designSystemEditor` resolves to
        // RIVET_FLAG_DESIGN_SYSTEM_EDITOR, as documented in flags.ts.
        const snakeKey = key
            .replace(/([a-z0-9])([A-Z])/g, '$1_$2')
            .replace(/-/g, '_')
            .toUpperCase();
        const envKey = `RIVET_FLAG_${snakeKey}`;
        const envVal = process.env[envKey];
        if (envVal !== undefined) {
            return [key, { isEnabled: envVal !== 'false' }];
        }
        // PostHog bulk value first.
        if (key in allFlags) {
            return [key, { isEnabled: !!allFlags[key] }];
        }
        // Fallback to per-flag evaluation for missing keys (e.g. inactive flags).
        const evaluated = await service.isFeatureEnabled(key, options.userKey);
        if (evaluated !== null) {
            return [key, { isEnabled: evaluated }];
        }
        // Last-resort local default.
        return [key, { isEnabled: flags_1.FLAG_DEFINITIONS[key].default }];
    }));
    return Object.fromEntries(entries);
}
//# sourceMappingURL=evaluateFlags.js.map