"""Custom scrollbar component with proportional thumb for position indication."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/components/scrollbar.ipynb.

# %% auto #0
__all__ = ['render_scrollbar_thumb', 'render_scrollbar']

# %% ../../nbs/components/scrollbar.ipynb #4f4abe73
from fasthtml.common import Div

from ..core.models import VirtualCollectionConfig, VirtualCollectionState
from ..core.html_ids import VirtualCollectionHtmlIds

from cjm_fasthtml_virtual_scrollbar.core.models import ScrollbarConfig, ScrollbarState, ScrollbarIds
from cjm_fasthtml_virtual_scrollbar.components.scrollbar import (
    render_scrollbar as _sb_render_scrollbar,
    render_scrollbar_thumb as _sb_render_scrollbar_thumb,
)

# %% ../../nbs/components/scrollbar.ipynb #c3d7bdfa
def _map_to_scrollbar(
    state: VirtualCollectionState,
    config: VirtualCollectionConfig,
    ids: VirtualCollectionHtmlIds,
):  # (ScrollbarState, ScrollbarConfig, ScrollbarIds)
    """Map VC types to scrollbar lib types.

    Uses cursor-based model (like card-stack): scrollbar position tracks
    the focused row, not the viewport window offset. This ensures dragging
    the thumb to the very top/bottom always reaches the first/last item.
    Always visible (auto_hide=False) since the scrollbar serves as a
    position indicator, not just an overflow indicator.
    """
    total = state.total_items
    sb_state = ScrollbarState(
        position=state.cursor_index,
        visible_count=state.visible_rows,
        total_items=total,
        max_position=max(0, total - 1),
        thumb_ratio=1 / total if total > 0 else None,
    )
    sb_config = ScrollbarConfig(
        prefix=config.prefix,
        show_scrollbar=config.show_scrollbar,
        min_thumb_height=config.min_thumb_height,
        auto_hide=False,
    )
    sb_ids = ScrollbarIds(prefix=ids.prefix)
    return sb_state, sb_config, sb_ids


def render_scrollbar_thumb(
    state: VirtualCollectionState,       # Collection state
    config: VirtualCollectionConfig,      # Collection config
    ids: VirtualCollectionHtmlIds,        # HTML IDs
    track_height: float = 600.0,          # Track height for min thumb calculation
    oob: bool = False,                    # Whether to include hx-swap-oob
) -> Div:  # Thumb element
    """Render the scrollbar thumb at the correct position."""
    sb_state, sb_config, sb_ids = _map_to_scrollbar(state, config, ids)
    return _sb_render_scrollbar_thumb(sb_state, sb_config, sb_ids, track_height, oob)

# %% ../../nbs/components/scrollbar.ipynb #c126a242
def render_scrollbar(
    state: VirtualCollectionState,       # Collection state
    config: VirtualCollectionConfig,      # Collection config
    ids: VirtualCollectionHtmlIds,        # HTML IDs
) -> Div:  # Complete scrollbar (track + thumb)
    """Render the custom scrollbar with track and proportional thumb."""
    sb_state, sb_config, sb_ids = _map_to_scrollbar(state, config, ids)
    return _sb_render_scrollbar(sb_state, sb_config, sb_ids)
