from __future__ import annotations from app.domain.editor import RuntimeOperation, VariantChange, VariantRuntimePayload def build_runtime_payload_from_changes( variant_id: str, changes: list[VariantChange], ) -> VariantRuntimePayload: """Convert editor-facing change records into runtime-facing operations. The builder keeps the runtime contract stable even if the editor UI later changes how it stores intermediate draft state. """ operations: list[RuntimeOperation] = [] for change in sorted(changes, key=lambda item: item.sort_order): action = change.payload.get("action") if isinstance(change.payload, dict) else None operations.append( RuntimeOperation( selector_type=change.selector_type, selector_value=change.selector_value, action=action or change.change_type, payload=change.payload, ) ) return VariantRuntimePayload( variant_id=variant_id, operations=operations, )