package dev.ulfrx.recipe.ui.components.glass

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import io.github.fletchmckee.liquid.LiquidState
import io.github.fletchmckee.liquid.liquefiable
import io.github.fletchmckee.liquid.rememberLiquidState

val LocalGlassBackdropState =
    staticCompositionLocalOf<GlassBackdropState> {
        error("LocalGlassBackdropState not provided — wrap in GlassBackdropSource")
    }

@Stable
class GlassBackdropState internal constructor(
    internal val liquidState: LiquidState,
)

@Composable
fun rememberGlassBackdropState(): GlassBackdropState {
    val liquidState = rememberLiquidState()
    return remember(liquidState) {
        GlassBackdropState(liquidState)
    }
}

@Composable
fun GlassBackdropSource(
    state: GlassBackdropState,
    modifier: Modifier = Modifier,
    content: @Composable BoxScope.() -> Unit,
) {
    Box(
        modifier = modifier
            .liquefiable(state.liquidState),
        content = content,
    )
}
