# src/accountable_record/pipeline/diagnostic_mapping.py

"""Helpers for mapping typed stage errors to diagnostics."""

from accountable_record.errors import StageError
from accountable_record.pipeline.diagnostics import (
    Diagnostic,
    DiagnosticSpec,
    from_spec_at,
)


def diagnostic_for_stage_error(
    error: StageError,
    mapping: tuple[tuple[type[StageError], DiagnosticSpec], ...],
    *,
    fallback: DiagnosticSpec,
) -> Diagnostic:
    """Map a typed StageError to a Diagnostic using stage-owned specs."""
    for error_type, spec in mapping:
        if isinstance(error, error_type):
            return from_spec_at(
                spec,
                error.location,
                detail=str(error),
            )

    return from_spec_at(
        fallback,
        error.location,
        detail=str(error),
    )
