import { delegatedEvents, handlers } from "../internal-state";
export const delegateEvent = (eventName) => {
    if (!delegatedEvents.has(eventName)) {
        // TODO: allow registering deletegated event handlers at different levels than document
        // TODO: register at root element level
        document.addEventListener(eventName, (event) => {
            for (var handledTarget = event.target, h = handlers.get(event.target); handledTarget && !(h && h.some(conf => conf.eventName == event.type)); handledTarget = handledTarget.parentNode, h = handlers.get(handledTarget))
                // TODO: traverse in memory vs the DOM?
                ;
            return (h || [])
                .filter(h => h.handler && h.eventName == event.type)
                .map(h => h.handler(event, handledTarget) || true)
                .reduce((a, b) => a && b, true);
        }, 
        // doing it once would also need to add it multiple times!
        //, eventName == 'mount' ? {once: true} : undefined)
        { capture: true });
        delegatedEvents.add(eventName);
    }
};
