"""Response builder functions for card stack operations (Tier 1 API)."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/routes/handlers.ipynb.

# %% auto #0
__all__ = ['build_slots_response', 'build_nav_response', 'card_stack_navigate', 'card_stack_navigate_to_index',
           'card_stack_update_viewport', 'card_stack_save_width', 'card_stack_save_scale']

# %% ../../nbs/routes/handlers.ipynb #h1000003
from typing import Any, Callable, List, Optional, Tuple

from ..core.config import CardStackConfig
from ..core.html_ids import CardStackHtmlIds
from ..core.models import CardStackState, CardStackUrls
from ..components.viewport import render_all_slots_oob, render_viewport
from ..components.progress import render_progress_indicator
from ..helpers.focus import render_focus_oob

# %% ../../nbs/routes/handlers.ipynb #h1000005
def build_slots_response(
    card_items: List[Any],  # All data items
    state: CardStackState,  # Current card stack state
    config: CardStackConfig,  # Card stack configuration
    ids: CardStackHtmlIds,  # HTML IDs for this instance
    urls: CardStackUrls,  # URL bundle for navigation
    render_card: Callable,  # Card renderer callback
) -> List[Any]:  # OOB slot elements (3 viewport sections)
    """Build OOB slot updates for the viewport sections only."""
    return render_all_slots_oob(
        card_items=card_items,
        state=state,
        config=config,
        ids=ids,
        urls=urls,
        render_card=render_card,
    )

# %% ../../nbs/routes/handlers.ipynb #h1000006
def build_nav_response(
    card_items: List[Any],  # All data items
    state: CardStackState,  # Current card stack state
    config: CardStackConfig,  # Card stack configuration
    ids: CardStackHtmlIds,  # HTML IDs for this instance
    urls: CardStackUrls,  # URL bundle for navigation
    render_card: Callable,  # Card renderer callback
    progress_label: str = "Item",  # Label for progress indicator
) -> Tuple:  # OOB elements (slots + progress + focus)
    """Build full OOB response for navigation: slots + progress + focus inputs."""
    slots_oob = build_slots_response(
        card_items=card_items, state=state, config=config,
        ids=ids, urls=urls, render_card=render_card,
    )
    progress_oob = render_progress_indicator(
        state.focused_index, len(card_items), ids,
        label=progress_label, oob=True,
    )
    focus_oob = render_focus_oob(state.focused_index, ids)
    return (*slots_oob, progress_oob, *focus_oob)

# %% ../../nbs/routes/handlers.ipynb #h1000008
def card_stack_navigate(
    direction: str,  # "up", "down", "first", "last", "page_up", "page_down"
    card_items: List[Any],  # All data items
    state: CardStackState,  # Current card stack state (mutated in place)
    config: CardStackConfig,  # Card stack configuration
    ids: CardStackHtmlIds,  # HTML IDs for this instance
    urls: CardStackUrls,  # URL bundle for navigation
    render_card: Callable,  # Card renderer callback
    progress_label: str = "Item",  # Label for progress indicator
) -> Tuple:  # OOB elements (slots + progress + focus)
    """Navigate to a different item. Mutates state.focused_index in place."""
    total = len(card_items)
    if total == 0:
        return build_slots_response(
            card_items, state, config, ids, urls, render_card
        )

    # Page jump = visible_count - 1 (one overlap card), minimum 1
    page_jump = max(1, state.visible_count - 1)

    direction_map = {
        "up": max(0, state.focused_index - 1),
        "down": min(total - 1, state.focused_index + 1),
        "first": 0,
        "last": total - 1,
        "page_up": max(0, state.focused_index - page_jump),
        "page_down": min(total - 1, state.focused_index + page_jump),
    }
    state.focused_index = direction_map.get(direction, state.focused_index)

    return build_nav_response(
        card_items, state, config, ids, urls, render_card,
        progress_label=progress_label,
    )

# %% ../../nbs/routes/handlers.ipynb #h1000009
def card_stack_navigate_to_index(
    target_index: int,  # Target item index to navigate to
    card_items: List[Any],  # All data items
    state: CardStackState,  # Current card stack state (mutated in place)
    config: CardStackConfig,  # Card stack configuration
    ids: CardStackHtmlIds,  # HTML IDs for this instance
    urls: CardStackUrls,  # URL bundle for navigation
    render_card: Callable,  # Card renderer callback
    progress_label: str = "Item",  # Label for progress indicator
) -> Tuple:  # OOB elements (slots + progress + focus)
    """Navigate to a specific item index. Mutates state.focused_index in place."""
    total = len(card_items)
    if total == 0:
        return build_slots_response(
            card_items, state, config, ids, urls, render_card
        )

    state.focused_index = max(0, min(total - 1, target_index))

    return build_nav_response(
        card_items, state, config, ids, urls, render_card,
        progress_label=progress_label,
    )

# %% ../../nbs/routes/handlers.ipynb #h1000011
def card_stack_update_viewport(
    visible_count: int,  # New number of visible cards
    card_items: List[Any],  # All data items
    state: CardStackState,  # Current card stack state (mutated in place)
    config: CardStackConfig,  # Card stack configuration
    ids: CardStackHtmlIds,  # HTML IDs for this instance
    urls: CardStackUrls,  # URL bundle for navigation
    render_card: Callable,  # Card renderer callback
) -> Tuple:  # OOB section elements (3 viewport sections)
    """Update viewport with new card count via OOB section swaps. Mutates state.visible_count in place."""
    state.visible_count = visible_count
    return tuple(build_slots_response(
        card_items=card_items,
        state=state,
        config=config,
        ids=ids,
        urls=urls,
        render_card=render_card,
    ))

# %% ../../nbs/routes/handlers.ipynb #h1000013
def card_stack_save_width(
    state: CardStackState,  # Current card stack state (mutated in place)
    card_width: int,  # Card stack width in rem
    config: CardStackConfig,  # Card stack configuration (for clamping bounds)
) -> None:  # No response (swap=none on client)
    """Save card stack width. Mutates state.card_width in place."""
    state.card_width = max(config.card_width_min, min(config.card_width_max, card_width))

# %% ../../nbs/routes/handlers.ipynb #h1000014
def card_stack_save_scale(
    state: CardStackState,  # Current card stack state (mutated in place)
    card_scale: int,  # Card stack scale percentage
    config: CardStackConfig,  # Card stack configuration (for clamping bounds)
) -> None:  # No response (swap=none on client)
    """Save card stack scale. Mutates state.card_scale in place."""
    state.card_scale = max(config.card_scale_min, min(config.card_scale_max, card_scale))
