use std::cell::{Cell, RefCell};
use crate::turn_stage::{Stage, StagePipeline};
use super::super::{dom, templates};
const SLOT: &str = "turn-status";
thread_local! {
static PIPELINE: RefCell<StagePipeline> = RefCell::new(StagePipeline::new());
static ARMED: Cell<bool> = const { Cell::new(false) };
static BODY_ID: RefCell<String> = const { RefCell::new(String::new()) };
}
pub(crate) fn begin(body_id: &str) {
PIPELINE.with(|p| *p.borrow_mut() = StagePipeline::new());
BODY_ID.with(|b| *b.borrow_mut() = body_id.to_string());
ARMED.with(|a| a.set(true));
dom::swap_inner(SLOT, "");
}
pub(crate) fn enter(stage: Stage) {
if !ARMED.with(Cell::get) {
return;
}
let changed = PIPELINE.with(|p| p.borrow_mut().enter(stage));
if changed {
dom::swap_inner(SLOT, &templates::stage_status_button(stage).into_string());
BODY_ID.with(|b| dom::set_attr(&b.borrow(), "data-stage", stage.word()));
}
}
pub(crate) fn end() {
ARMED.with(|a| a.set(false));
PIPELINE.with(|p| *p.borrow_mut() = StagePipeline::new());
BODY_ID.with(|b| {
let mut b = b.borrow_mut();
dom::set_attr(&b, "data-stage", "");
b.clear();
});
dom::swap_inner(SLOT, "");
}