import omni.ui as ui


collapsed_settings = {}


def prep(frame: ui.CollapsableFrame, type: str):
    global collapsed_settings

    index_name = f"{type}/{frame.title}"
    state = collapsed_settings.get(index_name, None)
    if not state is None:
        frame.collapsed = state
    frame.set_collapsed_changed_fn(lambda c, i=index_name: set_collapsed_state(i, c))


def set_collapsed_state(index_name: str, state: bool):
    global collapsed_settings

    if state == None:
        del collapsed_settings[index_name]
    else:
        collapsed_settings[index_name] = state


def get_collapsed_state(index_name: str=None):
    global collapsed_settings

    if index_name:
        state = collapsed_settings.get(index_name, None)
        return state
    return collapsed_settings


def reset_collapsed_state():
    global collapsed_settings

    collapsed_settings = {}
