import { randomUUID } from "node:crypto";
/**
 * Default in-memory bound. Audit events are emitted on every credential
 * update/revoke, provider use, run creation, and permission decision, so an
 * unbounded array is a slow memory leak in any long-lived process. The
 * in-memory sink is intended for tests and ephemeral modes; production is
 * backed by {@link SqliteAuditSink}. When the cap is exceeded the oldest events
 * are dropped (the durable sink keeps the full append-only history).
 */
export const DEFAULT_IN_MEMORY_AUDIT_CAPACITY = 1_000;
export class InMemoryAuditSink {
    #events = [];
    #capacity;
    constructor(capacity = DEFAULT_IN_MEMORY_AUDIT_CAPACITY) {
        if (!Number.isInteger(capacity) || capacity < 1) {
            throw new RangeError("InMemoryAuditSink capacity must be a positive integer.");
        }
        this.#capacity = capacity;
    }
    append(event) {
        const stored = {
            id: randomUUID(),
            at: new Date().toISOString(),
            ...event,
        };
        this.#events.push(stored);
        if (this.#events.length > this.#capacity) {
            this.#events.splice(0, this.#events.length - this.#capacity);
        }
        return stored;
    }
    list() {
        return [...this.#events];
    }
}
