import { createLogger } from "../utils/logger.js";
const log = createLogger("audit-manager");
/**
 * Fan-out orchestrator for audit events.
 * Distributes each event to all registered sinks. Sink failures are
 * logged but never propagate — audit must never break tool execution.
 */
export class AuditManager {
    sinks = [];
    addSink(sink) {
        this.sinks.push(sink);
        log.debug(`Audit sink registered: ${sink.name}`);
    }
    emit(event) {
        for (const sink of this.sinks) {
            try {
                const result = sink.emit(event);
                if (result && typeof result.catch === "function") {
                    result.catch((err) => {
                        log.warn(`Audit sink "${sink.name}" async emit failed`, { error: String(err) });
                    });
                }
            }
            catch (err) {
                log.warn(`Audit sink "${sink.name}" emit failed`, { error: String(err) });
            }
        }
    }
    async flush() {
        await Promise.allSettled(this.sinks
            .filter((s) => s.flush)
            .map(async (s) => {
            try {
                await s.flush();
            }
            catch (err) {
                log.warn(`Audit sink "${s.name}" flush failed`, { error: String(err) });
            }
        }));
    }
    async close() {
        await this.flush();
        await Promise.allSettled(this.sinks
            .filter((s) => s.close)
            .map(async (s) => {
            try {
                await s.close();
            }
            catch (err) {
                log.warn(`Audit sink "${s.name}" close failed`, { error: String(err) });
            }
        }));
    }
}
//# sourceMappingURL=manager.js.map