"""Renderer registry: pick the first renderer that accepts a message."""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from rich.console import RenderableType

    from .base import ContentRenderer


class RendererRegistry:
    """Ordered list of renderers; first accepting renderer wins."""

    def __init__(self) -> None:
        self._renderers: list[ContentRenderer] = []

    def register(self, renderer: "ContentRenderer") -> None:
        self._renderers.append(renderer)

    def render(self, msg: dict) -> "RenderableType":
        for renderer in self._renderers:
            if renderer.accepts(msg):
                return renderer.render(msg)
        return str(msg)

    def render_plain(self, msg: dict) -> "RenderableType":
        """Borderless renderable for reading mode (first accepting renderer wins).

        Mirrors :meth:`render` but calls each renderer's ``render_plain``, which
        drops Panel/Padding chrome so a raw terminal selection copies clean text.
        """
        for renderer in self._renderers:
            if renderer.accepts(msg):
                return renderer.render_plain(msg)
        return str(msg)
