package com.example.myapplication.database.room.viewmodel.rent

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.myapplication.database.room.model.item.ItemFromRent
import com.example.myapplication.database.room.repository.AppContainer
import com.example.myapplication.database.room.repository.online.RentRepository
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

class RentViewModel(
    savedStateHandle: SavedStateHandle,
    private val rentRepository: RentRepository
) : ViewModel() {
    private val rentUid: Int = checkNotNull(savedStateHandle["id"])

    val rentUiState: StateFlow<RentUiState> = flow{emit(rentRepository.getRent(rentUid))} .map {
        RentUiState(it)
    }.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(stopTimeoutMillis = AppContainer.TIMEOUT),
        initialValue = RentUiState()
    )
}

data class RentUiState(val itemList: List<ItemFromRent> = listOf())