from __future__ import annotations

from typing import Any


def get_artifact_from_metadata(
    *, artifacts_meta: dict[str, Any] | None, artifacts_id: str, artifact_index: int
) -> dict[str, Any] | None:
    if not isinstance(artifacts_meta, dict) or artifact_index < 0:
        return None

    artifacts_group = artifacts_meta.get(artifacts_id)
    if not isinstance(artifacts_group, dict):
        return None

    artifacts = artifacts_group.get("artifacts") or []
    if not isinstance(artifacts, list):
        return None

    if artifact_index >= len(artifacts):
        return None
    artifact = artifacts[artifact_index]
    return artifact if isinstance(artifact, dict) else None
