"use strict";
// ─── Silent-failure sink ────────────────────────────────────────────────
//
// Centralised landing zone for promise rejections that the call site
// chose to swallow (`.catch(() => {})`). Such swallows are legitimate
// for fire-and-forget paths — e.g. an event-store append should not
// fail a user-facing route — but the previous pattern left no record
// when the swallowed error mattered (disk full, EACCES on the audit
// log, etc.). This module gives us a ring buffer that the diagnostics
// endpoint can surface, so that "silent" failures become at least
// post-hoc visible without changing the call-site semantics.
//
// Design constraints:
//  - No I/O (the sink itself must never throw / never depend on disk).
//  - Bounded memory (ring buffer, default 200 entries).
//  - Synchronous read API (the diagnostics endpoint must not block).
//  - Cheap enough to drop on the hot path of every catch().
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordSwallowed = recordSwallowed;
exports.getSwallowedFailures = getSwallowedFailures;
exports.getSwallowedFailureCount = getSwallowedFailureCount;
exports.getSwallowedFailureDroppedCount = getSwallowedFailureDroppedCount;
exports.getSwallowedFailureTotalCount = getSwallowedFailureTotalCount;
exports._resetSwallowedFailuresForTest = _resetSwallowedFailuresForTest;
const MAX_ENTRIES = 200;
const buffer = [];
let droppedCount = 0;
let totalRecorded = 0;
/**
 * Record a swallowed promise rejection. Always succeeds; never throws.
 * Use directly inside `.catch(...)` blocks, e.g.
 *   somePromise().catch((err) => recordSwallowed('emitEvent', err))
 */
function recordSwallowed(label, error, meta) {
    try {
        const message = error instanceof Error ? error.message : String(error);
        buffer.push({ label, message, at: new Date().toISOString(), meta });
        totalRecorded++;
        if (buffer.length > MAX_ENTRIES) {
            const overflow = buffer.length - MAX_ENTRIES;
            droppedCount += overflow;
            buffer.splice(0, overflow);
        }
    }
    catch {
        // sink-of-last-resort: must never throw
    }
}
/** Return a shallow copy of the current ring buffer, oldest first. */
function getSwallowedFailures() {
    return buffer.slice();
}
/** Total swallowed-failure count since process start (post-buffer-trim count). */
function getSwallowedFailureCount() {
    return buffer.length;
}
/** Number of swallowed failures dropped from the ring buffer due to overflow. */
function getSwallowedFailureDroppedCount() {
    return droppedCount;
}
/** Total number of swallowed failures recorded since process start (including dropped). */
function getSwallowedFailureTotalCount() {
    return totalRecorded;
}
/** Clear the buffer. Only intended for tests. */
function _resetSwallowedFailuresForTest() {
    buffer.length = 0;
    droppedCount = 0;
    totalRecorded = 0;
}
//# sourceMappingURL=silentFailureSink.js.map