import json
from pathlib import Path
from typing import Any

import yaml


def load_openapi_spec(path: Path) -> dict[str, Any]:
    suffix = path.suffix.lower()
    text = path.read_text(encoding="utf-8")

    if suffix in {".yaml", ".yml"}:
        data = yaml.safe_load(text)
    elif suffix == ".json":
        data = json.loads(text)
    else:
        raise ValueError(f"不支持的 OpenAPI 文件后缀：{path.suffix}（仅支持 .yaml/.yml/.json）")

    if not isinstance(data, dict):
        raise ValueError("OpenAPI 文件解析结果不是对象。")
    return data
