#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.startChat = main;
const CliChatService_js_1 = require("./chat/CliChatService.js");
const InternalChatModule_js_1 = require("./chat/modules/InternalChatModule.js");
const config_js_1 = require("./config.js");
const loadRenderer_js_1 = require("./chat/renderer/loadRenderer.js");
async function main() {
    try {
        let config;
        try {
            config = await (0, config_js_1.getConfig)();
        }
        catch (configError) {
            console.warn("Warning: Could not load config, using default plugins:", configError);
            config = {
                plugins: {
                    enabled: [
                        "embeddings",
                        "language",
                        "vim",
                        "github",
                        "asana",
                        "jira",
                        "linear",
                        "download",
                        "figma",
                        "url",
                    ],
                    disabled: [],
                },
            };
        }
        const chatService = new CliChatService_js_1.CliChatService(config.plugins?.enabled ?? []);
        const rendererSpecifier = config.chat?.renderer;
        if (rendererSpecifier) {
            try {
                const customRenderer = await (0, loadRenderer_js_1.loadRenderer)(rendererSpecifier);
                chatService.setContext({ renderer: customRenderer });
                console.log(`✓ Loaded custom renderer: ${rendererSpecifier}`);
            }
            catch (err) {
                console.warn(`⚠ Could not load renderer "${rendererSpecifier}": ${err.message}`);
                console.warn("  Falling back to default ConsoleRenderer.");
            }
        }
        const rootModuleSpecifier = config.chat?.rootModule;
        if (rootModuleSpecifier) {
            try {
                const customModule = await (0, loadRenderer_js_1.loadRootModule)(rootModuleSpecifier);
                await customModule.initialize(chatService);
                console.log(`✓ Loaded custom root module: ${rootModuleSpecifier}`);
            }
            catch (err) {
                console.warn(`⚠ Could not load root module "${rootModuleSpecifier}": ${err.message}`);
                console.warn("  Falling back to InternalChatModule.");
                const internalModule = new InternalChatModule_js_1.InternalChatModule();
                await internalModule.initialize(chatService);
            }
        }
        else {
            const internalModule = new InternalChatModule_js_1.InternalChatModule();
            await internalModule.initialize(chatService);
        }
        const moduleSpecifiers = config.chat?.modules ?? [];
        for (const specifier of moduleSpecifiers) {
            try {
                const chatModule = await (0, loadRenderer_js_1.loadChatModule)(specifier);
                await chatModule.initialize(chatService);
                console.log(`✓ Loaded chat module: ${specifier}`);
            }
            catch (err) {
                console.warn(`⚠ Could not load chat module "${specifier}": ${err.message}`);
            }
        }
        await chatService.startChatLoop();
    }
    catch (error) {
        console.error("Error starting chat:", error);
        process.exit(1);
    }
}
const isMainModule = (process.argv[1] && process.argv[1].endsWith("chat2.ts")) ||
    (process.argv[1] && process.argv[1].endsWith("chat2.js")) ||
    (process.argv[1] && process.argv[1].endsWith("chat.ts")) ||
    (process.argv[1] && process.argv[1].endsWith("chat.js"));
if (isMainModule) {
    main().catch(console.error);
}
//# sourceMappingURL=chat.js.map