use alloc::sync::Arc;

use device_api::interrupt::ExternalInterruptController;
use libk_util::OneTimeInit;
use yggdrasil_abi::error::Error;

pub mod block;
pub mod char;
pub mod display;
pub mod manager;

// TODO get rid of this, this does not work when there are multiple interrupt controllers
// Interrupt controllers don't disappear (or at least in my OS), so it's okay to keep a reference
// inside a static OneTimeInit.
static EXTERNAL_INTC: OneTimeInit<Arc<dyn ExternalInterruptController>> = OneTimeInit::new();

pub fn register_external_interrupt_controller(intc: Arc<dyn ExternalInterruptController>) {
    EXTERNAL_INTC.init(intc);
}

pub fn external_interrupt_controller(
) -> Result<&'static Arc<dyn ExternalInterruptController>, Error> {
    EXTERNAL_INTC.try_get().ok_or(Error::DoesNotExist)
}
