# ============================================================
# src/accountable_record/stage_030_sources/transform.py
# ============================================================
"""s030 sources stage transform."""

from accountable_record.pipeline.context import PipelineState
from accountable_record.pipeline.diagnostics import Concern
from accountable_record.pipeline.stages import stage
from accountable_record.stage_030_sources.validation import validate_sources_stage

STAGE_CODE = "s030"


@stage(STAGE_CODE, Concern.SOURCES, "Validate repository source materials.")
def transform_sources(state: PipelineState) -> PipelineState:
    """Validate target repository sources and accumulate the result."""
    result = validate_sources_stage(state)

    if result.has_errors or result.sources is None:
        return state.with_failure(STAGE_CODE, result.diagnostics)

    return state.with_sources(result.sources, result.diagnostics)
