//! Shared canvas utilities for avatar and prop rendering.
//!
//! Common functions used by both AvatarCanvas and LoosePropCanvas components.

/// Normalize an asset path to be absolute, prefixing with /static/ if needed.
#[cfg(feature = "hydrate")]
pub fn normalize_asset_path(path: &str) -> String {
    if path.starts_with('/') {
        path.to_string()
    } else {
        format!("/static/{}", path)
    }
}

/// Test if a click at the given client coordinates hits a non-transparent pixel.
///
/// Returns true if the alpha channel at the clicked pixel is > 0.
/// This enables pixel-perfect hit detection on canvas elements.
#[cfg(feature = "hydrate")]
pub fn hit_test_canvas(
    canvas: &web_sys::HtmlCanvasElement,
    client_x: f64,
    client_y: f64,
) -> bool {
    use wasm_bindgen::JsCast;

    // Get the canvas bounding rect to transform client coords to canvas coords
    let rect = canvas.get_bounding_client_rect();

    // Calculate click position relative to the canvas element
    let relative_x = client_x - rect.left();
    let relative_y = client_y - rect.top();

    // Check if click is within canvas bounds
    if relative_x < 0.0
        || relative_y < 0.0
        || relative_x >= rect.width()
        || relative_y >= rect.height()
    {
        return false;
    }

    // Transform to canvas pixel coordinates (accounting for CSS scaling)
    let canvas_width = canvas.width() as f64;
    let canvas_height = canvas.height() as f64;

    // Avoid division by zero
    if rect.width() == 0.0 || rect.height() == 0.0 {
        return false;
    }

    let scale_x = canvas_width / rect.width();
    let scale_y = canvas_height / rect.height();

    let pixel_x = relative_x * scale_x;
    let pixel_y = relative_y * scale_y;

    // Get the 2D context and read the pixel data
    if let Ok(Some(ctx)) = canvas.get_context("2d") {
        let ctx: web_sys::CanvasRenderingContext2d = ctx.dyn_into().unwrap();

        match ctx.get_image_data(pixel_x, pixel_y, 1.0, 1.0) {
            Ok(image_data) => {
                let data = image_data.data();
                // Alpha channel is the 4th value (index 3)
                if data.len() >= 4 {
                    return data[3] > 0;
                }
            }
            Err(_) => {
                return false;
            }
        }
    }

    false
}
