"""Transport URI parsing for the Docstream CLI (spec §6.5)."""
from __future__ import annotations


def parse_source_uri(uri: str) -> tuple[str, str]:
    """Parse a source URI into ``(transport, location)``.

    Mapping per §6.5:

    - No prefix, ``local://``, or ``file://`` → ``transport="local"``;
      ``location`` is the filesystem path (the scheme is stripped if present).
    - ``ssh://host/path`` → ``transport="ssh"``; ``location="host:/path"``.
    - ``wg://host/path`` → ``transport="wireguard"``; ``location="host:/path"``.

    For remote transports the scheme is stripped and the remainder is
    converted to ``host:path`` form.
    """
    if uri.startswith("ssh://"):
        remainder = uri[len("ssh://"):]
        return "ssh", _to_host_colon_path(remainder)
    if uri.startswith("wg://"):
        remainder = uri[len("wg://"):]
        return "wireguard", _to_host_colon_path(remainder)
    if uri.startswith("local://"):
        return "local", uri[len("local://"):]
    if uri.startswith("file://"):
        return "local", uri[len("file://"):]
    return "local", uri


def _to_host_colon_path(remainder: str) -> str:
    """Convert ``host/path/to/docs`` → ``host:/path/to/docs``."""
    if "/" not in remainder:
        # No path component — treat the whole string as the host with empty path.
        return f"{remainder}:"
    host, path = remainder.split("/", 1)
    return f"{host}:/{path}"
