from __future__ import annotations

from collections import deque
from typing import Any, List, Deque

# Simple in-memory mirror for recent receipts (debug / replay aid)
_mirror_buffer: Deque[Any] = deque(maxlen=1000)
_mirror_enabled: bool = False


def enable_mirror(enabled: bool = True, maxlen: int = 1000) -> None:
    global _mirror_buffer, _mirror_enabled
    _mirror_enabled = enabled
    _mirror_buffer = deque(maxlen=maxlen)


def mirror_receipt(receipt: Any) -> None:
    if not _mirror_enabled:
        return
    try:
        _mirror_buffer.append(receipt)
    except Exception:
        # best-effort; never break primary path
        pass


def mirror_tail(n: int = 100) -> List[Any]:
    if not _mirror_enabled:
        return []
    return list(_mirror_buffer)[-n:]
