/**
 * Session resolution from CLI flags.
 *
 * Translates --session, --no-session, --continue, and --session-dir flags
 * into a concrete SessionManager. Supports local lookup, global search
 * (across all project directories), and cross-project session forking.
 */
import chalk from 'chalk';
import { createInterface } from 'readline';
import { listAllSessions, listSessions } from '../app/session/session-catalog.js';
import { continueRecentSessionManager, createInMemorySessionManager, createSessionManager as createManagedSession, forkSessionManager, openSessionManager } from '../app/session/session-factory.js';
async function resolveSessionPath(sessionArg, cwd, sessionDir) {
    if (sessionArg.includes('/') || sessionArg.includes('\\') || sessionArg.endsWith('.jsonl')) {
        return { type: 'path', path: sessionArg };
    }
    const localSessions = await listSessions(cwd, sessionDir);
    const localMatches = localSessions.filter((s) => s.id.startsWith(sessionArg));
    if (localMatches.length >= 1) {
        return { type: 'local', path: localMatches[0].path };
    }
    const allSessions = await listAllSessions();
    const globalMatches = allSessions.filter((s) => s.id.startsWith(sessionArg));
    if (globalMatches.length >= 1) {
        const match = globalMatches[0];
        return { type: 'global', path: match.path, cwd: match.cwd };
    }
    return { type: 'not_found', arg: sessionArg };
}
export async function promptConfirm(message) {
    return new Promise((resolve) => {
        const rl = createInterface({
            input: process.stdin,
            output: process.stdout
        });
        rl.question(`${message} [y/N] `, (answer) => {
            rl.close();
            resolve(answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes');
        });
    });
}
/** Create a SessionManager based on CLI session flags, prompting for fork confirmation when needed. */
export async function createSessionManager(parsed, cwd) {
    if (parsed.noSession) {
        return createInMemorySessionManager();
    }
    if (parsed.session) {
        const resolved = await resolveSessionPath(parsed.session, cwd, parsed.sessionDir);
        switch (resolved.type) {
            case 'path':
            case 'local':
                return openSessionManager(resolved.path, parsed.sessionDir);
            case 'global': {
                console.log(chalk.yellow(`Session found in different project: ${resolved.cwd}`));
                const shouldFork = await promptConfirm('Fork this session into current directory?');
                if (!shouldFork) {
                    console.log(chalk.dim('Aborted.'));
                    process.exit(0);
                }
                return forkSessionManager(resolved.path, cwd, parsed.sessionDir);
            }
            case 'not_found':
                console.error(chalk.red(`No session found matching '${resolved.arg}'`));
                process.exit(1);
        }
    }
    if (parsed.continue) {
        return continueRecentSessionManager(cwd, parsed.sessionDir);
    }
    if (parsed.sessionDir) {
        return createManagedSession(cwd, parsed.sessionDir);
    }
    return undefined;
}
//# sourceMappingURL=session-selection.js.map