use crate::spectra::cie_y;
use crate::utils::containers::InternCache;
use parking_lot::Mutex;
use shared::Float;
use shared::core::spectrum::Spectrum;
use shared::spectra::DenselySampledSpectrum;
use std::collections::HashMap;
use std::sync::LazyLock;

pub static SPECTRUM_CACHE: LazyLock<InternCache<DenselySampledSpectrum>> =
    LazyLock::new(InternCache::new);

pub static SPECTRUM_FILE_CACHE: LazyLock<Mutex<HashMap<String, Spectrum>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));

pub fn get_spectrum_cache() -> &'static InternCache<DenselySampledSpectrum> {
    &SPECTRUM_CACHE
}

pub fn spectrum_to_photometric(s: Spectrum) -> Float {
    let effective_spectrum = match s {
        Spectrum::RGBIlluminant(ill) => &Spectrum::Dense(ill.illuminant),
        _ => &s,
    };
    effective_spectrum.inner_product(&cie_y())
}
