//! Shared HDR render-to-texture helpers for editor viewport panels.

use bevy::prelude::*;
use bevy::render::render_resource::TextureFormat;

/// Offscreen render target displayed by the primary editor viewport.
#[derive(Debug, Clone)]
pub struct ViewportTarget {
    pub image: Handle<Image>,
}

/// Current primary viewport render target, if the panel has a valid size.
#[derive(Resource, Default, Debug, Clone)]
pub struct ViewportRenderTarget(pub Option<ViewportTarget>);

/// Allocates or reuses an HDR offscreen color target when the panel size changes.
pub fn ensure_hdr_panel_target(
    physical_size: UVec2,
    last_size: &mut Option<UVec2>,
    images: &mut Assets<Image>,
) -> Option<Handle<Image>> {
    if *last_size == Some(physical_size) {
        return None;
    }
    *last_size = Some(physical_size);
    Some(images.add(Image::new_target_texture(
        physical_size.x,
        physical_size.y,
        TextureFormat::Rgba16Float,
        None,
    )))
}

/// Standard HDR format for editor panel render targets.
pub const PANEL_TARGET_FORMAT: TextureFormat = TextureFormat::Rgba16Float;
