package com.pixelized.desktop.lwa.ui.composable.textfield

import androidx.compose.runtime.Stable
import kotlinx.coroutines.flow.MutableStateFlow

@Stable
data class LwaTextFieldFlow(
    val errorFlow: MutableStateFlow<Boolean>,
    val labelFlow: MutableStateFlow<String?>,
    val valueFlow: MutableStateFlow<String>,
)

fun createLwaTextFieldFlow(
    error: Boolean = false,
    label: String?,
    value: String = "",
): LwaTextFieldFlow {
    return createLwaTextFieldFlow(
        errorFlow = MutableStateFlow(error),
        valueFlow = MutableStateFlow(value),
        labelFlow = MutableStateFlow(label),
    )
}

fun createLwaTextFieldFlow(
    errorFlow: MutableStateFlow<Boolean> = MutableStateFlow(false),
    labelFlow: MutableStateFlow<String?>,
    valueFlow: MutableStateFlow<String>,
): LwaTextFieldFlow {
    return LwaTextFieldFlow(
        errorFlow = errorFlow,
        valueFlow = valueFlow,
        labelFlow = labelFlow,
    )
}

fun LwaTextFieldFlow.createLwaTextField(
    enable: Boolean = true,
    placeHolder: String? = null,
    checkForError: ((String) -> Boolean)? = null,
    onValueChange: (String) -> Unit = {
        errorFlow.value = checkForError?.invoke(it) ?: errorFlow.value
        valueFlow.value = it
    },
): LwaTextFieldUio {
    return LwaTextFieldUio(
        enable = enable,
        errorFlow = errorFlow,
        valueFlow = valueFlow,
        labelFlow = labelFlow,
        placeHolder = placeHolder,
        onValueChange = onValueChange,
    )
}