"""Helpers for reading packaged OpenAPI resources."""

from __future__ import annotations

from importlib.resources import files
from typing import Any

import yaml


def get_openapi_text() -> str:
    return files("cora_contract").joinpath("openapi/openapi.yaml").read_text(encoding="utf-8")


def get_openapi_document() -> dict[str, Any]:
    payload = yaml.safe_load(get_openapi_text())
    if not isinstance(payload, dict):
        raise TypeError("Packaged OpenAPI document must be a mapping.")
    return payload

