"""JSON -> PDF (pretty-printed, monospace). Validation is explicit (a JSON error is a *job* error with a clear message, not a render failure): whitespace-only input -> "empty JSON file: ...", unparseable input -> "Invalid JSON: ". Valid documents are pretty-printed (indent=2, original key order) into
 with PRE_CSS so
structure and indentation survive the render.
"""
from __future__ import annotations

import json
from pathlib import Path

from . import common
from .text_converter import decode_bytes


def convert(path: Path, job_id: str, progress_cb=None, output_dir=None,
            source_name=None) -> str:
    """Render a JSON file to a pretty-printed monospace PDF."""
    raw = path.read_bytes()
    if not raw.strip():
        raise ValueError("empty JSON file: nothing to convert")
    text = decode_bytes(raw)
    try:
        obj = json.loads(text)
    except json.JSONDecodeError as exc:
        raise ValueError(f"Invalid JSON: {exc}") from exc
    pretty = json.dumps(obj, indent=2, ensure_ascii=False)

    def _build():
        return f"
{common.escape(pretty)}
", path.name return common.convert_single( path, job_id, build_html=_build, css=common.PRE_CSS, progress_cb=progress_cb, output_dir=output_dir, name_src=source_name or path.name, )