def METHOD_NAME(self, instance: StatusMessage, categories_data: dict) -> StatusMessage:
    """
    Update the categories of the status message.
    """
    if categories_data is not None:
        current_categories = instance.categories.all()
        current_category_ids = set(category.id for category in current_categories)
        new_category_ids = set(category.id for category in categories_data)
        categories_to_add = Category.objects.filter(id__in=new_category_ids - current_category_ids)
        categories_to_remove = current_categories.filter(id__in=current_category_ids - new_category_ids)
        for category in categories_to_add:
            StatusMessageCategory.objects.create(status_message=instance, category=category, position=0)
        for category in categories_to_remove:
            instance.categories.remove(category)
    return instance