// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { Templates } from './templates.js';
import * as constants from './constants.js';
import { lt } from 'semver';
import * as versions from '../../version.js';
import { inject } from 'tsyringe-neo';
import { InjectTokens } from './dependency-injection/inject-tokens.js';
import { patchInject } from './dependency-injection/container-helper.js';
/**
 * Wrapper used to generate `block-nodes.json` file
 * for the consensus node used to configure block node connections.
 */
let BlockNodesJsonWrapper = class BlockNodesJsonWrapper {
    blockNodeMap;
    externalBlockNodeMap;
    remoteConfig;
    blockNodes;
    externalBlockNodes;
    constructor(blockNodeMap, externalBlockNodeMap, remoteConfig) {
        this.blockNodeMap = blockNodeMap;
        this.externalBlockNodeMap = externalBlockNodeMap;
        this.remoteConfig = patchInject(remoteConfig, InjectTokens.RemoteConfigRuntimeState, this.constructor.name);
        this.blockNodes = this.remoteConfig.configuration.state.blockNodes;
        this.externalBlockNodes = this.remoteConfig.configuration.state.externalBlockNodes;
    }
    toJSON() {
        return JSON.stringify(this.buildBlockNodesJsonStructure());
    }
    buildBlockNodesJsonStructure() {
        // Figure out field name for port
        const useLegacyPortName = lt(this.remoteConfig.configuration.versions.consensusNode, versions.MINIMUM_HIERO_CONSENSUS_NODE_VERSION_FOR_LEGACY_PORT_NAME_FOR_BLOCK_NODES_JSON_FILE);
        const blockNodeConnectionData = [];
        for (const [id, priority] of this.blockNodeMap) {
            const blockNodeComponent = this.blockNodes.find((component) => component.metadata.id === id);
            const cluster = this.remoteConfig.configuration.clusters.find((cluster) => cluster.name === blockNodeComponent.metadata.cluster);
            const address = Templates.renderSvcFullyQualifiedDomainName(Templates.renderBlockNodeName(blockNodeComponent.metadata.id), blockNodeComponent.metadata.namespace, cluster.dnsBaseDomain);
            // Figure out the block node port
            const useLegacyPort = lt(this.remoteConfig.configuration.versions.blockNodeChart, versions.MINIMUM_HIERO_BLOCK_NODE_VERSION_FOR_NEW_LIVENESS_CHECK_PORT);
            const port = useLegacyPort ? constants.BLOCK_NODE_PORT_LEGACY : constants.BLOCK_NODE_PORT;
            blockNodeConnectionData.push(useLegacyPortName ? { address, port, priority } : { address, streamingPort: port, servicePort: port, priority });
        }
        for (const [id, priority] of this.externalBlockNodeMap) {
            const blockNodeComponent = this.externalBlockNodes.find((component) => component.id === id);
            const address = blockNodeComponent.address;
            const port = blockNodeComponent.port;
            blockNodeConnectionData.push(useLegacyPortName ? { address, port, priority } : { address, streamingPort: port, servicePort: port, priority });
        }
        return {
            nodes: blockNodeConnectionData,
            blockItemBatchSize: constants.BLOCK_ITEM_BATCH_SIZE,
        };
    }
};
BlockNodesJsonWrapper = __decorate([
    __param(2, inject(InjectTokens.RemoteConfigRuntimeState)),
    __metadata("design:paramtypes", [Array, Array, Object])
], BlockNodesJsonWrapper);
export { BlockNodesJsonWrapper };
//# sourceMappingURL=block-nodes-json-wrapper.js.map