"""Lightweight HTTP helper utilities shared across infra_core."""

from __future__ import annotations

from urllib.parse import urlsplit, urlunsplit


def absolute_url(base: str, *segments: str) -> str:
    """Return an absolute URL by joining the base with one or more path segments."""

    trimmed_segments = [(segment or "").strip("/") for segment in segments]
    if not any(trimmed_segments):
        return (base or "").rstrip("/") or "/"

    parts = urlsplit(base or "")
    url = parts.path.rstrip("/")
    for trimmed in trimmed_segments:
        if not trimmed:
            continue
        url = f"{url}/{trimmed}" if url else f"/{trimmed}"
    return urlunsplit((parts.scheme, parts.netloc, url or "/", parts.query, parts.fragment))


__all__ = ["absolute_url"]
