import { isEventKey } from '../trois/lib'
import { Trois } from '../types'

export const remove = (element: Trois.Element) => {
    console.log(element)
    // cancel if nothing to remove

    // remove from parent if needed
    element?.instance?.parent?.remove(element?.instance)
    // dispose if needed
    element?.instance?.dispose?.()
    // remove event listeners if needed
    if (element) {
        Object.keys(element?.eventListenerRemoveFunctions).forEach((evtKey: string) => {
            if (!isEventKey(evtKey)) {
                throw 'incorrect event key: ' + evtKey
            }
            element?.eventListenerRemoveFunctions[evtKey].forEach(f => f())
        })
    }

    // run on children
    element?.children?.forEach?.(child => {
        remove(child)
    })
}