from typing import Callable


def _scroll_screen(direction: int) -> Callable:
    """
    Scroll to the next/prev group of the subset allocated to a specific screen.
    This will rotate between e.g. 1->2->3->1 when the first screen is focussed.
    """

    def _inner(qtile):
        if len(qtile.screens) == 1:
            current = qtile.groups.index(qtile.current_group)
            destination = (current + direction) % 6
            qtile.groups[destination].cmd_toscreen()
            return

        current = qtile.groups.index(qtile.current_group)
        if current < 3:
            destination = (current + direction) % 3
        else:
            destination = ((current - 3 + direction) % 3) + 3
        qtile.groups[destination].cmd_toscreen()

    return _inner