//! Internal DOM manipulation utilities. Generated by the `template!` macro. Should not be used directly.
//! Internal APIs can be changed at any time without a major release.

use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;

use wasm_bindgen::{prelude::*, JsCast};
use web_sys::{DocumentFragment, Element, Event, Node};

use crate::prelude::*;

/// Create a new [`Element`] with the specified tag.
pub fn element(tag: &str) -> Element {
    web_sys::window()
        .unwrap()
        .document()
        .unwrap()
        .create_element(tag)
        .unwrap()
        .dyn_into()
        .unwrap()
}

pub fn fragment() -> DocumentFragment {
    web_sys::window()
        .unwrap()
        .document()
        .unwrap()
        .create_document_fragment()
}

/// Sets an attribute on an [`Element`].
pub fn attr(element: &Element, name: &str, value: Box<dyn Fn() -> String>) {
    let element = element.clone();
    let name = name.to_string();
    create_effect(move || {
        element.set_attribute(&name, &value()).unwrap();
    })
}

type EventListener = dyn Fn(Event);

thread_local! {
    /// A global event listener pool to prevent [`Closure`]s from being deallocated.
    /// TODO: remove events when elements are detached.
    static EVENT_LISTENERS: RefCell<Vec<Closure<EventListener>>> = RefCell::new(Vec::new());
}

/// Sets an event listener on an [`Element`].
pub fn event(element: &Element, name: &str, handler: Box<EventListener>) {
    let closure = Closure::wrap(handler);
    element
        .add_event_listener_with_callback(name, closure.as_ref().unchecked_ref())
        .unwrap();

    EVENT_LISTENERS.with(|event_listeners| event_listeners.borrow_mut().push(closure));
}

/// Appends a child node to an element.
pub fn append(element: &dyn AsRef<Node>, child: &dyn AsRef<Node>) {
    element.as_ref().append_child(child.as_ref()).unwrap();
}

/// Appends a [`dyn Render`](Render) to the `parent` node.
/// Node is created inside an effect with [`Render::update_node`].
pub fn append_render(parent: &dyn AsRef<Node>, child: Box<dyn Fn() -> Box<dyn Render>>) {
    let parent = parent.as_ref().clone();

    let node = create_effect_initial(cloned!((parent) => move || {
        let node = RefCell::new(child().render());

        let effect = cloned!((node) => move || {
            let new_node = child().update_node(&parent, &node.borrow());
            *node.borrow_mut() = new_node;
        });

        (Rc::new(effect), node)
    }));

    parent.append_child(&node.borrow()).unwrap();
}

/// Appends a static text node to the `parent` node.
pub fn append_static_text(parent: &dyn AsRef<Node>, text: &dyn fmt::Display) {
    let text_node = web_sys::window()
        .unwrap()
        .document()
        .unwrap()
        .create_text_node(&format!("{}", text));

    parent.as_ref().append_child(&text_node).unwrap();
}

/// Sets the value of a [`NodeRef`].
pub fn set_noderef(node: &dyn AsRef<Node>, noderef: NodeRef) {
    noderef.set(node.as_ref().clone());
}
