package com.pixelized.biblib.ui.screen.home.detail

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.pixelized.biblib.utils.extention.capitalize
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class BookOptionViewModel @Inject constructor() : ViewModel() {

    var emails by mutableStateOf(listOf<OptionUio>())
        private set

    var formats by mutableStateOf(initialFormat())
        private set

    fun updateEmails(amazonEmails: List<String>) {
        emails = amazonEmails.mapIndexed { index, mail ->
            OptionUio(value = mail, selected = index == 0)
        }
    }

    fun selectMail(mail: String) {
        emails = emails.map {
            it.copy(selected = it.value == mail)
        }
    }

    fun selectFormat(@Format format: String) {
        formats = formats.map {
            it.copy(selected = it.value == format)
        }
    }

    private fun initialFormat() = listOf(
        OptionUio(Format.EPUB.capitalize(), true),
        OptionUio(Format.MOBI.capitalize()),
    )
}