package eu.geyskens.pdfscan.ui.screens

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import eu.geyskens.pdfscan.ScanViewModel
import eu.geyskens.pdfscan.data.Page
import android.graphics.Bitmap

/**
 * Renders [page] to a deskewed, filtered bitmap off the main thread and exposes it as
 * Compose state. Re-renders whenever the page's corners, rotation or filter change.
 */
@Composable
fun rememberRendered(vm: ScanViewModel, page: Page): State<Bitmap?> {
    val state = remember(page.id) { mutableStateOf<Bitmap?>(null) }
    LaunchedEffect(page.id, page.corners, page.rotationDegrees, page.filter) {
        state.value = vm.render(page)
    }
    return state
}
