"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runInteractiveShell = runInteractiveShell;
const promises_1 = require("node:readline/promises");
const node_process_1 = require("node:process");
const logger_1 = require("../logger/logger");
const DZapSDK_1 = require("../sdk/DZapSDK");
const ask_1 = require("./ask");
const commands_1 = require("./commands");
const config_1 = require("./config");
const sdk_1 = require("./sdk");
const terminal_1 = require("./terminal");
async function authorizeShellStartup(rl, sdk) {
    while (true) {
        const startupPassword = (await rl.question((0, terminal_1.colorize)("Startup password: ", terminal_1.ansi.cyan, terminal_1.ansi.bold))).trim();
        if (startupPassword.length === 0) {
            (0, terminal_1.logLine)("warn", "Startup password is required.");
            continue;
        }
        try {
            await (0, sdk_1.initializeSdk)(sdk, startupPassword);
            return;
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            if (message.includes("Invalid startup password.")) {
                (0, terminal_1.logLine)("error", "Incorrect startup password.");
                continue;
            }
            throw error;
        }
    }
}
async function runInteractiveShell() {
    const rl = (0, promises_1.createInterface)({ input: node_process_1.stdin, output: node_process_1.stdout, terminal: true });
    const config = await (0, config_1.loadConfig)();
    (0, terminal_1.printBanner)();
    const sdk = new DZapSDK_1.DZapSDK();
    await authorizeShellStartup(rl, sdk);
    const profile = await (0, config_1.resolveProfile)(rl, config);
    const state = {
        profile,
        debug: (0, config_1.resolveInitialDebug)(config),
        confirmTransactions: (0, config_1.resolveInitialConfirmTransactions)(config),
    };
    logger_1.logger.level = state.debug ? "info" : "error";
    if (state.debug) {
        (0, terminal_1.logLine)("info", "Debug mode is ON. Use /debug off for concise logs.");
    }
    if (state.confirmTransactions) {
        (0, terminal_1.logLine)("info", "Transaction confirmation mode is ON. Use /confirmmode off to auto-execute tx.");
    }
    (0, terminal_1.printShellHelp)();
    console.log("");
    while (true) {
        let line;
        try {
            line = await rl.question((0, terminal_1.colorize)(">> ", terminal_1.ansi.cyan, terminal_1.ansi.bold));
        }
        catch (_a) {
            break;
        }
        const trimmed = line.trim();
        if (trimmed.length === 0) {
            continue;
        }
        const parsedCommand = (0, commands_1.parseSlashCommand)(trimmed);
        try {
            if (parsedCommand) {
                const shouldContinue = await (0, commands_1.handleSlashCommand)(sdk, rl, state, config, parsedCommand, sdk_1.initializeSdk);
                if (!shouldContinue) {
                    break;
                }
                continue;
            }
            await (0, ask_1.handleAskInput)(sdk, state, trimmed, rl);
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            (0, terminal_1.logLine)("error", message);
        }
    }
    rl.close();
}
