use std::cell::RefCell;
use crate::turn_stage::{Stage, StagePipeline};
use super::super::{dom, templates};
thread_local! {
static PIPELINE: RefCell<StagePipeline> = RefCell::new(StagePipeline::new());
static TARGET: RefCell<Option<String>> = const { RefCell::new(None) };
}
pub(crate) fn begin(turn_id: u32) {
PIPELINE.with(|p| *p.borrow_mut() = StagePipeline::new());
let target = format!("stage-{turn_id}");
let slots = PIPELINE.with(|p| p.borrow().slots());
dom::swap_inner(&target, &templates::stage_line(&slots).into_string());
TARGET.with(|t| *t.borrow_mut() = Some(target));
}
pub(crate) fn enter(stage: Stage) {
let Some(target) = TARGET.with(|t| t.borrow().clone()) else {
return;
};
let changed = PIPELINE.with(|p| p.borrow_mut().enter(stage));
if changed {
let slots = PIPELINE.with(|p| p.borrow().slots());
dom::swap_inner(&target, &templates::stage_line(&slots).into_string());
}
}
pub(crate) fn end() {
if let Some(target) = TARGET.with(|t| t.borrow_mut().take()) {
dom::swap_inner(&target, "");
}
PIPELINE.with(|p| *p.borrow_mut() = StagePipeline::new());
}