# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/3-Renderer.ipynb.

# %% auto 0
__all__ = ['ChatRenderer']

# %% ../../nbs/3-Renderer.ipynb 1
from typing import List, Dict, Any, Optional
from fasthtml.common import *
from .cards import ChatCard
from .session import Message, ChatSession

# %% ../../nbs/3-Renderer.ipynb 3
class ChatRenderer:
    """Renders chat components with customizable styling."""

    CHAT_DIV_ID = "chat-cards"

    def __init__(
        self,
        card: Optional[ChatCard] = None,
        input_placeholder: str = "Type a message...",
        input_style: Optional[str] = None,
        chat_container_style: Optional[str] = None,
    ):
        """
        Initialize ChatRenderer.

        Args:
            card: ChatCard instance for rendering messages
            input_placeholder: Placeholder text for input field
            input_style: Custom CSS style for input field
            chat_container_style: Custom CSS style for chat container
        """
        self.card = card or ChatCard()
        self.input_placeholder = input_placeholder
        self.input_style = input_style or (
            "width: 60%; max-width: 600px; padding: 0.75em; "
            "font-size: 1em; border-radius: 0.5em"
        )
        self.chat_container_style = chat_container_style or (
            "display: flex; flex-direction: column; gap: 10px;"
        )

    def render_message(self, message: Message) -> Any:
        """Render a single message."""
        return self.card(message.to_dict())

    def render_messages(self, messages: List[Message]) -> Any:
        """
        Render a list of messages in a container.

        Args:
            messages: List of Message objects to render

        Returns:
            FastHTML Div containing all rendered messages
        """
        return Div(
            *[self.render_message(msg) for msg in messages],
            id=self.CHAT_DIV_ID,
            cls="chat-cards",
            style=self.chat_container_style,
        )

    def render_messages_from_dicts(self, message_dicts: List[Dict[str, Any]]) -> Any:
        """
        Render messages from dictionary representations.

        Args:
            message_dicts: List of message dictionaries

        Returns:
            FastHTML Div containing all rendered messages
        """
        messages = [Message.from_dict(d) for d in message_dicts]
        return self.render_messages(messages)

    def render_input(self, input_id: str = "msg", autofocus: bool = True) -> Any:
        """
        Render the message input field.

        Args:
            input_id: HTML ID for the input element
            autofocus: Whether to autofocus the input

        Returns:
            FastHTML Input element
        """
        return Input(
            id=input_id,
            placeholder=self.input_placeholder,
            autofocus=autofocus,
            style=self.input_style,
        )

    def render_form(
        self,
        form_id: str = "form",
        form_style: Optional[str] = None,
        ws_send: bool = True,
    ) -> Any:
        """
        Render the input form with styling.

        Args:
            form_id: HTML ID for the form
            form_style: Custom CSS style for form
            ws_send: Whether form sends via WebSocket

        Returns:
            FastHTML Form element
        """
        form_style = form_style or (
            "display: flex; justify-content: center; "
            "margin-top: 20px; padding: 20px;"
        )

        return Form(
            self.render_input(),
            id=form_id,
            ws_send=ws_send,
            style=form_style,
        )

    def render_chat_interface(
        self,
        messages: List[Message],
        title: str = "Chat",
        header_style: Optional[str] = None,
        container_style: Optional[str] = None,
    ) -> Any:
        """
        Render complete chat interface with messages and input.

        Args:
            messages: List of messages to display
            title: Chat title/header
            header_style: Custom style for header
            container_style: Custom style for main container

        Returns:
            FastHTML Div with complete chat interface
        """
        header_style = header_style or "text-align: center; padding: 1em;"

        return Div(
            H1(title, style=header_style),
            self.render_messages(messages),
            self.render_form(),
            style=container_style,
        )
