package com.rosetta.messenger.session

object SessionReducer {
    fun reduce(current: SessionState, action: SessionAction): SessionState {
        return when (action) {
            is SessionAction.LoggedOut -> SessionState.LoggedOut
            is SessionAction.AuthInProgress ->
                SessionState.AuthInProgress(
                    publicKey = action.publicKey?.trim().orEmpty().ifBlank { null },
                    reason = action.reason
                )
            is SessionAction.Ready ->
                SessionState.Ready(
                    account = action.account,
                    reason = action.reason
                )
            is SessionAction.SyncFromCachedAccount -> {
                val account = action.account
                if (account == null) {
                    if (current is SessionState.Ready) SessionState.LoggedOut else current
                } else {
                    SessionState.Ready(account = account, reason = "cached")
                }
            }
        }
    }
}
