use crate::ecs::world::World;

/// Per-frame egui host state, present only with the `egui` feature.
///
/// `enabled` gates whether the egui pass is opened each frame and whether the
/// [`EguiPass`](crate::render::wgpu::passes::EguiPass) draws. `state` owns the
/// `egui::Context` through `egui_winit::State`. `frame_active` is true between
/// the begin and end of the pass, which is when [`egui_context`] hands out the
/// context. `frame_output` carries the tessellated primitives into the renderer.
#[derive(Default)]
pub struct EguiState {
    pub enabled: bool,
    pub frame_active: bool,
    pub state: Option<egui_winit::State>,
    pub frame_output: Option<(egui::FullOutput, Vec<egui::ClippedPrimitive>)>,
    #[cfg(target_arch = "wasm32")]
    pub wasm_paste_queue: std::sync::Arc<std::sync::Mutex<Vec<String>>>,
}

/// Returns the egui context for this frame, or `None` when egui is disabled or
/// the frame's pass is not open. Draw with it from any per-frame system: the
/// returned context is a cheap handle to the active pass, so issue
/// `egui::Window`, panel, and widget calls against it. The tessellated output
/// composites over the scene and the retained UI.
pub fn egui_context(world: &World) -> Option<egui::Context> {
    if !world.resources.egui.frame_active {
        return None;
    }
    world
        .resources
        .egui
        .state
        .as_ref()
        .map(|state| state.egui_ctx().clone())
}
