def get_carousel_state()

in mm_action_prediction/tools/extract_actions.py [0:0]


def get_carousel_state(state=None, action_args=None):
    """Obtain carousel state from dictionary of States.

    Args:
        state: The original state to convert into new format.
        action_args: Action arguments.

    Returns:
        new_state: Return state in a new format.
    """
    new_state = {"focus": None, "carousel": []}
    if state is None:
        return new_state

    # Check if the state is empty. If yes, return None.
    empty_lists = [
        "prefabsInCarousel",
        "sharedPrefabsInCarousel",
        "textPrefabsInCarousel",
    ]
    empty_strs = ["prefabInFocus", "sharedPrefabInFocus", "textPrefabInFocus"]
    empty_state = all(state[ii] == "" for ii in empty_strs)
    if empty_state:
        for key in empty_lists:
            if len(state[key]):
                empty_state = False
                break
    if empty_state:
        return new_state

    # Get items in the carousel.
    search_order_carousel = ["prefabsInCarousel", "sharedPrefabsInCarousel"]
    search_order_focus = ["prefabInFocus", "sharedPrefabInFocus"]
    action = action_args["api"]
    if action in [
        SEARCH_FURNITURE_ACTION,
        NAVIGATE_CAROUSEL_ACTION
    ]:
        for order in search_order_carousel:
            if state[order]:
                new_state["carousel"] = state[order]
                # break loop and return - don't return focus item
                # SearchFurniture and NavigateCarousel actions
                return new_state
    elif action in [ROTATE_ACTION, FOCUS_ON_FURNITURE_ACTION]:
        focus_id = action_args["args"]["furniture_id"]
        for order in search_order_carousel:
            if focus_id in state[order]:
                new_state["carousel"] = state[order]

    # Focus object.
    for key in search_order_focus:
        if state[key] != "":
            new_state["focus"] = state[key]
            break
    return new_state