import { randomUUID } from "node:crypto";
import { getDefaultMemoryPriority } from "./memory-schema.js";
function textMatches(event, query) {
    const normalizedQuery = query.trim().toLowerCase();
    if (!normalizedQuery)
        return true;
    return [event.summary, event.content, ...(event.tags ?? []), ...(event.filePaths ?? [])]
        .filter((value) => Boolean(value))
        .some((value) => value.toLowerCase().includes(normalizedQuery));
}
function eventMatchesRequest(event, request) {
    if (request.projectId && event.projectId !== request.projectId)
        return false;
    if (request.sessionId && event.sessionId !== request.sessionId)
        return false;
    if (request.types && !request.types.includes(event.type))
        return false;
    if (request.filePaths && !request.filePaths.some((filePath) => event.filePaths?.includes(filePath)))
        return false;
    if (request.tags && !request.tags.some((tag) => event.tags?.includes(tag)))
        return false;
    return textMatches(event, request.query);
}
export class InMemoryMemoryStore {
    kind = "memory";
    events = [];
    session;
    async initialize(session) {
        this.session = session;
    }
    async recordEvent(session, event) {
        const memoryEvent = {
            ...event,
            id: randomUUID(),
            projectId: session.projectId,
            sessionId: session.sessionId,
            priority: event.priority ?? getDefaultMemoryPriority(event.type),
            createdAt: event.createdAt ?? new Date().toISOString(),
        };
        this.events.push(memoryEvent);
        return memoryEvent;
    }
    async search(request) {
        const limit = request.limit ?? 10;
        return this.events
            .filter((event) => eventMatchesRequest(event, request))
            .slice(-limit)
            .reverse()
            .map((event, index) => ({ event, score: limit - index, reason: "in-memory substring match" }));
    }
    async getRecent(projectId, limit) {
        return this.events
            .filter((event) => event.projectId === projectId)
            .slice(-limit)
            .reverse();
    }
    async deleteEvent(projectId, eventId) {
        const index = this.events.findIndex((event) => event.projectId === projectId && event.id === eventId);
        if (index === -1)
            return false;
        this.events.splice(index, 1);
        return true;
    }
    async purgeProject(projectId) {
        const before = this.events.length;
        this.events = this.events.filter((event) => event.projectId !== projectId);
        return before - this.events.length;
    }
    async getStatus() {
        return {
            enabled: true,
            projectId: this.session?.projectId,
            sessionId: this.session?.sessionId,
            eventCount: this.events.length,
            store: this.kind,
        };
    }
    async close() {
        // In-memory store has no resources to release.
    }
}
export class NoopMemoryStore {
    kind = "noop";
    async initialize(_session) {
        // No-op store for disabled memory and early wiring.
    }
    async recordEvent(_session, _event) {
        return undefined;
    }
    async search(_request) {
        return [];
    }
    async getRecent(_projectId, _limit) {
        return [];
    }
    async deleteEvent(_projectId, _eventId) {
        return false;
    }
    async purgeProject(_projectId) {
        return 0;
    }
    async getStatus() {
        return { enabled: false, eventCount: 0, store: this.kind };
    }
    async close() {
        // No-op.
    }
}
//# sourceMappingURL=memory-store.js.map