package de.tsschulz.tt_tagebuch.app.ui

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import coil.compose.AsyncImage
import coil.request.ImageRequest
import okhttp3.Headers

@Composable
fun AuthenticatedAsyncImage(
    imageUrl: String,
    authHeaders: Map<String, String>,
    modifier: Modifier = Modifier,
    contentDescription: String? = null,
    contentScale: ContentScale = ContentScale.Fit,
) {
    val context = LocalContext.current
    val request = remember(imageUrl, authHeaders) {
        val hb = Headers.Builder()
        authHeaders.forEach { (k, v) -> hb.add(k, v) }
        ImageRequest.Builder(context)
            .data(imageUrl)
            .headers(hb.build())
            .crossfade(true)
            .build()
    }
    AsyncImage(
        model = request,
        contentDescription = contentDescription,
        modifier = modifier,
        contentScale = contentScale,
    )
}
