# ============================================================
# src/accountable_record/stage_050_catalog/transform.py
# ============================================================
"""s050 catalog 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_050_catalog.validation import validate_catalog_stage

STAGE_CODE = "s050"


@stage(STAGE_CODE, Concern.CATALOG, "Build and validate the normalized catalog.")
def transform_catalog(state: PipelineState) -> PipelineState:
    """Build/validate catalog and accumulate the result."""
    result = validate_catalog_stage(state)

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

    return state.with_catalog(result.catalog, result.diagnostics)
