//! JavaScript Assets Module
//!
//! This module provides access to JavaScript assets generated by the SDK.
//! All JavaScript code is managed in `packages/auroraview-sdk` and built
//! into this crate's assets directory.

use rust_embed::Embed;

/// Embedded JavaScript assets for Chrome Extension polyfill
#[derive(Embed)]
#[folder = "src/assets/js"]
#[include = "*.js"]
pub struct JsAssets;

/// Get the Chrome Extension polyfill script
///
/// This script provides the `chrome.*` API polyfill for running
/// Chrome extensions inside AuroraView WebView.
pub fn chrome_polyfill() -> &'static str {
    static POLYFILL: std::sync::OnceLock<String> = std::sync::OnceLock::new();
    POLYFILL.get_or_init(|| {
        JsAssets::get("chrome_polyfill.js")
            .map(|f| String::from_utf8_lossy(&f.data).into_owned())
            .unwrap_or_else(|| {
                tracing::error!("Failed to load chrome_polyfill.js");
                String::new()
            })
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_chrome_polyfill_loads() {
        let polyfill = chrome_polyfill();
        assert!(!polyfill.is_empty(), "Chrome polyfill should not be empty");
        assert!(
            polyfill.contains("chrome"),
            "Chrome polyfill should contain 'chrome'"
        );
    }
}
