import { CatalogEntryReadSchema, CatalogEntryWriteSchema, CatalogReadSchema, CatalogWriteSchema, SessionBodyReadSchema, SessionBodyWriteSchema, } from './chat-session-schemas.js';
export class ChatSessionCodec {
    static parseCatalog(value) {
        const parsed = CatalogReadSchema.safeParse(value);
        if (!parsed.success) {
            return undefined;
        }
        return {
            version: 1,
            sessions: parsed.data.sessions.flatMap((entry) => ChatSessionCodec.parseCatalogEntry(entry)),
        };
    }
    static parseSessionBody(value, args) {
        const parsed = SessionBodyReadSchema.safeParse(value);
        if (!parsed.success) {
            return [];
        }
        return [{
                ...parsed.data,
                ...args.entry,
                history: parsed.data.history ?? [],
                messages: ChatSessionCodec.resolveMessages(parsed.data.messages),
                turns: parsed.data.turns ?? [],
                queuedPrompts: parsed.data.queuedPrompts ?? [],
            }];
    }
    static projectCatalogEntry(session) {
        const parsed = CatalogEntryWriteSchema.parse(session);
        return {
            ...parsed,
            pinned: parsed.pinned ?? false,
        };
    }
    static serializeCatalog(catalog) {
        return `${JSON.stringify(CatalogWriteSchema.parse(catalog), null, 2)}\n`;
    }
    static serializeSessionBody(session) {
        return `${JSON.stringify(SessionBodyWriteSchema.parse(session), null, 2)}\n`;
    }
    static parseCatalogEntry(value) {
        const parsed = CatalogEntryReadSchema.safeParse(value);
        return parsed.success ? [ChatSessionCodec.normalizeCatalogEntry(parsed.data)] : [];
    }
    static normalizeCatalogEntry(value) {
        const createdAt = value.createdAt ?? new Date().toISOString();
        return {
            retention: undefined,
            workspaceId: undefined,
            model: undefined,
            reasoningEffort: undefined,
            lastContinuePrompt: undefined,
            context: undefined,
            archives: undefined,
            lease: undefined,
            ...value,
            createdAt,
            updatedAt: value.updatedAt ?? createdAt,
            driftEnabled: value.driftEnabled ?? false,
            pinned: value.pinned ?? false,
        };
    }
    static resolveMessages(messages) {
        return (messages ?? []).filter((message) => message.id !== 'intro' && message.id !== 'missing-key');
    }
}
//# sourceMappingURL=chat-session-codec.js.map