"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getThisNodeName = getThisNodeName;
exports.clearThisNodeName = clearThisNodeName;
exports.hostnameToUrl = hostnameToUrl;
exports.urlToNodeName = urlToNodeName;
exports.getThisNodeUrl = getThisNodeUrl;
const node_fs_1 = require("node:fs");
const node_crypto_1 = require("node:crypto");
const hdbTerms_ts_1 = require("../utility/hdbTerms.js");
const environmentManager_js_1 = __importDefault(require("../utility/environment/environmentManager.js"));
const logger_ts_1 = require("../utility/logging/logger.js");
Object.defineProperty(server, 'hostname', {
    get() {
        return getThisNodeName();
    },
});
let commonNameFromCert;
function getCommonNameFromCert() {
    if (commonNameFromCert !== undefined)
        return commonNameFromCert;
    const certificatePath = environmentManager_js_1.default.get(hdbTerms_ts_1.CONFIG_PARAMS.OPERATIONSAPI_TLS_CERTIFICATE) || environmentManager_js_1.default.get(hdbTerms_ts_1.CONFIG_PARAMS.TLS_CERTIFICATE);
    if (certificatePath) {
        // we can use this to get the hostname if it isn't provided by config
        const certParsed = new node_crypto_1.X509Certificate((0, node_fs_1.readFileSync)(certificatePath));
        const subject = certParsed.subject;
        return (commonNameFromCert = subject?.match(/CN=(.*)/)?.[1] ?? null);
    }
}
let nodeName;
function getThisNodeName() {
    if (nodeName)
        return nodeName; // if already determined, just return
    nodeName = environmentManager_js_1.default.get(hdbTerms_ts_1.CONFIG_PARAMS.NODE_HOSTNAME); // standard config
    if (nodeName) {
        if (environmentManager_js_1.default.get('replication_hostname') && environmentManager_js_1.default.get('replication_hostname') !== nodeName) {
            // if these are both set, it can be very confusing, make sure to warn
            logger_ts_1.logger.warn?.(`The node.hostname and replication.hostname configuration values are both set and are different. It is recommended that you set the node.hostname, using node.hostname: ${nodeName})`);
        }
        return nodeName;
    }
    // fallback to other means of getting the node name
    nodeName =
        environmentManager_js_1.default.get('replication_hostname') ?? // for backwards compatibility
            urlToNodeName(environmentManager_js_1.default.get('replication_url')) ??
            getCommonNameFromCert() ??
            getHostFromListeningPort('operationsapi_network_secureport') ??
            getHostFromListeningPort('operationsapi_network_port') ??
            '127.0.0.1';
    return nodeName;
}
function clearThisNodeName() {
    nodeName = undefined;
}
function getHostFromListeningPort(key) {
    const port = environmentManager_js_1.default.get(key);
    const lastColon = port?.lastIndexOf?.(':');
    if (lastColon > 0)
        return port.slice(0, lastColon);
}
function getPortFromListeningPort(key) {
    const port = environmentManager_js_1.default.get(key);
    const lastColon = port?.lastIndexOf?.(':');
    if (lastColon > 0)
        return +port.slice(lastColon + 1).replace(/[[\]]/g, '');
    return +port;
}
function hostnameToUrl(hostname) {
    let port = getPortFromListeningPort('replication_port');
    if (port)
        return `ws://${hostname}:${port}`;
    port = getPortFromListeningPort('replication_secureport');
    if (port)
        return `wss://${hostname}:${port}`;
    port = getPortFromListeningPort('operationsapi_network_port');
    if (port)
        return `ws://${hostname}:${port}`;
    port = getPortFromListeningPort('operationsapi_network_secureport');
    if (port)
        return `wss://${hostname}:${port}`;
}
function urlToNodeName(nodeUrl) {
    if (nodeUrl)
        return new URL(nodeUrl).hostname; // this the part of the URL that is the node name, as we want it to match common name in the certificate
}
function getThisNodeUrl() {
    const url = environmentManager_js_1.default.get(hdbTerms_ts_1.CONFIG_PARAMS.REPLICATION_URL);
    if (url)
        return url;
    return hostnameToUrl(getThisNodeName());
}
//# sourceMappingURL=nodeName.js.map