// 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 { CommandHandler } from "../../../../../core/decorator/CommandHandlerDecorator";
import { lazyInject } from "../../../../../core/decorator/LazyInjectDecorator";
import Injectable from "../../../../../core/injectable/Injectable";
import { MirrorNodeAdapter } from "../../../../../port/out/mirror/MirrorNodeAdapter";
import { RPCQueryAdapter } from "../../../../../port/out/rpc/RPCQueryAdapter";
import NetworkService from "../../../../service/network/NetworkService";
import { SetNetworkCommandError } from "./error/SetNetworkCommandError";
import { SetNetworkCommand, SetNetworkCommandResponse } from "./SetNetworkCommand";
let SetNetworkCommandHandler = class SetNetworkCommandHandler {
    networkService;
    mirrorNodeAdapter;
    constructor(networkService, mirrorNodeAdapter) {
        this.networkService = networkService;
        this.mirrorNodeAdapter = mirrorNodeAdapter;
    }
    async execute(command) {
        try {
            this.networkService.environment = command.environment;
            if (command.consensusNodes)
                this.networkService.consensusNodes = command.consensusNodes;
            if (command.rpcNode)
                this.networkService.rpcNode = command.rpcNode;
            // Init Mirror Node Adapter
            this.mirrorNodeAdapter.set(command.mirrorNode);
            this.networkService.mirrorNode = command.mirrorNode;
            // Init RPC Query Adapter
            Injectable.resolve(RPCQueryAdapter).init(this.networkService.rpcNode.baseUrl, this.networkService.rpcNode.apiKey);
            return Promise.resolve(new SetNetworkCommandResponse(this.networkService.environment, this.networkService.mirrorNode ?? "", this.networkService.rpcNode ?? "", this.networkService.consensusNodes ?? ""));
        }
        catch (error) {
            throw new SetNetworkCommandError(error);
        }
    }
};
SetNetworkCommandHandler = __decorate([
    CommandHandler(SetNetworkCommand),
    __param(0, lazyInject(NetworkService)),
    __param(1, lazyInject(MirrorNodeAdapter)),
    __metadata("design:paramtypes", [NetworkService,
        MirrorNodeAdapter])
], SetNetworkCommandHandler);
export { SetNetworkCommandHandler };
