"""
HTTP utility helpers shared across apps.

Extracted from browse/services/services.py so order/orders blueprints
(which live in the cart app) don't need to import from the browse blueprint.
"""
from __future__ import annotations

from urllib.parse import urlencode

from quart import g, request
from shared.utils import host_url


def vary(resp):
    """
    Ensure SX-Request/HX-Request and X-Origin are part of the Vary header
    so caches distinguish fragment from full-page requests.
    """
    v = resp.headers.get("Vary", "")
    parts = [p.strip() for p in v.split(",") if p.strip()]
    for h in ("SX-Request", "HX-Request", "X-Origin"):
        if h not in parts:
            parts.append(h)
    if parts:
        resp.headers["Vary"] = ", ".join(parts)
    return resp


def current_url_without_page():
    """
    Return the current URL with the ``page`` query-string parameter removed.
    Used for Hx-Push-Url headers on paginated routes.
    """
    (request.script_root or "").rstrip("/")
    root2 = "/" + g.root
    path_only = request.path

    if root2 and path_only.startswith(root2):
        rel = path_only[len(root2):]
        rel = rel if rel.startswith("/") else "/" + rel
    else:
        rel = path_only
    base = host_url(rel)

    params = request.args.to_dict(flat=False)
    params.pop("page", None)
    qs = urlencode(params, doseq=True)
    return f"{base}?{qs}" if qs else base
