// 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); }
};
var NodeCommand_1;
import { YargsCommand } from '../../core/yargs-command.js';
import { BaseCommand } from './../base.js';
import * as NodeFlags from './flags.js';
import { patchInject } from '../../core/dependency-injection/container-helper.js';
import { InjectTokens } from '../../core/dependency-injection/inject-tokens.js';
import { inject, injectable } from 'tsyringe-neo';
/**
 * Defines the core functionalities of 'node' command
 */
let NodeCommand = class NodeCommand extends BaseCommand {
    static { NodeCommand_1 = this; }
    accountManager;
    handlers;
    _portForwards;
    constructor(accountManager, handlers) {
        super();
        this.accountManager = accountManager;
        this.handlers = handlers;
        this.accountManager = patchInject(accountManager, InjectTokens.AccountManager, this.constructor.name);
        this.handlers = patchInject(handlers, InjectTokens.NodeCommandHandlers, this.constructor.name);
        this._portForwards = [];
    }
    static COMMAND_NAME = 'node';
    /**
     * stops and closes the port forwards
     * - calls the accountManager.close()
     * - for all portForwards, calls k8Factory.default().pods().readByReference(null).stopPortForward(srv)
     */
    async close() {
        await this.accountManager.close();
        if (this._portForwards) {
            for (const srv of this._portForwards) {
                // pass null to readByReference because it isn't needed for stopPortForward()
                await this.k8Factory.default().pods().readByReference(null).stopPortForward(srv);
            }
        }
        this._portForwards = [];
    }
    getUnusedConfigs(configName) {
        return this.handlers.getUnusedConfigs(configName);
    }
    getCommandDefinition() {
        const self = this;
        return {
            command: NodeCommand_1.COMMAND_NAME,
            desc: 'Manage Hedera platform node in solo network',
            builder: (yargs) => {
                return yargs
                    .command(new YargsCommand({
                    command: 'setup',
                    description: 'Setup node with a specific version of Hedera platform',
                    commandDef: self,
                    handler: 'setup',
                }, NodeFlags.SETUP_FLAGS))
                    .command(new YargsCommand({
                    command: 'start',
                    description: 'Start a node',
                    commandDef: self,
                    handler: 'start',
                }, NodeFlags.START_FLAGS))
                    .command(new YargsCommand({
                    command: 'stop',
                    description: 'Stop a node',
                    commandDef: self,
                    handler: 'stop',
                }, NodeFlags.STOP_FLAGS))
                    .command(new YargsCommand({
                    command: 'freeze',
                    description: 'Freeze all nodes of the network',
                    commandDef: self,
                    handler: 'freeze',
                }, NodeFlags.FREEZE_FLAGS))
                    .command(new YargsCommand({
                    command: 'restart',
                    description: 'Restart all nodes of the network',
                    commandDef: self,
                    handler: 'restart',
                }, NodeFlags.RESTART_FLAGS))
                    .command(new YargsCommand({
                    command: 'keys',
                    description: 'Generate node keys',
                    commandDef: self,
                    handler: 'keys',
                }, NodeFlags.KEYS_FLAGS))
                    .command(new YargsCommand({
                    command: 'refresh',
                    description: 'Reset and restart a node',
                    commandDef: self,
                    handler: 'refresh',
                }, NodeFlags.REFRESH_FLAGS))
                    .command(new YargsCommand({
                    command: 'logs',
                    description: 'Download application logs from the network nodes and stores them in <SOLO_LOGS_DIR>/<namespace>/<podName>/ directory',
                    commandDef: self,
                    handler: 'logs',
                }, NodeFlags.LOGS_FLAGS))
                    .command(new YargsCommand({
                    command: 'states',
                    description: 'Download hedera states from the network nodes and stores them in <SOLO_LOGS_DIR>/<namespace>/<podName>/ directory',
                    commandDef: self,
                    handler: 'states',
                }, NodeFlags.STATES_FLAGS))
                    .command(new YargsCommand({
                    command: 'add',
                    description: 'Adds a node with a specific version of Hedera platform',
                    commandDef: self,
                    handler: 'add',
                }, NodeFlags.ADD_FLAGS))
                    .command(new YargsCommand({
                    command: 'add-prepare',
                    description: 'Prepares the addition of a node with a specific version of Hedera platform',
                    commandDef: self,
                    handler: 'addPrepare',
                }, NodeFlags.ADD_PREPARE_FLAGS))
                    .command(new YargsCommand({
                    command: 'add-submit-transactions',
                    description: 'Submits NodeCreateTransaction and Upgrade transactions to the network nodes',
                    commandDef: self,
                    handler: 'addSubmitTransactions',
                }, NodeFlags.ADD_SUBMIT_TRANSACTIONS_FLAGS))
                    .command(new YargsCommand({
                    command: 'add-execute',
                    description: 'Executes the addition of a previously prepared node',
                    commandDef: self,
                    handler: 'addExecute',
                }, NodeFlags.ADD_EXECUTE_FLAGS))
                    .command(new YargsCommand({
                    command: 'update',
                    description: 'Update a node with a specific version of Hedera platform',
                    commandDef: self,
                    handler: 'update',
                }, NodeFlags.UPDATE_FLAGS))
                    .command(new YargsCommand({
                    command: 'update-prepare',
                    description: 'Prepare the deployment to update a node with a specific version of Hedera platform',
                    commandDef: self,
                    handler: 'updatePrepare',
                }, NodeFlags.UPDATE_PREPARE_FLAGS))
                    .command(new YargsCommand({
                    command: 'update-submit-transactions',
                    description: 'Submit transactions for updating a node with a specific version of Hedera platform',
                    commandDef: self,
                    handler: 'updateSubmitTransactions',
                }, NodeFlags.UPDATE_SUBMIT_TRANSACTIONS_FLAGS))
                    .command(new YargsCommand({
                    command: 'update-execute',
                    description: 'Executes the updating of a node with a specific version of Hedera platform',
                    commandDef: self,
                    handler: 'updateExecute',
                }, NodeFlags.UPDATE_SUBMIT_TRANSACTIONS_FLAGS))
                    .command(new YargsCommand({
                    command: 'delete',
                    description: 'Delete a node with a specific version of Hedera platform',
                    commandDef: self,
                    handler: 'delete',
                }, NodeFlags.DELETE_FLAGS))
                    .command(new YargsCommand({
                    command: 'delete-prepare',
                    description: 'Prepares the deletion of a node with a specific version of Hedera platform',
                    commandDef: self,
                    handler: 'deletePrepare',
                }, NodeFlags.DELETE_PREPARE_FLAGS))
                    .command(new YargsCommand({
                    command: 'delete-submit-transactions',
                    description: 'Submits transactions to the network nodes for deleting a node',
                    commandDef: self,
                    handler: 'deleteSubmitTransactions',
                }, NodeFlags.DELETE_SUBMIT_TRANSACTIONS_FLAGS))
                    .command(new YargsCommand({
                    command: 'delete-execute',
                    description: 'Executes the deletion of a previously prepared node',
                    commandDef: self,
                    handler: 'deleteExecute',
                }, NodeFlags.DELETE_EXECUTE_FLAGS))
                    .command(new YargsCommand({
                    command: 'prepare-upgrade',
                    description: 'Prepare the network for a Freeze Upgrade operation',
                    commandDef: self,
                    handler: 'prepareUpgrade',
                }, NodeFlags.PREPARE_UPGRADE_FLAGS))
                    .command(new YargsCommand({
                    command: 'freeze-upgrade',
                    description: 'Performs a Freeze Upgrade operation with on the network after it has been prepared with prepare-upgrade',
                    commandDef: self,
                    handler: 'freezeUpgrade',
                }, NodeFlags.PREPARE_UPGRADE_FLAGS))
                    .command(new YargsCommand({
                    command: 'upgrade',
                    description: 'upgrades all nodes on the network',
                    commandDef: self,
                    handler: 'upgrade',
                }, NodeFlags.UPGRADE_FLAGS))
                    .command(new YargsCommand({
                    command: 'upgrade-prepare',
                    description: 'Prepare the deployment to upgrade network',
                    commandDef: self,
                    handler: 'upgradePrepare',
                }, NodeFlags.UPGRADE_PREPARE_FLAGS))
                    .command(new YargsCommand({
                    command: 'upgrade-submit-transactions',
                    description: 'Submit transactions for upgrading network',
                    commandDef: self,
                    handler: 'upgradeSubmitTransactions',
                }, NodeFlags.UPGRADE_SUBMIT_TRANSACTIONS_FLAGS))
                    .command(new YargsCommand({
                    command: 'upgrade-execute',
                    description: 'Executes the upgrading the network',
                    commandDef: self,
                    handler: 'upgradeExecute',
                }, NodeFlags.UPGRADE_EXECUTE_FLAGS))
                    .command(new YargsCommand({
                    command: 'download-generated-files',
                    description: 'Downloads the generated files from an existing node',
                    commandDef: self,
                    handler: 'downloadGeneratedFiles',
                }, NodeFlags.DEFAULT_FLAGS))
                    .demandCommand(1, 'Select a node command');
            },
        };
    }
};
NodeCommand = NodeCommand_1 = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.AccountManager)),
    __param(1, inject(InjectTokens.NodeCommandHandlers)),
    __metadata("design:paramtypes", [Function, Function])
], NodeCommand);
export { NodeCommand };
//# sourceMappingURL=index.js.map