"""Shared utilities for storage layer."""

from typing import Optional


def resolve_ref(new_value: Optional[str], current_value: Optional[str]) -> Optional[str]:
    """Resolve a reference field update.

    Handles three cases for nullable reference ID fields:
      - new_value == ''   -> clear to None (detach)
      - new_value is None -> keep current value (no change)
      - otherwise         -> use new_value

    Args:
        new_value: The incoming value from the API update request.
        current_value: The current value stored on the entity.

    Returns:
        The resolved value to assign to the field.
    """
    if new_value == "":
        return None
    return new_value if new_value is not None else current_value
