'use strict';

var index = require('../../node_modules/@flipt-io/flipt-client-js/dist/node/index.js');
var node = require('@sentry/node');
var featureFlagsHelper = require('../utils/featureFlagsHelper.js');

class FeatureFlagService {
    clientConfig;
    clientPromise;
    constructor(config = {}) {
        const { token, url } = config && config;
        if (!token || !url) {
            return;
        }
        const updateInterval = (typeof config.updateInterval === 'number' && config.updateInterval) || 120;
        this.clientConfig = {
            url,
            namespace: featureFlagsHelper.FEATURE_FLAG_NAMESPACE,
            authentication: {
                clientToken: token,
            },
            updateInterval,
        };
    }
    async getClient() {
        if (!this.clientConfig) {
            return undefined;
        }
        if (!this.clientPromise) {
            this.clientPromise = index.FliptClient.init(this.clientConfig).catch((error) => {
                this.clientPromise = undefined;
                throw error;
            });
        }
        return this.clientPromise;
    }
    async refresh() {
        const client = await this.getClient();
        await client?.refresh();
    }
    async evaluateBooleanFlag(flagKey, subject) {
        const evaluation = await this.evaluateBooleanFlags([flagKey], subject);
        return evaluation[flagKey];
    }
    async evaluateBooleanFlags(flagKeys, subject) {
        const results = Object.fromEntries(flagKeys.map((flagKey) => [flagKey, featureFlagsHelper.getFeatureFlagFallbackState(flagKey)]));
        if (flagKeys.length === 0) {
            return results;
        }
        const client = await this.getClient().catch((error) => {
            node.captureException(error);
            return undefined;
        });
        if (!client) {
            return results;
        }
        const requests = flagKeys.map((flagKey) => ({
            flagKey,
            entityId: subject.entityId,
            context: subject.context,
        }));
        try {
            const batchResponse = client.evaluateBatch(requests);
            batchResponse.responses
                .filter((response) => response.type === 'BOOLEAN_EVALUATION_RESPONSE_TYPE')
                .forEach((response) => {
                const flagKey = response.booleanEvaluationResponse?.flagKey;
                const enabled = response.booleanEvaluationResponse?.enabled;
                if (flagKey && enabled !== undefined && flagKey in results) {
                    results[flagKey] = enabled;
                }
            });
        }
        catch (error) {
            node.captureException(error);
            return results;
        }
        return results;
    }
}
const resolveFlag = (app, flagName) => {
    return app.locals['featureFlags']?.flags?.[flagName];
};
const isBooleanFlagEnabledOrMissing = (flagName, app) => {
    const flag = resolveFlag(app, flagName);
    return flag !== false;
};

exports.FeatureFlagService = FeatureFlagService;
exports.isBooleanFlagEnabledOrMissing = isBooleanFlagEnabledOrMissing;
//# sourceMappingURL=featureFlagService.js.map
