"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClustersFromConfig = getClustersFromConfig;
exports.getClusterConfig = getClusterConfig;
const constants_1 = require("../constants");
const config_localcluster_1 = require("../config.localcluster");
const config_realcluster_1 = require("../config.realcluster");
const ServerFactory_1 = __importStar(require("../ServerFactory"));
const utils_1 = require("../utils");
/*
  Gets config file, which can be either
  * the file containing all real clusters, or
  * the file containing config of a local cluster, with the proxy address either
    * defaulting to `process.env.PROXY` which corresponds to the case of a local interface paired with local cluster
    * provided explicitly which corresponds to a local cluster paired with the main interface running in cloud platform.
 */
function getClientConfig(proxy) {
    if ((proxy && (0, ServerFactory_1.getApp)().config.ytAllowRemoteLocalProxy) || (0, utils_1.isLocalModeByEnvironment)()) {
        return (0, config_localcluster_1.makeLocalModeConfig)(proxy);
    }
    else {
        return (0, config_realcluster_1.getRealClustersConfig)();
    }
}
function getClustersFromConfig() {
    return getClientConfig().clusters;
}
/*
  Gets particular cluster config for `cluster` name, which is either
  * a real cluster name
  * a local cluster pseudo-name
  * an address of a yt-local proxy running in some sandbox
 */
function getClusterConfig(cluster) {
    const ytConfig = getClientConfig();
    if (!cluster) {
        return { ytConfig };
    }
    const { clusters } = ytConfig;
    if (Object.hasOwnProperty.call(clusters, cluster)) {
        return {
            ytConfig,
            clusterConfig: applyInternalProxy(clusters[cluster]),
        };
    }
    else if (ServerFactory_1.default.isLocalClusterId(cluster)) {
        const config = getClientConfig(cluster);
        return {
            ytConfig: config,
            clusterConfig: applyInternalProxy(config.clusters[cluster]),
        };
    }
    else {
        return { ytConfig };
    }
}
/**
 * PROXY_INTERNAL environment variable should be applied only for 'ui' cluster.
 * Do not use `isLocalClusterId(...)` in the function!
 */
function applyInternalProxy(clusterConfig) {
    const internalProxyName = process.env.PROXY_INTERNAL;
    if (!internalProxyName || clusterConfig.id !== constants_1.YT_LOCAL_CLUSTER_ID) {
        return clusterConfig;
    }
    return {
        ...clusterConfig,
        proxy: internalProxyName,
    };
}
