from __future__ import annotations

import json
from pathlib import Path


def load_auth_settings(path: Path) -> dict:
    if not path.exists():
        return {}
    try:
        payload = json.loads(path.read_text(encoding="utf-8"))
        return payload if isinstance(payload, dict) else {}
    except Exception:
        return {}


def save_auth_settings(path: Path, payload: dict) -> dict:
    normalized = payload if isinstance(payload, dict) else {}
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(normalized, ensure_ascii=False, indent=2), encoding="utf-8")
    return normalized
