"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StartCommand = void 0;
const core_1 = require("@oclif/core");
const path = require("path");
const fs = require("fs");
const lisk_commander_1 = require("lisk-commander");
const lisk_sdk_1 = require("lisk-sdk");
const lisk_framework_monitor_plugin_1 = require("@liskhq/lisk-framework-monitor-plugin");
const lisk_framework_forger_plugin_1 = require("@liskhq/lisk-framework-forger-plugin");
const lisk_framework_report_misbehavior_plugin_1 = require("@liskhq/lisk-framework-report-misbehavior-plugin");
const lisk_framework_faucet_plugin_1 = require("@liskhq/lisk-framework-faucet-plugin");
const lisk_framework_chain_connector_plugin_1 = require("@liskhq/lisk-framework-chain-connector-plugin");
const application_1 = require("../application");
const download_1 = require("./genesis-block/download");
const constants_1 = require("../constants");
const flags_1 = require("../utils/flags");
const setPluginConfig = (config, flags) => {
    var _a, _b, _c;
    if (flags['monitor-plugin-port'] !== undefined) {
        config.plugins[lisk_framework_monitor_plugin_1.MonitorPlugin.name] = (_a = config.plugins[lisk_framework_monitor_plugin_1.MonitorPlugin.name]) !== null && _a !== void 0 ? _a : {};
        config.plugins[lisk_framework_monitor_plugin_1.MonitorPlugin.name].port = flags['monitor-plugin-port'];
    }
    if (flags['monitor-plugin-whitelist'] !== undefined &&
        typeof flags['monitor-plugin-whitelist'] === 'string') {
        config.plugins[lisk_framework_monitor_plugin_1.MonitorPlugin.name] = (_b = config.plugins[lisk_framework_monitor_plugin_1.MonitorPlugin.name]) !== null && _b !== void 0 ? _b : {};
        config.plugins[lisk_framework_monitor_plugin_1.MonitorPlugin.name].whiteList = flags['monitor-plugin-whitelist']
            .split(',')
            .filter(Boolean);
    }
    if (flags['faucet-plugin-port'] !== undefined) {
        config.plugins[lisk_framework_faucet_plugin_1.FaucetPlugin.name] = (_c = config.plugins[lisk_framework_faucet_plugin_1.FaucetPlugin.name]) !== null && _c !== void 0 ? _c : {};
        config.plugins[lisk_framework_faucet_plugin_1.FaucetPlugin.name].port = flags['faucet-plugin-port'];
    }
};
class StartCommand extends lisk_commander_1.BaseStartCommand {
    async init() {
        const { flags } = await this.parse(StartCommand);
        if ([constants_1.NETWORK.MAINNET, constants_1.NETWORK.TESTNET].includes(flags.network) &&
            (!fs.existsSync(path.resolve(this.getApplicationConfigDir(), flags.network, 'genesis_block.blob')) ||
                flags['overwrite-genesis-block'])) {
            if (flags['overwrite-genesis-block']) {
                this.log(`Overwriting genesis block for "${flags.network}".`);
            }
            else {
                this.log(`Genesis block for "${flags.network}" does not exist.`);
            }
            const downloadParamsForAppConfig = [
                '--data-path',
                this.getApplicationDir(),
                '--network',
                flags.network,
            ];
            if (flags['overwrite-genesis-block']) {
                downloadParamsForAppConfig.push('--force');
            }
            if (flags['genesis-block-url']) {
                downloadParamsForAppConfig.push('--url', flags['genesis-block-url']);
            }
            await download_1.default.run(downloadParamsForAppConfig);
        }
    }
    async getApplication(config) {
        const { flags } = await this.parse(StartCommand);
        setPluginConfig(config, flags);
        const app = (0, application_1.getApplication)(lisk_sdk_1.utils.objects.mergeDeep({}, lisk_sdk_1.applicationConfigSchema.default, config));
        if (flags['enable-forger-plugin']) {
            app.registerPlugin(new lisk_framework_forger_plugin_1.ForgerPlugin(), { loadAsChildProcess: true });
        }
        if (flags['enable-monitor-plugin']) {
            app.registerPlugin(new lisk_framework_monitor_plugin_1.MonitorPlugin(), { loadAsChildProcess: true });
        }
        if (flags['enable-report-misbehavior-plugin']) {
            app.registerPlugin(new lisk_framework_report_misbehavior_plugin_1.ReportMisbehaviorPlugin(), { loadAsChildProcess: true });
        }
        if (flags['enable-faucet-plugin']) {
            app.registerPlugin(new lisk_framework_faucet_plugin_1.FaucetPlugin(), { loadAsChildProcess: true });
        }
        if (flags['enable-chain-connector-plugin']) {
            app.registerPlugin(new lisk_framework_chain_connector_plugin_1.ChainConnectorPlugin(), { loadAsChildProcess: true });
        }
        return app;
    }
    getApplicationConfigDir() {
        return path.join(__dirname, '../../config');
    }
    getApplicationDir() {
        return path.join(__dirname, '../..');
    }
}
StartCommand.flags = {
    ...lisk_commander_1.BaseStartCommand.flags,
    network: core_1.Flags.string({
        ...flags_1.flags.network,
        env: 'LISK_NETWORK',
        default: constants_1.DEFAULT_NETWORK,
    }),
    'enable-forger-plugin': core_1.Flags.boolean({
        description: 'Enable Forger Plugin. Environment variable "LISK_ENABLE_FORGER_PLUGIN" can also be used.',
        env: 'LISK_ENABLE_FORGER_PLUGIN',
        default: false,
    }),
    'enable-monitor-plugin': core_1.Flags.boolean({
        description: 'Enable Monitor Plugin. Environment variable "LISK_ENABLE_MONITOR_PLUGIN" can also be used.',
        env: 'LISK_ENABLE_MONITOR_PLUGIN',
        default: false,
    }),
    'monitor-plugin-port': core_1.Flags.integer({
        description: 'Port to be used for Monitor Plugin. Environment variable "LISK_MONITOR_PLUGIN_PORT" can also be used.',
        env: 'LISK_MONITOR_PLUGIN_PORT',
        dependsOn: ['enable-monitor-plugin'],
    }),
    'monitor-plugin-whitelist': core_1.Flags.string({
        description: 'List of IPs in comma separated value to allow the connection. Environment variable "LISK_MONITOR_PLUGIN_WHITELIST" can also be used.',
        env: 'LISK_MONITOR_PLUGIN_WHITELIST',
        dependsOn: ['enable-monitor-plugin'],
    }),
    'enable-report-misbehavior-plugin': core_1.Flags.boolean({
        description: 'Enable ReportMisbehavior Plugin. Environment variable "LISK_ENABLE_REPORT_MISBEHAVIOR_PLUGIN" can also be used.',
        env: 'LISK_ENABLE_REPORT_MISBEHAVIOR_PLUGIN',
        default: false,
    }),
    'enable-faucet-plugin': core_1.Flags.boolean({
        description: 'Enable Faucet Plugin. Environment variable "LISK_ENABLE_FAUCET_PLUGIN" can also be used.',
        env: 'LISK_ENABLE_FAUCET_PLUGIN',
        default: false,
    }),
    'faucet-plugin-port': core_1.Flags.integer({
        description: 'Port to be used for Faucet Plugin. Environment variable "LISK_FAUCET_PLUGIN_PORT" can also be used.',
        env: 'LISK_FAUCET_PLUGIN_PORT',
        dependsOn: ['enable-faucet-plugin'],
    }),
    'enable-dashboard-plugin': core_1.Flags.boolean({
        description: 'Enable Dashboard Plugin. Environment variable "LISK_ENABLE_DASHBOARD_PLUGIN" can also be used.',
        env: 'LISK_ENABLE_DASHBOARD_PLUGIN',
        default: false,
    }),
    'dashboard-plugin-port': core_1.Flags.integer({
        description: 'Port to be used for Dashboard Plugin. Environment variable "LISK_DASHBOARD_PLUGIN_PORT" can also be used.',
        env: 'LISK_DASHBOARD_PLUGIN_PORT',
        dependsOn: ['enable-dashboard-plugin'],
    }),
    'enable-chain-connector-plugin': core_1.Flags.boolean({
        description: 'Enable Chain Connector Plugin. Environment variable "LISK_ENABLE_CHAIN_CONNECTOR_PLUGIN" can also be used.',
        env: 'LISK_ENABLE_CHAIN_CONNECTOR_PLUGIN',
        default: false,
    }),
    'genesis-block-url': core_1.Flags.string({
        char: 'u',
        description: 'The URL to download the genesis block. Environment variable "LISK_GENESIS_BLOCK_URL" can also be used. Kindly ensure that the provided URL downloads the genesis block \'blob\' in the tarball format.',
        env: 'LISK_GENESIS_BLOCK_URL',
    }),
    'overwrite-genesis-block': core_1.Flags.boolean({
        description: 'Download and overwrite existing genesis block. Environment variable "LISK_GENESIS_BLOCK_OVERWRITE" can also be used.',
        env: 'LISK_GENESIS_BLOCK_OVERWRITE',
        default: false,
    }),
};
exports.StartCommand = StartCommand;
//# sourceMappingURL=start.js.map