package com.fabledsword.minstrel.shared.widgets

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.composables.icons.lucide.CircleCheckBig
import com.composables.icons.lucide.Lucide
import com.fabledsword.minstrel.theme.LocalFabledSwordTheme

/**
 * Track ids with audio cached locally, provided once at the app root
 * from `CachedTrackIds`. Defaults to empty so any unwrapped preview /
 * test simply shows no dots rather than crashing.
 */
val LocalCachedTrackIds = staticCompositionLocalOf { emptySet<String>() }

/**
 * Small accent download glyph shown next to a track row when the track
 * is cached locally. Ports Flutter's `CachedIndicator`
 * (`circle_check_big`, size 14, accent, 4px left pad) — renders nothing
 * when the track isn't cached. Reads the reactive set from
 * [LocalCachedTrackIds], so the dot appears/disappears as the cache
 * fills and evicts without per-row queries.
 */
@Composable
fun CachedDot(trackId: String, modifier: Modifier = Modifier) {
    if (trackId !in LocalCachedTrackIds.current) return
    Icon(
        imageVector = Lucide.CircleCheckBig,
        contentDescription = "Downloaded",
        tint = LocalFabledSwordTheme.current.accent,
        modifier = modifier
            .padding(start = 4.dp)
            .size(14.dp),
    )
}
