import { join } from "path";
import { CURRENT_SESSION_VERSION, } from "./session-manager-types.js";
import { generateId } from "./session-manager-validation.js";
function entryBase(byId, parentId) {
    return {
        id: generateId(byId),
        parentId,
        timestamp: new Date().toISOString(),
    };
}
export function createSessionHeader(id, cwd, timestamp = new Date().toISOString(), parentSession, internal, workflow) {
    const header = {
        type: "session",
        version: CURRENT_SESSION_VERSION,
        id,
        timestamp,
        cwd,
    };
    if (parentSession !== undefined)
        header.parentSession = parentSession;
    if (internal)
        header.internal = true;
    if (workflow)
        header.workflow = workflow;
    return header;
}
export function createSessionFilePath(sessionDir, timestamp, sessionId) {
    const fileTimestamp = timestamp.replace(/[:.]/g, "-");
    return join(sessionDir, `${fileTimestamp}_${sessionId}.jsonl`);
}
export function createMessageEntry(message, byId, parentId) {
    return {
        type: "message",
        ...entryBase(byId, parentId),
        message,
    };
}
export function createThinkingLevelChangeEntry(thinkingLevel, byId, parentId) {
    return {
        type: "thinking_level_change",
        ...entryBase(byId, parentId),
        thinkingLevel,
    };
}
export function createContextWindowChangeEntry(contextWindow, byId, parentId) {
    return {
        type: "context_window_change",
        ...entryBase(byId, parentId),
        contextWindow,
    };
}
export function createModelChangeEntry(provider, modelId, byId, parentId) {
    return {
        type: "model_change",
        ...entryBase(byId, parentId),
        provider,
        modelId,
    };
}
export function createContextCompactionEntry(deletedTargets, protectedEntryIds, stats, backupPath, byId, parentId) {
    return {
        type: "context_compaction",
        ...entryBase(byId, parentId),
        promptVersion: 1,
        deletedTargets,
        protectedEntryIds,
        stats,
        backupPath,
    };
}
export function createCustomEntry(customType, data, byId, parentId) {
    return {
        type: "custom",
        customType,
        data,
        ...entryBase(byId, parentId),
    };
}
export function createSessionInfoEntry(name, byId, parentId) {
    return {
        type: "session_info",
        ...entryBase(byId, parentId),
        name: name.trim(),
    };
}
export function getLatestSessionName(entries) {
    // Walk entries in reverse to find the latest session_info entry.
    // Empty names explicitly clear the session title.
    for (let i = entries.length - 1; i >= 0; i--) {
        const entry = entries[i];
        if (entry.type === "session_info") {
            return entry.name?.trim() || undefined;
        }
    }
    return undefined;
}
export function createCustomMessageEntry(customType, content, display, details, excludeFromContext, byId, parentId) {
    return {
        type: "custom_message",
        customType,
        content,
        display,
        details,
        ...(excludeFromContext === true ? { excludeFromContext: true } : {}),
        ...entryBase(byId, parentId),
    };
}
export function createLabelEntry(targetId, label, byId, parentId, timestamp) {
    return {
        type: "label",
        ...entryBase(byId, parentId),
        ...(timestamp !== undefined ? { timestamp } : {}),
        targetId,
        label,
    };
}
export function createBranchSummaryEntry(branchFromId, summary, details, fromHook, byId) {
    return {
        type: "branch_summary",
        ...entryBase(byId, branchFromId),
        fromId: branchFromId ?? "root",
        summary,
        details,
        fromHook,
    };
}
export function getEntriesWithoutHeader(fileEntries) {
    return fileEntries.filter((entry) => entry.type !== "session");
}
//# sourceMappingURL=session-manager-entries.js.map