"""Package resource helpers for UI presentation assets."""

from __future__ import annotations

from functools import cache
from importlib.resources import files
from pathlib import PurePosixPath


@cache
def read_css(relative_path: str) -> str:
    """Read and cache a CSS resource relative to ``ui/static``."""
    resource_path = PurePosixPath(relative_path)
    if resource_path.is_absolute() or ".." in resource_path.parts or resource_path.suffix != ".css":
        msg = f"Invalid CSS resource path: {relative_path}"
        raise ValueError(msg)

    resource = files("transcription.ui").joinpath("static", *resource_path.parts)
    return resource.read_text(encoding="utf-8")
