/** Interactive-shell session workflow for creating, resuming, and driving runtime turns. */
import { listAllSessions, listSessions } from '../../app/session/session-catalog.js';
import { openSessionManager } from '../../app/session/session-factory.js';
import { SessionSelectorComponent } from '../components/selectors/session-selector.js';
import { TreeSelectorComponent } from '../components/selectors/tree-selector.js';
export class SessionWorkflow {
    deps;
    listSessionsImpl;
    listAllSessionsImpl;
    openSessionManagerImpl;
    createTreeSelectorImpl;
    createSessionSelectorImpl;
    constructor(deps) {
        this.deps = deps;
        this.listSessionsImpl = deps.listSessions ?? listSessions;
        this.listAllSessionsImpl = deps.listAllSessions ?? listAllSessions;
        this.openSessionManagerImpl = deps.openSessionManager ?? openSessionManager;
        this.createTreeSelectorImpl =
            deps.createTreeSelector ??
                ((args) => {
                    const selector = new TreeSelectorComponent(args.tree, args.currentLeafId, args.terminalRows, args.onSelect, args.onCancel, args.onLabelChange, args.initialSelectedId);
                    return { component: selector, focus: selector };
                });
        this.createSessionSelectorImpl =
            deps.createSessionSelector ??
                ((args) => {
                    const selector = new SessionSelectorComponent((onProgress) => args.currentSessionsLoader(args.cwd, args.sessionDir, onProgress), args.allSessionsLoader, args.onSelect, args.onCancel, () => {
                        void args.onExit();
                    }, args.onRequestRender, {
                        renameSession: args.renameSession,
                        showRenameHint: true,
                        keybindings: args.keybindings
                    }, args.currentSessionFile);
                    return { component: selector, focus: selector };
                });
    }
    showTreeSelector(initialSelectedId) {
        const tree = this.deps.sessionManager.getTree();
        const currentLeafId = this.deps.sessionManager.getLeafId();
        if (tree.length === 0) {
            this.deps.showStatus('No entries in session');
            return;
        }
        this.deps.showSelector((done) => this.createTreeSelectorImpl({
            tree,
            currentLeafId,
            terminalRows: this.deps.terminalRows,
            onSelect: async (entryId) => {
                if (entryId === currentLeafId) {
                    done();
                    this.deps.showStatus('Already at this point');
                    return;
                }
                done();
                try {
                    const result = await this.deps.session.navigateTree(entryId);
                    if (result.cancelled) {
                        this.deps.showStatus('Navigation cancelled');
                        return;
                    }
                    this.deps.rebuildChat();
                    if (result.editorText && !this.deps.getEditorText().trim()) {
                        this.deps.setEditorText(result.editorText);
                    }
                    this.deps.showStatus('Navigated to selected point');
                }
                catch (error) {
                    this.deps.showError(error instanceof Error ? error.message : String(error));
                }
            },
            onCancel: () => {
                done();
                this.deps.requestRender();
            },
            onLabelChange: (entryId, label) => {
                this.deps.sessionManager.appendLabelChange(entryId, label);
                this.deps.requestRender();
            },
            initialSelectedId
        }));
    }
    showSessionSelector() {
        this.deps.showSelector((done) => this.createSessionSelectorImpl({
            currentSessionsLoader: this.listSessionsImpl,
            allSessionsLoader: this.listAllSessionsImpl,
            cwd: this.deps.sessionManager.getCwd(),
            sessionDir: this.deps.sessionManager.getSessionDir(),
            onSelect: async (sessionPath) => {
                done();
                await this.handleResumeSession(sessionPath);
            },
            onCancel: () => {
                done();
                this.deps.requestRender();
            },
            onExit: async () => this.deps.shutdown(),
            onRequestRender: () => this.deps.requestRender(),
            renameSession: async (sessionFilePath, nextName) => {
                const next = (nextName ?? '').trim();
                if (!next) {
                    return;
                }
                const manager = this.openSessionManagerImpl(sessionFilePath);
                manager.appendSessionInfo(next);
            },
            keybindings: this.deps.keybindings,
            currentSessionFile: this.deps.sessionManager.getSessionFile()
        }));
    }
    async handleResumeSession(sessionPath) {
        this.deps.resetTransientUiState();
        await this.deps.session.switchSession(sessionPath);
        this.deps.rebuildChat();
        this.deps.showStatus('Resumed session');
    }
}
//# sourceMappingURL=session-workflow.js.map