"""
Base template context shared by all apps.

This module no longer imports cart or menu_items services directly.
Each app provides its own context_fn that calls this base and adds
app-specific variables (cart data, menu_items, etc.).
"""
from __future__ import annotations

from datetime import datetime

from quart import request, g, current_app

from shared.config import config
from shared.utils import host_url
from shared.browser.app.utils import current_route_relative_path


async def base_context() -> dict:
    """
    Common template variables available in every app.

    Does NOT include cart, calendar_cart_entries, total, calendar_total,
    or menu_items — those are added by each app's context_fn.
    """
    is_htmx = request.headers.get("HX-Request") == "true"
    search = request.headers.get("X-Search", "")
    zap_filter = is_htmx and search == ""

    def base_url():
        return host_url()

    hx_select = "#main-panel"
    hx_select_search = (
        hx_select
        + ", #search-mobile, #search-count-mobile, #search-desktop, #search-count-desktop, #menu-items-nav-wrapper"
    )

    return {
        "is_htmx": is_htmx,
        "request": request,
        "now": datetime.now(),
        "current_local_href": current_route_relative_path(),
        "config": config(),
        "asset_url": current_app.jinja_env.globals.get("asset_url", lambda p: ""),
        "sort_options": [
            ("az", "A\u2013Z", "order/a-z.svg"),
            ("za", "Z\u2013A", "order/z-a.svg"),
            ("price-asc", "\u00a3 low\u2192high", "order/l-h.svg"),
            ("price-desc", "\u00a3 high\u2192low", "order/h-l.svg"),
        ],
        "zap_filter": zap_filter,
        "print": print,
        "base_url": base_url,
        "base_title": config()["title"],
        "hx_select": hx_select,
        "hx_select_search": hx_select_search,
    }
