import { getLogger } from '@nangohq/utils';
import { RedisKVStore } from './kvstore/RedisStore.js';
import { getRedisUrl } from './utils.js';
const logger = getLogger('FeatureFlags');
class FeatureFlags {
    redis;
    constructor(redis) {
        try {
            this.redis = redis;
        }
        catch {
            logger.error('Feature flags not enabled');
        }
    }
    async isEnabled(key, distinctId, fallback, isExcludingFlag = false) {
        if (!this.redis) {
            return fallback;
        }
        return this.redis.exists(`flag:${key}:${distinctId}`).then((r) => {
            return isExcludingFlag ? !r : r;
        }, (_) => {
            return fallback;
        });
    }
}
const redis = await (async () => {
    let redis;
    const url = getRedisUrl();
    if (url) {
        redis = new RedisKVStore(url);
        await redis.connect();
    }
    return redis;
})();
export default new FeatureFlags(redis);
//# sourceMappingURL=featureflags.js.map