"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalChatModule = void 0;
const AgentModule_1 = require("./AgentModule");
const AskModule_1 = require("./AskModule");
const SearchModule_1 = require("./SearchModule");
const VoiceModule_1 = require("./VoiceModule");
const SystemModule_1 = require("./SystemModule");
const SetupModule_1 = require("./SetupModule");
const CustomCommandsModule_1 = require("./CustomCommandsModule");
const ShellCommandModule_1 = require("./ShellCommandModule");
const RendererModule_1 = require("./RendererModule");
const SessionsModule_1 = require("./SessionsModule");
const RemoteSyncModule_1 = require("./RemoteSyncModule");
class InternalChatModule {
    chatService;
    name = "internal";
    description = "Internal chat module aggregating all functionality";
    commands = [];
    modes = [];
    agentModule = new AgentModule_1.AgentModule();
    askModule = new AskModule_1.AskModule();
    searchModule = new SearchModule_1.SearchModule();
    sessionsModule;
    voiceModule = new VoiceModule_1.VoiceModule();
    systemModule = new SystemModule_1.SystemModule();
    setupModule;
    customCommandsModule = new CustomCommandsModule_1.CustomCommandsModule();
    shellCommandModule = new ShellCommandModule_1.ShellCommandModule();
    rendererModule;
    remoteSyncModule;
    constructor() {
        this.rendererModule = new RendererModule_1.RendererModule(this.agentModule);
        this.setupModule = new SetupModule_1.SetupModule(this.agentModule);
        this.sessionsModule = new SessionsModule_1.SessionsModule(this.agentModule);
        this.remoteSyncModule = new RemoteSyncModule_1.RemoteSyncModule(this.agentModule);
        this.agentModule.setRemoteSyncModule(this.remoteSyncModule);
    }
    async initialize(chatService) {
        this.chatService = chatService;
        chatService.registerModule(this);
        await this.rendererModule.initialize(chatService);
        await this.agentModule.initialize(chatService);
        await this.sessionsModule.initialize(chatService);
        await this.askModule.initialize(chatService);
        await this.searchModule.initialize(chatService);
        await this.voiceModule.initialize(chatService);
        await this.systemModule.initialize(chatService);
        await this.setupModule.initialize(chatService);
        await this.customCommandsModule.initialize(chatService);
        await this.remoteSyncModule.initialize(chatService);
        await this.shellCommandModule.initialize(chatService);
        chatService.registerCommand({
            name: "exit",
            description: "Exit the chat",
            handler: this.handleExitCommand.bind(this),
        });
        chatService.registerCommand({
            name: "multi",
            description: "Toggle multiline mode",
            handler: this.handleMultiCommand.bind(this),
        });
        for (const mode of this.getModes()) {
            chatService.registerMode(mode);
        }
    }
    getCommands() {
        const commands = [
            ...this.agentModule.getCommands(),
            ...this.sessionsModule.getCommands(),
            ...this.askModule.getCommands(),
            ...this.searchModule.getCommands(),
            ...this.voiceModule.getCommands(),
            ...this.systemModule.getCommands(),
            ...this.setupModule.getCommands(),
            ...this.customCommandsModule.getCommands(),
            ...this.shellCommandModule.getCommands(),
            ...this.rendererModule.getCommands(),
            ...this.remoteSyncModule.getCommands(),
            {
                name: "exit",
                description: "Exit the chat",
                handler: this.handleExitCommand.bind(this),
            },
            {
                name: "multi",
                description: "Toggle multiline mode",
                handler: this.handleMultiCommand.bind(this),
            },
        ];
        return commands;
    }
    getModes() {
        return [
            ...this.agentModule.getModes(),
            ...this.sessionsModule.getModes(),
            ...this.askModule.getModes(),
            ...this.searchModule.getModes(),
            ...this.voiceModule.getModes(),
            ...this.systemModule.getModes(),
            ...this.setupModule.getModes(),
            ...this.customCommandsModule.getModes(),
            ...this.shellCommandModule.getModes(),
            ...this.rendererModule.getModes(),
        ];
    }
    async handleExitCommand(args) {
        console.log("Goodbye!");
        process.exit(0);
    }
    async handleMultiCommand(args) {
        const context = this.chatService?.getContext();
        const newMultiMode = !context?.multilineMode;
        this.chatService?.setContext({ multilineMode: newMultiMode });
        console.log(`Multiline mode: ${newMultiMode ? "enabled" : "disabled"}`);
    }
    checkForFuzzyCommand(input) {
        if (!this.chatService)
            return false;
        const trimmedInput = input.toLowerCase().trim();
        const availableCommands = this.chatService.getCommands();
        const matchingCommand = availableCommands.find((cmd) => cmd.name.toLowerCase() === trimmedInput ||
            trimmedInput.startsWith(cmd.name.toLowerCase() + " "));
        if (matchingCommand) {
            console.log(`Did you mean "/${matchingCommand.name}"? Commands must start with "/"`);
            console.log(`Available commands: ${availableCommands
                .map((cmd) => `/${cmd.name}`)
                .join(", ")}`);
            return true;
        }
        return false;
    }
    async handleInput(input, context) {
        if (this.checkForFuzzyCommand(input)) {
            return true;
        }
        if (context.agentMode &&
            input
                .toLowerCase()
                .match(/^(multi|agent|ask|search|voice|system|exit|setup|help|clear)(\s|$)/i)) {
            return this.checkForFuzzyCommand(input);
        }
        if (await this.agentModule.handleInput(input, context)) {
            return true;
        }
        if (await this.searchModule.handleInput(input, context)) {
            return true;
        }
        if (await this.voiceModule.handleInput(input, context)) {
            return true;
        }
        return await this.askModule.handleInput(input, context);
    }
    async cleanup() {
    }
}
exports.InternalChatModule = InternalChatModule;
//# sourceMappingURL=InternalChatModule.js.map