import { BorderedLoader } from '../components/primitives/bordered-loader.js';
import { setTheme } from '../theme/runtime.js';
export class ReloadWorkflow {
    deps;
    createLoaderImpl;
    constructor(deps) {
        this.deps = deps;
        this.createLoaderImpl =
            deps.createLoader ??
                ((message) => new BorderedLoader(deps.ui, deps.theme, message, {
                    cancellable: false
                }));
    }
    async handleReloadCommand() {
        if (this.deps.session.isStreaming) {
            this.deps.showWarning('Wait for the current response to finish before reloading.');
            return;
        }
        if (this.deps.session.isCompacting) {
            this.deps.showWarning('Wait for compaction to finish before reloading.');
            return;
        }
        this.deps.resetExtensionUI();
        const loader = this.createLoaderImpl('Reloading extensions, skills, and prompts...');
        const previousEditor = this.deps.getEditor();
        this.deps.editorContainer.clear();
        this.deps.editorContainer.addChild(loader);
        this.deps.ui.setFocus(loader);
        this.deps.ui.requestRender();
        const dismissLoader = (editor) => {
            loader.dispose();
            this.deps.editorContainer.clear();
            this.deps.editorContainer.addChild(editor);
            this.deps.ui.setFocus(editor);
            this.deps.ui.requestRender();
        };
        try {
            const { warnings } = await this.deps.session.reload();
            for (const warning of warnings) {
                this.deps.showError(warning);
            }
            this.deps.setHideThinkingBlock(this.deps.getHideThinkingBlock());
            const themeName = this.deps.getThemeName();
            const themeResult = themeName ? setTheme(themeName, true) : { success: true };
            if (!themeResult.success) {
                this.deps.showError(`Failed to load theme "${themeName}": ${themeResult.error}\nFell back to dark theme.`);
            }
            this.deps.ui.setShowHardwareCursor(this.deps.getShowHardwareCursor());
            this.deps.ui.setClearOnShrink(this.deps.getClearOnShrink());
            this.deps.setupAutocomplete(this.deps.getFdPath());
            const runner = this.deps.session.extensionRunner;
            this.deps.rebuildChatFromMessages();
            dismissLoader(this.deps.getEditor());
            this.deps.showLoadedResources({
                extensionPaths: runner?.getExtensionPaths() ?? [],
                force: false,
                showDiagnosticsWhenQuiet: true
            });
            const modelsJsonError = this.deps.session.modelRegistry.getError();
            if (modelsJsonError) {
                this.deps.showError(`models.json error: ${modelsJsonError}`);
            }
            this.deps.showStatus('Reloaded extensions, skills, and prompts');
        }
        catch (error) {
            dismissLoader(previousEditor);
            this.deps.showError(`Reload failed: ${error instanceof Error ? error.message : String(error)}`);
        }
    }
}
//# sourceMappingURL=reload-workflow.js.map