"""JavaScript generator for dynamic viewport height calculation."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/js/viewport.ipynb.

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

# %% ../../nbs/js/viewport.ipynb #jv000003
from ..core.html_ids import CardStackHtmlIds
from cjm_fasthtml_viewport_fit.models import ViewportFitConfig
from cjm_fasthtml_viewport_fit.js import (
    generate_debug_helpers_js,
    generate_space_below_js,
    generate_calculate_height_js,
    generate_resize_handler_js,
    generate_sibling_observer_js,
    generate_init_js,
)

# %% ../../nbs/js/viewport.ipynb #jv000005
def generate_viewport_height_js(
    ids: CardStackHtmlIds,  # HTML IDs for this card stack instance
    container_id: str = "",  # Unused — kept for API compatibility
) -> str:  # JavaScript code fragment for viewport height calculation
    """Generate JS for dynamic viewport height calculation via cjm-fasthtml-viewport-fit.

    Delegates to the viewport-fit library's individual generator functions.
    The card stack coordinator handles HTMX settle events separately.
    """
    config = ViewportFitConfig(
        namespace=ids.prefix,
        target_id=ids.card_stack,
        inner_id=ids.card_stack_inner,
        min_height=200,
        debounce_ms=100,
        scroll_to_top=True,
        enable_htmx_settle=False,
        observe_siblings=True,
        resize_callback="if (ns.triggerAutoAdjust) ns.triggerAutoAdjust();",
    )

    parts = [
        "// === Viewport Height Calculation ===",
        generate_debug_helpers_js(config),
        generate_space_below_js(config),
        generate_calculate_height_js(config),
        generate_resize_handler_js(config),
        generate_sibling_observer_js(config),
        "ns.recalculateHeight = _calculateAndSetHeight;",
        "ns._setupSiblingObserver = _setupSiblingObserver;",
        generate_init_js(config),
    ]

    return "\n".join(parts)
