"""
Backward-compatible VIS helpers built on top of the unified backend module.
"""

from .backend import ensure_vis_server, is_vis_running, render_png, render_png_bytes, send_phg


def HTTPPOST(ip, phg):
    if ip not in ("127.0.0.1", "localhost"):
        raise ValueError("Only local VIS server is supported.")
    send_phg(phg.decode("utf-8") if isinstance(phg, bytes) else str(phg))


def vis(phg):
    ensure_vis_server()
    send_phg(phg)


def HTTPPOST_IMG(ip, phg, filename="shot.png"):
    if ip not in ("127.0.0.1", "localhost"):
        raise ValueError("Only local VIS server is supported.")
    render_png(phg.decode("utf-8") if isinstance(phg, bytes) else str(phg), filename=filename)


def image(
    phg,
    filename="shot.png",
    width=800,
    height=600,
    view=3,
    timeout=30.0,
    **camera_kwargs,
):
    return render_png(
        phg,
        filename=filename,
        width=width,
        height=height,
        view=view,
        timeout=timeout,
        **camera_kwargs,
    )


__all__ = [
    "ensure_vis_server",
    "is_vis_running",
    "render_png",
    "render_png_bytes",
    "send_phg",
    "HTTPPOST",
    "HTTPPOST_IMG",
    "vis",
    "image",
]
