"""Multiline Avatar chat input sizing helpers (0.2.0.3)."""

from __future__ import annotations

MIN_INPUT_LINES = 1
MAX_INPUT_LINES = 6
DEFAULT_LINE_HEIGHT_PX = 22


def count_logical_lines(text: str) -> int:
    normalized = str(text or "").replace("\r\n", "\n").replace("\r", "\n")
    if not normalized:
        return MIN_INPUT_LINES
    return max(MIN_INPUT_LINES, len(normalized.split("\n")))


def clamp_input_lines(line_count: int) -> int:
    return max(MIN_INPUT_LINES, min(MAX_INPUT_LINES, int(line_count)))


def input_viewport_height_px(
    text: str,
    *,
    line_height_px: int = DEFAULT_LINE_HEIGHT_PX,
) -> int:
    """Pixel height for the input scroll area (1–6 lines, then internal scroll)."""
    lines = clamp_input_lines(count_logical_lines(text))
    return max(line_height_px, lines * line_height_px)


def input_needs_internal_scroll(text: str) -> bool:
    return count_logical_lines(text) > MAX_INPUT_LINES
