# ============================================================
# src/accountable_record/stage_060_exports/transform.py
# ============================================================
"""s060 exports 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_060_exports.validation import validate_exports_stage

STAGE_CODE = "s060"


@stage(STAGE_CODE, Concern.EXPORTS, "Write and validate machine-readable exports.")
def transform_exports(state: PipelineState) -> PipelineState:
    """Write/validate exports and accumulate the result."""
    result = validate_exports_stage(state)

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

    return state.with_exports(result.exports, result.diagnostics)
