"""Tab bar rendering for the home page header."""

from __future__ import annotations

from pyratatui import Constraint, Direction, Frame, Layout, Paragraph, Rect, Tabs

from pygittools.tui.git_colors import active_tab_style, current_theme, focus_style


def init_tab_colors() -> None:
    """Refresh cached tab attributes from the active Git color theme."""
    return None


def render_tab_row(
    frame: Frame,
    area: Rect,
    prefix: str,
    tab_labels: tuple[str, ...],
    active_index: int,
    *,
    prefix_focused: bool = False,
) -> None:
    """Draw project prefix and selectable tabs into ``area``."""
    if area.height <= 0 or area.width <= 0:
        return
    theme = current_theme()
    prefix_style = focus_style(theme.branch) if prefix_focused else theme.branch
    prefix_width = min(area.width, len(prefix) + 2)
    chunks = (
        Layout()
        .direction(Direction.Horizontal)
        .constraints([Constraint.length(prefix_width), Constraint.fill(1)])
        .split(area)
    )
    frame.render_widget(Paragraph.from_string(prefix).style(prefix_style), chunks[0])
    frame.render_widget(
        Tabs(list(tab_labels))
        .select(active_index)
        .highlight_style(active_tab_style())
        .style(theme.tab_inactive)
        .divider("  "),
        chunks[1],
    )