# ============================================================
# src/accountable_record/stage_040_elements/transform.py
# ============================================================
"""s040 elements 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_040_elements.validation import validate_elements_stage

STAGE_CODE = "s040"


@stage(STAGE_CODE, Concern.ELEMENTS, "Validate verifiable element declarations.")
def transform_elements(state: PipelineState) -> PipelineState:
    """Validate target repository elements and accumulate the result."""
    result = validate_elements_stage(state)

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

    return state.with_elements(result.elements, result.diagnostics)
