import { useState, useCallback, useRef } from "react";
import { SessionManager } from "../../core/session-manager.js";
import crypto from "node:crypto";
export function useSessionManager(sessionsDir) {
    const managerRef = useRef(new SessionManager(sessionsDir));
    const [sessionId, setSessionId] = useState("");
    const [sessionPath, setSessionPath] = useState("");
    const create = useCallback(async (cwd, provider, model) => {
        const session = await managerRef.current.create(cwd, provider, model);
        setSessionId(session.id);
        setSessionPath(session.path);
    }, []);
    const load = useCallback(async (path) => {
        const loaded = await managerRef.current.load(path);
        return managerRef.current.getMessages(loaded.entries);
    }, []);
    const persistMessages = useCallback(async (messages, fromIndex) => {
        if (!sessionPath)
            return;
        for (let i = fromIndex; i < messages.length; i++) {
            const entry = {
                type: "message",
                id: crypto.randomUUID(),
                parentId: null,
                timestamp: new Date().toISOString(),
                message: messages[i],
            };
            await managerRef.current.appendEntry(sessionPath, entry);
        }
    }, [sessionPath]);
    const listSessions = useCallback(async (cwd) => {
        return managerRef.current.list(cwd);
    }, []);
    return {
        sessionId,
        sessionPath,
        create,
        load,
        persistMessages,
        listSessions,
    };
}
//# sourceMappingURL=useSessionManager.js.map