import { SlashCommandParser } from '../../parser.js';
import { argumentAfterPrefix, slashMessageResult } from '../results.js';
export function createSessionSlashCommandModule() {
    return {
        id: 'session',
        hints: [
            { command: '/continue', description: 'resume from the current transcript' },
            { command: '/clear', description: 'reset the current session transcript' },
            { command: '/session list', description: 'list local chat sessions' },
            { command: '/session choose [query]', description: 'pick a recent session with filtering' },
            { command: '/session new [name]', description: 'create and switch to a new session' },
            { command: '/session switch <id>', description: 'switch to another session' },
            { command: '/session continue <id>', description: 'switch to a session and resume it' },
            { command: '/session rename <name>', description: 'rename the current session' },
            { command: '/session pin', description: 'pin the current session' },
            { command: '/session unpin', description: 'unpin the current session' },
            { command: '/session close <id>', description: 'remove a saved session' },
        ],
        commands: [
            {
                id: 'session.continue-current',
                syntax: '/continue',
                description: 'resume from the current transcript',
                match: SlashCommandParser.matchesExact('/continue'),
                run: () => ({ handled: true, kind: 'continue' }),
            },
            {
                id: 'session.clear',
                syntax: '/clear',
                description: 'reset the current session transcript',
                match: SlashCommandParser.matchesExact('/clear'),
                run: (context) => {
                    context.session.clear();
                    return slashMessageResult('Cleared the current chat transcript.');
                },
            },
            {
                id: 'session.list',
                syntax: '/session list',
                description: 'list local chat sessions',
                match: SlashCommandParser.matchesExact('/session list'),
                run: (context) => slashMessageResult(context.session.all().length > 0 ?
                    context.session.recentListMessage().join('\n')
                    : 'No sessions available.'),
            },
            {
                id: 'session.choose.help',
                syntax: '/session choose',
                description: 'pick a recent session with filtering',
                match: SlashCommandParser.matchesExact('/session choose'),
                run: () => slashMessageResult('Use /session choose <query> to filter recent sessions, then use arrows and Enter to choose one.'),
            },
            {
                id: 'session.new',
                syntax: '/session new [name]',
                description: 'create and switch to a new session',
                match: SlashCommandParser.matchesPrefix('/session new'),
                run: (context, input) => createSession(context, argumentAfterPrefix(input, '/session new')),
            },
            {
                id: 'session.switch',
                syntax: '/session switch <id>',
                description: 'switch to another session',
                match: matchesRequiredSessionArgument('/session switch'),
                run: (context, input) => switchSession(context, argumentAfterPrefix(input, '/session switch')),
            },
            {
                id: 'session.continue',
                syntax: '/session continue <id>',
                description: 'switch to a session and resume it',
                match: matchesRequiredSessionArgument('/session continue'),
                run: (context, input) => continueSession(context, argumentAfterPrefix(input, '/session continue')),
            },
            {
                id: 'session.rename',
                syntax: '/session rename <name>',
                description: 'rename the current session',
                match: matchesRequiredSessionArgument('/session rename'),
                run: (context, input) => renameSession(context, argumentAfterPrefix(input, '/session rename')),
            },
            {
                id: 'session.pin',
                syntax: '/session pin',
                description: 'pin the current session',
                match: SlashCommandParser.matchesExact('/session pin'),
                run: (context) => updatePinnedSession(context, true),
            },
            {
                id: 'session.unpin',
                syntax: '/session unpin',
                description: 'unpin the current session',
                match: SlashCommandParser.matchesExact('/session unpin'),
                run: (context) => updatePinnedSession(context, false),
            },
            {
                id: 'session.close',
                syntax: '/session close <id>',
                description: 'remove a saved session',
                match: matchesRequiredSessionArgument('/session close'),
                run: (context, input) => closeSession(context, argumentAfterPrefix(input, '/session close')),
            },
        ],
    };
}
function matchesRequiredSessionArgument(prefix) {
    return (input) => input.raw.startsWith(`${prefix} `);
}
export function resolveSessionReference(args) {
    const directMatch = args.sessions.find((candidate) => candidate.id === args.value);
    if (directMatch) {
        return directMatch;
    }
    const numericIndex = Number.parseInt(args.value, 10);
    return Number.isFinite(numericIndex) && numericIndex > 0 ? args.recentSessions[numericIndex - 1] : undefined;
}
function createSession(context, name) {
    const session = context.session.create(name || undefined);
    return slashMessageResult(`Created and switched to ${session.id} (${session.name}).`, session.id);
}
function switchSession(context, value) {
    const session = findSession(context, value);
    if (!session) {
        return slashMessageResult(`Unknown session: ${value}. Use /session list to inspect available sessions.`);
    }
    context.session.switch(session.id);
    return slashMessageResult(`Switched to ${session.id} (${session.name}).\n${context.session.summarize(session)}`, session.id);
}
function continueSession(context, value) {
    const session = findSession(context, value);
    if (!session) {
        return slashMessageResult(`Unknown session: ${value}.\nUse /session list to inspect available sessions.`);
    }
    return {
        handled: true,
        kind: 'continue',
        sessionId: session.id,
        message: `Switched to ${session.id} (${session.name}).\nContinuing from that session transcript.`,
    };
}
function renameSession(context, name) {
    if (!name) {
        return slashMessageResult('Usage: /session rename <name>');
    }
    context.session.rename(name);
    return slashMessageResult(`Renamed current session to ${name}.`);
}
function updatePinnedSession(context, pinned) {
    context.session.setPinned(pinned);
    return slashMessageResult(pinned ? 'Pinned current session.' : 'Unpinned current session.');
}
function closeSession(context, value) {
    const session = findSession(context, value);
    if (!session) {
        return slashMessageResult(`Unknown session: ${value}.\nUse /session list to inspect available sessions.`);
    }
    context.session.remove(session.id);
    return slashMessageResult(`Closed ${session.id} (${session.name}).`);
}
function findSession(context, value) {
    return resolveSessionReference({
        sessions: context.session.all(),
        recentSessions: context.session.recent(),
        value,
    });
}
//# sourceMappingURL=session-commands.js.map