use super::*;
use std::collections::HashSet;
use std::sync::Arc;

impl MirInterpreter {
    /// Phase 287: Release strong references for all values (including SSA aliases)
    /// This is called by ReleaseStrong instruction for variable overwrite semantics.
    pub(super) fn release_strong_refs(&mut self, values: &[ValueId]) {
        let mut arc_ptrs: HashSet<*const dyn NyashBox> = HashSet::new();

        for value_id in values {
            if let Some(VMValue::BoxRef(arc)) = self.regs.get(value_id) {
                arc_ptrs.insert(Arc::as_ptr(arc));
            }
        }

        for value_id in values {
            self.regs.remove(value_id);
        }

        if arc_ptrs.is_empty() {
            return;
        }

        let to_remove: Vec<ValueId> = self
            .regs
            .iter()
            .filter_map(|(value_id, value)| match value {
                VMValue::BoxRef(arc) if arc_ptrs.contains(&Arc::as_ptr(arc)) => Some(*value_id),
                _ => None,
            })
            .collect();

        for value_id in to_remove {
            self.regs.remove(&value_id);
        }
    }
}

