import { listAllSessions } from '../app/session/session-catalog.js';
function compareSessions(a, b) {
    return b.modified.getTime() - a.modified.getTime();
}
function toSummary(session, currentSessionPath) {
    return {
        path: session.path,
        id: session.id,
        cwd: session.cwd,
        name: session.name,
        parentSessionPath: session.parentSessionPath,
        createdAt: session.created.toISOString(),
        modifiedAt: session.modified.toISOString(),
        messageCount: session.messageCount,
        firstMessage: session.firstMessage,
        lastMessage: session.lastMessage,
        isCurrent: session.path === currentSessionPath
    };
}
function findByRequestedId(sessions, requestedSessionId) {
    const exact = sessions.find((session) => session.id === requestedSessionId);
    if (exact) {
        return exact;
    }
    const prefixMatches = sessions.filter((session) => session.id.startsWith(requestedSessionId));
    if (prefixMatches.length === 1) {
        return prefixMatches[0];
    }
    if (prefixMatches.length > 1) {
        return 'ambiguous';
    }
    return undefined;
}
function pickClearWinner(sessions) {
    if (sessions.length === 0) {
        return undefined;
    }
    if (sessions.length === 1) {
        return sessions[0];
    }
    const [first, second] = [...sessions].sort(compareSessions);
    if (first && second && first.modified.getTime() > second.modified.getTime()) {
        return first;
    }
    return undefined;
}
export class RemoteSessionRegistry {
    listSessionsImpl;
    constructor(options) {
        this.listSessionsImpl = options?.listSessions ?? listAllSessions;
    }
    async listSessions(currentSessionPath) {
        const sessions = await this.listSessionsImpl();
        return [...sessions]
            .sort(compareSessions)
            .map((session) => toSummary(session, currentSessionPath));
    }
    async resolveTarget(options = {}) {
        const sessions = [...(await this.listSessionsImpl())].sort(compareSessions);
        if (options.requestedSessionPath) {
            const exactPath = sessions.find((session) => session.path === options.requestedSessionPath);
            if (exactPath) {
                return {
                    kind: 'attach',
                    session: toSummary(exactPath, options.currentSessionPath)
                };
            }
        }
        if (options.requestedSessionId) {
            const requested = findByRequestedId(sessions, options.requestedSessionId);
            if (requested && requested !== 'ambiguous') {
                return {
                    kind: 'attach',
                    session: toSummary(requested, options.currentSessionPath)
                };
            }
            if (requested === 'ambiguous') {
                return {
                    kind: 'picker',
                    reason: 'ambiguous',
                    sessions: sessions.map((session) => toSummary(session, options.currentSessionPath))
                };
            }
            return {
                kind: 'not_found',
                requestedSessionId: options.requestedSessionId
            };
        }
        if (options.currentSessionPath) {
            const current = sessions.find((session) => session.path === options.currentSessionPath);
            if (current) {
                return {
                    kind: 'attach',
                    session: toSummary(current, options.currentSessionPath)
                };
            }
        }
        if (sessions.length === 0) {
            return { kind: 'create' };
        }
        const winner = pickClearWinner(sessions);
        if (winner) {
            return {
                kind: 'attach',
                session: toSummary(winner, options.currentSessionPath)
            };
        }
        return {
            kind: 'picker',
            reason: 'ambiguous',
            sessions: sessions.map((session) => toSummary(session, options.currentSessionPath))
        };
    }
}
//# sourceMappingURL=session-registry.js.map