KWin: Switch to window left/right/above/below -- how does it work?

I’m trying to figure out the logic of KWin’s “switch to window to the X” commands, but it’s quite perplexing. (X is Right, Left, Above or Below)

When all windows are non-overlapping, it works how you’d expect, and you can switch to windows by direction like in a tiling window manager.

When there are multiple maximized windows and nothing else, switch-up and switch-down work as expected, cycling through them.

When you have multiple maximized windows and other regular windows, or windows of different sizes with overlaps across monitors (i.e. the most common cases), the behavior of “switch to window to the X” is completely unpredictable, and many windows are inaccessible through these commands.

Does anyone know how Kwin decides which window is above/below or to the left/right of another when windows overlap? It doesn’t seem to be simple, such as by the pixel value of the left-most, right-most, top or bottom corners.