import { sessionManager } from '../../../session/session-manager.js';
import { formatError } from '../../../utils/error-formatter.js';
import { errorMsg, infoMsg } from '../../../utils/message-factory.js';
const RESUME_COMMANDS = ['resume', 'sessions', 'history'];
function isResumeCommand(commandName) {
    return RESUME_COMMANDS.includes(commandName.toLowerCase());
}
/**
 * Handles /resume, /sessions, /history (session resume).
 * No args: show session selector. One arg: resume by "last", id, or list index.
 * Returns true if handled.
 */
export async function handleResumeCommand(commandParts, options) {
    const commandName = commandParts[0]?.toLowerCase();
    if (!commandName || !isResumeCommand(commandName)) {
        return false;
    }
    const { onAddToChatQueue, onEnterSessionSelectorMode, onResumeSession, onCommandComplete, } = options;
    if (!onEnterSessionSelectorMode || !onResumeSession) {
        onAddToChatQueue(errorMsg('Session management is not available in this context.', 'resume-error'));
        onCommandComplete?.();
        return true;
    }
    const rawArgs = commandParts.slice(1);
    const showAll = rawArgs.includes('--all');
    const args = rawArgs.filter(a => a !== '--all');
    try {
        await sessionManager.initialize();
    }
    catch (error) {
        onAddToChatQueue(errorMsg(`Failed to initialize sessions: ${formatError(error)}`, 'resume-error'));
        onCommandComplete?.();
        return true;
    }
    if (args.length === 0) {
        onEnterSessionSelectorMode(showAll || undefined);
        onCommandComplete?.();
        return true;
    }
    const sessionIdOrSpecial = args[0];
    try {
        const listOptions = showAll ? undefined : { workingDirectory: process.cwd() };
        const sessions = await sessionManager.listSessions(listOptions);
        const sorted = [...sessions].sort((a, b) => new Date(b.lastAccessedAt).getTime() -
            new Date(a.lastAccessedAt).getTime());
        let sessionId = null;
        if (sessionIdOrSpecial.toLowerCase() === 'last') {
            if (sorted.length > 0)
                sessionId = sorted[0].id;
        }
        else {
            const index = Number.parseInt(sessionIdOrSpecial, 10);
            if (!Number.isNaN(index) && index >= 1 && index <= sorted.length) {
                sessionId = sorted[index - 1].id;
            }
            else {
                sessionId = sessionIdOrSpecial;
            }
        }
        if (!sessionId) {
            onAddToChatQueue(infoMsg('No sessions found.', 'resume-info'));
            onCommandComplete?.();
            return true;
        }
        const session = await sessionManager.loadSession(sessionId);
        if (session) {
            onResumeSession(session);
        }
        else {
            onAddToChatQueue(errorMsg(`Session not found: ${sessionId}`, 'resume-error'));
        }
    }
    catch (error) {
        onAddToChatQueue(errorMsg(`Failed to resume session: ${formatError(error)}`, 'resume-error'));
    }
    onCommandComplete?.();
    return true;
}
//# sourceMappingURL=session-handler.js.map