package com.ponzischeme89.memby.ui

internal enum class UserSwitcherDirection { UP, DOWN }

/**
 * Profiles occupy [0, profileCount); the pinned actions — My Alerts, then Manage users —
 * follow them in order. Keeping this arithmetic outside Compose makes remote navigation
 * deterministic.
 */
internal fun userSwitcherInitialIndex(
    profileIds: List<String>,
    activeProfileId: String?,
): Int = profileIds.indexOf(activeProfileId).takeIf { it >= 0 } ?: 0

internal fun userSwitcherNextIndex(
    currentIndex: Int,
    profileCount: Int,
    direction: UserSwitcherDirection,
    actionCount: Int = 1,
): Int {
    val lastIndex = (profileCount.coerceAtLeast(0) + actionCount.coerceAtLeast(1) - 1)
        .coerceAtLeast(0)
    val current = currentIndex.coerceIn(0, lastIndex)
    return when (direction) {
        UserSwitcherDirection.UP -> (current - 1).coerceAtLeast(0)
        UserSwitcherDirection.DOWN -> (current + 1).coerceAtMost(lastIndex)
    }
}
