use crate::{
    error::Error,
    interpreter::{memory::Memory, pointer::Pointer},
};
use arcis_compiler::core::global_value::global_expr_store::with_global_expr_store_as_local;
use quote::ToTokens;

pub fn add_to_outputs(
    pointer: Pointer,
    memory: &mut Memory,
    span: impl ToTokens,
) -> Result<Vec<usize>, Error> {
    let vals = pointer.as_data().to_expr_ids(memory)?;
    let all_public = with_global_expr_store_as_local(|expr_store| {
        vals.iter().all(|x| expr_store.get_is_plaintext(*x))
    });
    if !all_public {
        return Err(Error::new_spanned(
            span,
            "Returned values should be public.\n Note: you may need to encrypt your result with a `Enc`, or reveal it to the whole world with `.reveal()`.",
        ));
    }
    Ok(vals)
}
