# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/0-Card.ipynb.

# %% auto 0
__all__ = ['CHAT_DIV_ID', 'ChatCard', 'render_chat_list', 'mk_inp']

# %% ../../nbs/0-Card.ipynb 2
from fasthtml.common import *
from monsterui.all import TextPresets

# %% ../../nbs/0-Card.ipynb 4
class ChatCard:
    def __init__(
        self,
        user_emoji: str = "🗣️",
        assistant_emoji: str = "🕵️‍♂️",
        user_align: str = "right",
        assistant_align: str = "left",
        user_self_align: str = "flex-end",
        assistant_self_align: str = "flex-start",
        content_white_space: str = "normal",
        spinner_class: str = "spinner",
    ):
        self.emojis = {"User": user_emoji, "Assistant": assistant_emoji}

        self.alignments = {
            "User": {"text": user_align, "self": user_self_align},
            "Assistant": {"text": assistant_align, "self": assistant_self_align},
        }

        self.content_white_space = content_white_space
        self.spinner_class = spinner_class

    def get_mobile_styles(self) -> str:
        """Return mobile CSS handled in the renderer styles."""
        return ""

    def render(self, data: dict):
        """
        Render a chat card with the given data.

        Args:
            data: Dictionary containing:
                - role: "User" or "Assistant"
                - content: Message content (optional)
                - pending: Boolean indicating if message is loading (optional)

        Returns:
            FastHTML Div element representing the chat card
        """
        role = data["role"]
        content = data.get("content", "")
        pending = data.get("pending", False)

        emoji = self.emojis[role]
        align = self.alignments[role]
        row_cls = "chat-row chat-row--user" if role == "User" else "chat-row chat-row--assistant"
        bubble_cls = "chat-bubble chat-bubble--user" if role == "User" else "chat-bubble chat-bubble--assistant"

        if pending:
            spinner = Span(cls=self.spinner_class)
            return Div(
                Div(
                    Span(f"{emoji} {role}", cls=("chat-role", TextPresets.muted_sm)),
                    Div(spinner, cls="chat-text"),
                    cls=bubble_cls,
                    style=f"text-align: {align['text']};",
                ),
                cls=row_cls,
                style=f"justify-content: {align['self']};",
            )

        # Add 'marked' class to enable markdown rendering
        return Div(
            Div(
                Span(f"{emoji} {role}", cls=("chat-role", TextPresets.muted_sm)),
                Div(
                    content,
                    cls="marked chat-text",
                    style=f"white-space: {self.content_white_space};",
                ),
                cls=bubble_cls,
                style=f"text-align: {align['text']};",
            ),
            cls=row_cls,
            style=f"justify-content: {align['self']};",
        )

    def __call__(self, data: dict):
        """Make the class callable like the original function."""
        return self.render(data)

# %% ../../nbs/0-Card.ipynb 7
CHAT_DIV_ID = "chat-cards"


def render_chat_list(messages: List[Dict[str, str]], chat_card: ChatCard = None):
    chat_card = chat_card or ChatCard()
    return Div(
        *[chat_card(m) for m in messages],
        id=CHAT_DIV_ID,
        cls="chat-stream",
    )

# %% ../../nbs/0-Card.ipynb 9
def mk_inp():
    return Input(
        id="msg",
        name="msg",
        placeholder="Type a message...",
        autofocus=True,
        cls="uk-input chat-input-field",
    )
