//! Types related to `NodeList`s.
//!
//! ## MDN Documentation
//! https://developer.mozilla.org/en-US/docs/Web/API/NodeList

use crate::dom::node::DomNode;
use crate::ComponentImports;

/// A `NodeList`.
///
/// ## MDN Documentation
/// https://developer.mozilla.org/en-US/docs/Web/API/NodeList
#[derive(Clone)]
pub struct NodeList {
    handle: i64,
    imports: ComponentImports,
}

impl NodeList {
    pub(crate) fn new(handle: i64, imports: ComponentImports) -> Self {
        Self { handle, imports }
    }

    /// Get the node at the given index within the list.
    pub fn item(&self, index: i32) -> Option<DomNode> {
        let item = unsafe {
            afia_component_sys::node_list_item(
                self.imports.component_imports_ptr,
                self.handle,
                index,
            )
        };
        DomNode::new_maybe(&self.imports, item)
    }
}
