package com.jsjdesigns.poker

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.update

/**
 * A hand-numbered continuation gate.
 *
 * Remembering the greatest requested hand makes repeated taps idempotent: two
 * taps on hand 4 can release hand 4 only, never a future hand 5.
 */
internal class NextHandGate {
    private val requestedThrough = MutableStateFlow(0)

    fun request(completedHand: Int) {
        requestedThrough.update { maxOf(it, completedHand) }
    }

    suspend fun awaitRequest(completedHand: Int) {
        requestedThrough.first { it >= completedHand }
    }
}
