"""Chat conversation scroll helpers (unit-testable, no GTK import)."""

from __future__ import annotations


def chat_near_bottom(*, upper: float, page_size: float, value: float, threshold: float = 48.0) -> bool:
    """Return True when the viewport is already pinned near the latest messages."""
    if upper <= page_size:
        return True
    distance = upper - page_size - value
    return distance <= threshold


def chat_scroll_bottom_value(*, upper: float, page_size: float) -> float:
    """Scroll adjustment value that shows the bottom of the conversation."""
    return max(0.0, upper - page_size)


def should_follow_new_message(
    *,
    upper: float,
    page_size: float,
    value: float,
    threshold: float = 48.0,
) -> bool:
    """Auto-scroll on new messages when the user has not scrolled up to read history."""
    return chat_near_bottom(upper=upper, page_size=page_size, value=value, threshold=threshold)
