"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applySecurityAuthentication = exports.setServerVariables = exports.getServer = exports.createConfiguration = exports.Configuration = void 0;
const isomorphic_fetch_1 = require("./http/isomorphic-fetch");
const servers_1 = require("./servers");
const auth_1 = require("./auth");
const logger_1 = require("../../logger");
class Configuration {
    constructor(baseServer, serverIndex, operationServerIndices, httpApi, authMethods, httpConfig, debug, enableRetry, maxRetries, backoffBase, backoffMultiplier, unstableOperations) {
        this.baseServer = baseServer;
        this.serverIndex = serverIndex;
        this.operationServerIndices = operationServerIndices;
        this.httpApi = httpApi;
        this.authMethods = authMethods;
        this.httpConfig = httpConfig;
        this.debug = debug;
        this.enableRetry = enableRetry;
        this.maxRetries = maxRetries;
        this.backoffBase = backoffBase;
        this.backoffMultiplier = backoffMultiplier;
        this.unstableOperations = unstableOperations;
        this.servers = [];
        for (const server of servers_1.servers) {
            this.servers.push(server.clone());
        }
        this.operationServers = {};
        for (const endpoint in servers_1.operationServers) {
            this.operationServers[endpoint] = [];
            for (const server of servers_1.operationServers[endpoint]) {
                this.operationServers[endpoint].push(server.clone());
            }
        }
        if (backoffBase && backoffBase < 2) {
            throw new Error("Backoff base must be at least 2");
        }
    }
    setServerVariables(serverVariables) {
        if (this.baseServer !== undefined) {
            this.baseServer.setVariables(serverVariables);
            return;
        }
        const index = this.serverIndex;
        this.servers[index].setVariables(serverVariables);
        for (const op in this.operationServers) {
            this.operationServers[op][0].setVariables(serverVariables);
        }
    }
    getServer(endpoint) {
        if (this.baseServer !== undefined) {
            return this.baseServer;
        }
        const index = endpoint in this.operationServerIndices
            ? this.operationServerIndices[endpoint]
            : this.serverIndex;
        return endpoint in servers_1.operationServers
            ? this.operationServers[endpoint][index]
            : this.servers[index];
    }
}
exports.Configuration = Configuration;
/**
 * Configuration factory function
 *
 * If a property is not included in conf, a default is used:
 *    - baseServer: null
 *    - serverIndex: 0
 *    - operationServerIndices: {}
 *    - httpApi: IsomorphicFetchHttpLibrary
 *    - authMethods: {}
 *    - httpConfig: {}
 *    - debug: false
 *
 * @param conf partial configuration
 */
function createConfiguration(conf = {}) {
    if (typeof process !== "undefined" && process.env && process.env.DD_SITE) {
        const serverConf = servers_1.server1.getConfiguration();
        servers_1.server1.setVariables({ site: process.env.DD_SITE });
        for (const op in servers_1.operationServers) {
            servers_1.operationServers[op][0].setVariables({ site: process.env.DD_SITE });
        }
    }
    const authMethods = conf.authMethods || {};
    if (!("apiKeyAuth" in authMethods) &&
        typeof process !== "undefined" &&
        process.env &&
        process.env.DD_API_KEY) {
        authMethods["apiKeyAuth"] = process.env.DD_API_KEY;
    }
    if (!("appKeyAuth" in authMethods) &&
        typeof process !== "undefined" &&
        process.env &&
        process.env.DD_APP_KEY) {
        authMethods["appKeyAuth"] = process.env.DD_APP_KEY;
    }
    const configuration = new Configuration(conf.baseServer, conf.serverIndex || 0, conf.operationServerIndices || {}, conf.httpApi || new isomorphic_fetch_1.IsomorphicFetchHttpLibrary({ fetch: conf.fetch }), (0, auth_1.configureAuthMethods)(authMethods), conf.httpConfig || {}, conf.debug, conf.enableRetry || false, conf.maxRetries || 3, conf.backoffBase || 2, conf.backoffMultiplier || 2, {
        "v2.createOpenAPI": false,
        "v2.deleteOpenAPI": false,
        "v2.getOpenAPI": false,
        "v2.listAPIs": false,
        "v2.updateOpenAPI": false,
        "v2.getActiveBillingDimensions": false,
        "v2.getMonthlyCostAttribution": false,
        "v2.createDORADeployment": false,
        "v2.createDORAIncident": false,
        "v2.createIncident": false,
        "v2.createIncidentIntegration": false,
        "v2.createIncidentTodo": false,
        "v2.deleteIncident": false,
        "v2.deleteIncidentIntegration": false,
        "v2.deleteIncidentTodo": false,
        "v2.getIncident": false,
        "v2.getIncidentIntegration": false,
        "v2.getIncidentTodo": false,
        "v2.listIncidentAttachments": false,
        "v2.listIncidentIntegrations": false,
        "v2.listIncidents": false,
        "v2.listIncidentTodos": false,
        "v2.searchIncidents": false,
        "v2.updateIncident": false,
        "v2.updateIncidentAttachments": false,
        "v2.updateIncidentIntegration": false,
        "v2.updateIncidentTodo": false,
        "v2.queryScalarData": false,
        "v2.queryTimeseriesData": false,
        "v2.getFinding": false,
        "v2.listFindings": false,
        "v2.muteFindings": false,
        "v2.createScorecardOutcomesBatch": false,
        "v2.createScorecardRule": false,
        "v2.deleteScorecardRule": false,
        "v2.listScorecardOutcomes": false,
        "v2.listScorecardRules": false,
        "v2.updateScorecardRule": false,
        "v2.createIncidentService": false,
        "v2.deleteIncidentService": false,
        "v2.getIncidentService": false,
        "v2.listIncidentServices": false,
        "v2.updateIncidentService": false,
        "v2.createSLOReportJob": false,
        "v2.getSLOReport": false,
        "v2.getSLOReportJobStatus": false,
        "v2.createIncidentTeam": false,
        "v2.deleteIncidentTeam": false,
        "v2.getIncidentTeam": false,
        "v2.listIncidentTeams": false,
        "v2.updateIncidentTeam": false,
    });
    configuration.httpApi.zstdCompressorCallback = conf.zstdCompressorCallback;
    configuration.httpApi.debug = configuration.debug;
    configuration.httpApi.enableRetry = configuration.enableRetry;
    configuration.httpApi.maxRetries = configuration.maxRetries;
    configuration.httpApi.backoffBase = configuration.backoffBase;
    configuration.httpApi.backoffMultiplier = configuration.backoffMultiplier;
    return configuration;
}
exports.createConfiguration = createConfiguration;
function getServer(conf, endpoint) {
    logger_1.logger.warn("getServer is deprecated, please use Configuration.getServer instead.");
    return conf.getServer(endpoint);
}
exports.getServer = getServer;
/**
 * Sets the server variables.
 *
 * @param serverVariables key/value object representing the server variables (site, name, protocol, ...)
 */
function setServerVariables(conf, serverVariables) {
    logger_1.logger.warn("setServerVariables is deprecated, please use Configuration.setServerVariables instead.");
    return conf.setServerVariables(serverVariables);
}
exports.setServerVariables = setServerVariables;
/**
 * Apply given security authentication method if avaiable in configuration.
 */
function applySecurityAuthentication(conf, requestContext, authMethods) {
    for (const authMethodName of authMethods) {
        const authMethod = conf.authMethods[authMethodName];
        if (authMethod) {
            authMethod.applySecurityAuthentication(requestContext);
        }
    }
}
exports.applySecurityAuthentication = applySecurityAuthentication;
//# sourceMappingURL=configuration.js.map