// ── Buffer defaults ───────────────────────────────────────────────────────────
/** Default maximum number of entries retained in a bounded diagnostic buffer. */
export const DEFAULT_BUFFER_LIMIT = 500;
/**
 * Default panel configuration used when none is provided.
 */
export const DEFAULT_PANEL_CONFIG = {
    bufferLimit: DEFAULT_BUFFER_LIMIT,
};
// ── Utility ───────────────────────────────────────────────────────────────────
/**
 * Apply a DiagnosticFilter to an array of entries.
 * Entries must have at minimum: `sessionId`, `traceId`, and a timestamp field.
 * The `timestampField` parameter names the property used for time range checks.
 *
 * @param entries - Source array (most recent last).
 * @param filter - Filter to apply.
 * @param getTimestamp - Accessor for the entry's primary timestamp.
 * @returns Filtered array, most recent first, capped at filter.limit.
 */
export function applyFilter(entries, filter, getTimestamp) {
    if (!filter) {
        return [...entries].reverse();
    }
    let result = [...entries];
    if (filter.sessionId !== undefined) {
        result = result.filter((e) => e.sessionId === filter.sessionId);
    }
    if (filter.traceId !== undefined) {
        result = result.filter((e) => e.traceId === filter.traceId);
    }
    if (filter.since !== undefined) {
        const since = filter.since;
        result = result.filter((e) => getTimestamp(e) >= since);
    }
    if (filter.until !== undefined) {
        const until = filter.until;
        result = result.filter((e) => getTimestamp(e) <= until);
    }
    // Most recent first
    result.reverse();
    const limit = filter.limit ?? DEFAULT_BUFFER_LIMIT;
    return result.slice(0, limit);
}
/**
 * Append an entry to a bounded buffer, discarding the oldest entry when
 * the limit is exceeded.
 *
 * @param buffer - Mutable buffer array (oldest first).
 * @param entry - Entry to append.
 * @param limit - Maximum buffer size.
 */
export function appendBounded(buffer, entry, limit) {
    buffer.push(entry);
    if (buffer.length > limit) {
        buffer.shift();
    }
}
