use rbx_dom_weak::WeakDom;
use roblox_emulator::context::Context;

pub use error::RecoverableErrors;
pub use roblox_emulator::runner::Error as RunnerError;

pub mod rbx;
pub mod mesh;
mod error;
pub mod union;
pub mod primitives;

pub struct Model{
	dom:WeakDom,
}
impl Model{
	pub fn new(dom:WeakDom)->Self{
		Self{dom}
	}
}
impl AsRef<WeakDom> for Model{
	fn as_ref(&self)->&WeakDom{
		&self.dom
	}
}

pub struct Place{
	context:Context,
}
impl Place{
	pub fn new(dom:WeakDom)->Result<Self,roblox_emulator::context::ServicesError>{
		let context=Context::from_place(dom)?;
		Ok(Self{
			context,
		})
	}
	pub fn run_scripts(&mut self)->Result<Vec<RunnerError>,RunnerError>{
		let Place{context}=self;
		let runner=roblox_emulator::runner::Runner::new()?;
		let scripts=context.scripts();
		let runnable=runner.runnable_context(context)?;
		let mut errors=Vec::new();
		for script in scripts{
			if let Err(e)=runnable.run_script(script){
				errors.push(e);
			}
		}
		Ok(errors)
	}
}
impl AsRef<WeakDom> for Place{
	fn as_ref(&self)->&WeakDom{
		self.context.as_ref()
	}
}
impl From<Model> for Place{
	fn from(model:Model)->Self{
		let context=Context::from_model(model.dom);
		Self{
			context,
		}
	}
}
