"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPrometheusAuthHeaders = getPrometheusAuthHeaders;
exports.getYTApiClusterSetup = getYTApiClusterSetup;
exports.getRobotYTApiSetup = getRobotYTApiSetup;
exports.getUserYTApiSetup = getUserYTApiSetup;
exports.getUserTabletErrorApiSetup = getUserTabletErrorApiSetup;
const utils_1 = require("../components/utils");
const ServerFactory_1 = require("../ServerFactory");
function getRobotSecret(cluster, type, defaultValue) {
    const { ytInterfaceSecret } = (0, ServerFactory_1.getApp)().config;
    let res;
    if (ytInterfaceSecret) {
        // eslint-disable-next-line security/detect-non-literal-require
        const content = require(ytInterfaceSecret);
        res = content?.[cluster]?.[type] || content?.[type];
    }
    return res ?? defaultValue;
}
function getRobotOAuthToken(cluster) {
    return getRobotSecret(cluster, 'oauthToken', process.env.YT_TOKEN ?? '');
}
function getPrometheusAuthHeaders(cluster) {
    return getRobotSecret(cluster, 'prometheusAuthHeaders', {});
}
function getClusterSetup(clusterConfig) {
    const { proxy } = clusterConfig;
    const secure = Boolean(clusterConfig?.secure);
    return {
        setup: {
            id: clusterConfig?.id,
            proxy,
            authentication: clusterConfig?.authentication || 'none',
            secure,
            useEncodedParameters: true,
            useHeavyProxy: false,
            timeout: 5000,
            disableHeavyProxies: clusterConfig?.disableHeavyProxies || false,
        },
        proxyBaseUrl: secure ? `https://${proxy}` : `http://${proxy}`,
    };
}
function getYTApiClusterSetup(cluster) {
    const { clusterConfig, ytConfig: { isLocalCluster }, } = (0, utils_1.getClusterConfig)(cluster);
    if (!clusterConfig) {
        throw new Error(`Cluster '${cluster}' is not found`);
    }
    return { isLocalCluster, ...getClusterSetup(clusterConfig) };
}
function getRobotYTApiSetup(cluster) {
    const oauthToken = getRobotOAuthToken(cluster);
    const config = getYTApiClusterSetup(cluster);
    const { setup, ...rest } = config;
    const authHeaders = setup.authentication && setup.authentication !== 'none'
        ? {
            Authorization: `OAuth ${oauthToken}`,
        }
        : {};
    return {
        setup: {
            ...setup,
            authentication: setup.authentication && setup.authentication !== 'none'
                ? {
                    type: 'oauth',
                    token: oauthToken,
                }
                : 'none',
        },
        authHeaders,
        ...rest,
    };
}
/**
 * @param cluster
 * @param req
 * @returns undefined when cluster is not found
 */
function getUserYTApiSetup(cluster, req) {
    const { setup, ...rest } = getYTApiClusterSetup(cluster);
    const { authentication } = setup;
    const authHeaders = authentication && authentication !== 'none' ? req.yt.ytApiAuthHeaders || {} : {};
    Object.assign(setup, { requestHeaders: { ...authHeaders } });
    return {
        ...rest,
        setup,
        authHeaders,
    };
}
function getUserTabletErrorApiSetup(cluster, req, testingHeaders) {
    const { authentication } = getYTApiClusterSetup(cluster).setup;
    const tabletsHeaders = testingHeaders
        ? req.yt.tabletErrorTestingApiAuthHeaders
        : req.yt.tabletErrorApiAuthHeaders;
    const authHeaders = authentication && authentication !== 'none'
        ? tabletsHeaders || req.yt.ytApiAuthHeaders || {}
        : {};
    return { authHeaders };
}
