from __future__ import annotations

from typing import Any, Callable, Dict, Optional, cast


def public_job_preflight_payload(
    job: Dict[str, Any],
    *,
    sanitize: Callable[[Any], Any],
) -> Optional[Dict[str, Any]]:
    preflight = job.get("generate_preflight")
    if not isinstance(preflight, dict):
        state = job.get("state")
        state_dict = state if isinstance(state, dict) else {}
        preflight = state_dict.get("generate_preflight")
    if not isinstance(preflight, dict):
        return None
    payload = sanitize(preflight)
    return cast(Dict[str, Any], payload) if isinstance(payload, dict) else None
