import { ChatController } from "./chat_controller.js";
import { TuiChatView } from "./chat_view.js";
import { EXIT_DOUBLE_PRESS_WINDOW_MS, EXIT_TOAST_DURATION_MS } from "./constants.js";
import { SlashAutocompleteProvider } from "./ui/slash_autocomplete.js";
export class ChatApp {
    view;
    controller;
    lastCtrlCAt;
    constructor(options) {
        const queuedUserMessages = [];
        this.view = new TuiChatView({
            queuedUserMessages,
            compactToolUi: true,
            showThinking: false,
            terminalAppearance: options.terminalAppearance,
            themeId: options.config?.defaultTheme,
            themes: options.themes ?? [],
        });
        this.controller = new ChatController({
            ...options,
            view: this.view,
            queuedUserMessages,
        });
        const handlers = this.controller.getInputHandlers();
        handlers.onCtrlC = () => {
            const now = Date.now();
            if (this.lastCtrlCAt !== undefined && now - this.lastCtrlCAt <= EXIT_DOUBLE_PRESS_WINDOW_MS) {
                this.lastCtrlCAt = undefined;
                void this.stop().finally(() => process.exit(0));
                return;
            }
            this.lastCtrlCAt = now;
            this.view.addSystemMessage("press ctrl+c again to quit", "warn", {
                toastDurationMs: EXIT_TOAST_DURATION_MS,
            });
        };
        this.view.bindInputHandlers(handlers);
        const sources = this.controller.getAutocompleteSources();
        this.view.setAutocompleteProvider(new SlashAutocompleteProvider(this.controller.getCommandRegistry(), sources.personas, sources.prompts, sources.themes, sources.bashCommands, sources.projectFiles, sources.skills, sources.subagents, sources.riskLevels));
    }
    async start() {
        this.view.start();
        await this.controller.start();
    }
    async stop() {
        this.view.stop();
        try {
            await this.controller.dispose();
        }
        catch {
            // best-effort cleanup
        }
    }
    async onUserInput(text) {
        await this.controller.onUserInput(text);
    }
    onInterrupt() {
        this.controller.onInterrupt();
    }
    onEvent(event) {
        this.controller.onEvent(event);
    }
}
//# sourceMappingURL=app.js.map