"""Text output buffer — clean, TTS-ready output."""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from h2g2.engine.game_object import GameObject


class Output:
    """Accumulates game text for display and future TTS consumption.

    No ANSI codes, no terminal escape sequences — just plain text.
    """

    def __init__(self) -> None:
        self._buffer: list[str] = []

    def tell(self, *parts: str) -> None:
        """Append text fragments to the output buffer."""
        self._buffer.append("".join(parts))

    def newline(self) -> None:
        self._buffer.append("\n")

    def blank_line(self) -> None:
        self._buffer.append("\n\n")

    def describe(self, obj: "GameObject") -> None:
        """Output an object's description (equivalent to ZIL 'D')."""
        self.tell(obj.desc)

    def a_desc(self, obj: "GameObject") -> None:
        """Output 'a/an <desc>' or proper noun."""
        self.tell(obj.a_desc())

    def the_desc(self, obj: "GameObject") -> None:
        """Output 'the <desc>' or proper noun."""
        self.tell(obj.the_desc())

    def number(self, n: int) -> None:
        """Output a number."""
        self.tell(str(n))

    def flush(self) -> str:
        """Return accumulated text and clear the buffer."""
        result = "".join(self._buffer)
        self._buffer.clear()
        return result

    def has_content(self) -> bool:
        return bool(self._buffer)
