"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const constantTimeCompare_1 = require("../util/constantTimeCompare");
const metrics_helper_1 = __importDefault(require("../util/metrics-helper"));
const metric_events_1 = require("../metric-events");
class EdgeService {
    constructor({ apiTokenService }, { getLogger, flagResolver, eventBus, }) {
        this.logger = getLogger('lib/services/edge-service.ts');
        this.apiTokenService = apiTokenService;
        this.flagResolver = flagResolver;
        this.timer = (functionName) => metrics_helper_1.default.wrapTimer(eventBus, metric_events_1.FUNCTION_TIME, {
            className: 'EdgeService',
            functionName,
        });
    }
    async getValidTokens(tokens) {
        if (this.flagResolver.isEnabled('checkEdgeValidTokensFromCache')) {
            const stopTimer = this.timer('validateTokensWithCache');
            // new behavior: use cached tokens when possible
            // use the db to fetch the missing ones
            // cache stores both missing and active so we don't hammer the db
            const validatedTokens = [];
            for (const token of tokens) {
                const found = await this.apiTokenService.getTokenWithCache(token);
                if (found) {
                    validatedTokens.push({
                        token: token,
                        type: found.type,
                        projects: found.projects,
                    });
                }
            }
            stopTimer();
            return { tokens: validatedTokens };
        }
        else {
            // old behavior: go to the db to fetch all tokens and then filter in memory
            const stopTimer = this.timer('validateTokensWithoutCache');
            const activeTokens = await this.apiTokenService.getAllActiveTokens();
            const edgeTokens = tokens.reduce((result, token) => {
                const dbToken = activeTokens.find((activeToken) => (0, constantTimeCompare_1.constantTimeCompare)(activeToken.secret, token));
                if (dbToken) {
                    result.push({
                        token: token,
                        type: dbToken.type,
                        projects: dbToken.projects,
                    });
                }
                return result;
            }, []);
            stopTimer();
            return { tokens: edgeTokens };
        }
    }
}
exports.default = EdgeService;
module.exports = EdgeService;
//# sourceMappingURL=edge-service.js.map