import { useCallback } from "react";
import { appendMessagesToSession as appendSessionMessages, createCompactedSessionCheckpoint, } from "../../core/session-compaction.js";
import { log } from "../../core/logger.js";
/**
 * Owns session message persistence: appending new turn messages, persisting a
 * compacted checkpoint, and keeping `persistedIndexRef`/`sessionStatsRef`/the
 * session store in sync. Extracted verbatim from `App.tsx`.
 */
export function useSessionPersistence({ sessionManagerRef, sessionPathRef, sessionStatsRef, persistedIndexRef, messagesRef, cwdRef, currentProvider, currentModel, sessionStore, }) {
    const appendMessagesToSession = useCallback(async (sessionPath, messages, startIndex) => {
        const sm = sessionManagerRef.current;
        if (!sm)
            return;
        await appendSessionMessages(sm, sessionPath, messages, startIndex);
    }, [sessionManagerRef]);
    const persistCompactedSession = useCallback(async (compactedMessages) => {
        const sm = sessionManagerRef.current;
        if (!sm)
            return;
        const session = await createCompactedSessionCheckpoint(sm, {
            cwd: cwdRef.current,
            provider: currentProvider,
            model: currentModel,
            messages: compactedMessages,
        });
        sessionPathRef.current = session.path;
        sessionStatsRef.current.sessionId = session.id;
        persistedIndexRef.current = compactedMessages.length;
        if (sessionStore) {
            sessionStore.sessionPath = session.path;
            sessionStore.sessionId = session.id;
            sessionStore.messages = [...compactedMessages];
        }
        log("INFO", "compaction", "Persisted compacted session checkpoint", { path: session.path });
    }, [
        currentModel,
        currentProvider,
        sessionStore,
        sessionManagerRef,
        sessionPathRef,
        sessionStatsRef,
        persistedIndexRef,
        cwdRef,
    ]);
    const persistNewMessages = useCallback(async () => {
        const sp = sessionPathRef.current;
        if (!sp)
            return;
        const allMsgs = messagesRef.current;
        await appendMessagesToSession(sp, allMsgs, persistedIndexRef.current);
        persistedIndexRef.current = allMsgs.length;
        if (sessionStore) {
            sessionStore.messages = [...allMsgs];
            sessionStore.sessionPath = sp;
            sessionStore.sessionId = sessionStatsRef.current.sessionId;
        }
    }, [
        appendMessagesToSession,
        sessionStore,
        sessionPathRef,
        messagesRef,
        persistedIndexRef,
        sessionStatsRef,
    ]);
    return { appendMessagesToSession, persistCompactedSession, persistNewMessages };
}
//# sourceMappingURL=useSessionPersistence.js.map