/** Builds app sessions by composing model, tools, persistence, and shell-facing adapters. */
import { randomUUID } from 'crypto';
import { dirname } from 'node:path';
import { CURRENT_SESSION_VERSION } from '../../core/session-schema.js';
import { SessionManager } from './session-manager.js';
import { getDefaultSessionDir } from './session-paths.js';
import { FileSessionPersistenceBackend } from './session-persistence-backend.js';
function resolvePersistenceBackend(persist, persistenceBackend) {
    if (!persist)
        return undefined;
    return persistenceBackend ?? new FileSessionPersistenceBackend();
}
function createSessionInit(cwd, sessionDir, initialSessionOptions, persistenceBackend) {
    return {
        cwd,
        sessionDir: sessionDir ?? getDefaultSessionDir(cwd),
        sessionFile: undefined,
        persist: true,
        persistenceBackend: resolvePersistenceBackend(true, persistenceBackend),
        initialSessionOptions
    };
}
function openSessionInit(path, sessionDir, persistenceBackend) {
    const backend = resolvePersistenceBackend(true, persistenceBackend);
    const sessionFile = backend.normalizeSessionRef(path);
    const entries = backend.loadEntries(sessionFile);
    const header = entries.find((entry) => entry.type === 'session');
    return {
        cwd: header?.cwd ?? process.cwd(),
        sessionDir: sessionDir ?? dirname(sessionFile),
        sessionFile,
        persist: true,
        persistenceBackend: backend
    };
}
function continueRecentSessionInit(cwd, sessionDir, persistenceBackend) {
    const dir = sessionDir ?? getDefaultSessionDir(cwd);
    const backend = resolvePersistenceBackend(true, persistenceBackend);
    const mostRecent = backend.findMostRecentSessionRef(dir);
    return {
        cwd,
        sessionDir: dir,
        sessionFile: mostRecent ?? undefined,
        persist: true,
        persistenceBackend: backend
    };
}
function inMemorySessionInit(cwd = process.cwd(), initialSessionOptions) {
    return {
        cwd,
        sessionDir: '',
        sessionFile: undefined,
        persist: false,
        persistenceBackend: undefined,
        initialSessionOptions
    };
}
function forkSessionInit(sourcePath, targetCwd, sessionDir, persistenceBackend) {
    const backend = resolvePersistenceBackend(true, persistenceBackend);
    const sourceSessionFile = backend.normalizeSessionRef(sourcePath);
    const sourceEntries = backend.loadEntries(sourceSessionFile);
    if (sourceEntries.length === 0) {
        throw new Error(`Cannot fork: source session file is empty or invalid: ${sourcePath}`);
    }
    const sourceHeader = sourceEntries.find((entry) => entry.type === 'session');
    if (!sourceHeader) {
        throw new Error(`Cannot fork: source session has no header: ${sourcePath}`);
    }
    const dir = sessionDir ?? getDefaultSessionDir(targetCwd);
    const newSessionId = randomUUID();
    const timestamp = new Date().toISOString();
    const newSessionFile = backend.createSessionRef(dir, newSessionId, timestamp);
    const newHeader = {
        type: 'session',
        version: CURRENT_SESSION_VERSION,
        id: newSessionId,
        timestamp,
        cwd: targetCwd,
        parentSession: sourceSessionFile
    };
    backend.rewriteEntries(newSessionFile, [
        newHeader,
        ...sourceEntries.filter((entry) => entry.type !== 'session')
    ]);
    return {
        cwd: targetCwd,
        sessionDir: dir,
        sessionFile: newSessionFile,
        persist: true,
        persistenceBackend: backend
    };
}
export function createSessionManager(cwd, sessionDir, initialSessionOptions, persistenceBackend) {
    const init = createSessionInit(cwd, sessionDir, initialSessionOptions, persistenceBackend);
    return new SessionManager(init.cwd, init.sessionDir, init.sessionFile, init.persist, init.persistenceBackend, init.initialSessionOptions);
}
export function openSessionManager(path, sessionDir, persistenceBackend) {
    const init = openSessionInit(path, sessionDir, persistenceBackend);
    return new SessionManager(init.cwd, init.sessionDir, init.sessionFile, init.persist, init.persistenceBackend);
}
export function continueRecentSessionManager(cwd, sessionDir, persistenceBackend) {
    const init = continueRecentSessionInit(cwd, sessionDir, persistenceBackend);
    return new SessionManager(init.cwd, init.sessionDir, init.sessionFile, init.persist, init.persistenceBackend);
}
export function createInMemorySessionManager(cwd = process.cwd(), initialSessionOptions) {
    const init = inMemorySessionInit(cwd, initialSessionOptions);
    return new SessionManager(init.cwd, init.sessionDir, init.sessionFile, init.persist, init.persistenceBackend, init.initialSessionOptions);
}
export function forkSessionManager(sourcePath, targetCwd, sessionDir, persistenceBackend) {
    const init = forkSessionInit(sourcePath, targetCwd, sessionDir, persistenceBackend);
    return new SessionManager(init.cwd, init.sessionDir, init.sessionFile, init.persist, init.persistenceBackend);
}
//# sourceMappingURL=session-factory.js.map