import '../../../globals.js';
// Copyright 2023-2024, University of Colorado Boulder
/**
 * A collection of information about an event and the environment when it was fired
 *
 * @author Jonathan Olson <jonathan.olson@colorado.edu>
 */
import IOType from '../../../tandem/js/types/IOType.js';
import { EventIO, Input, scenery } from '../imports.js';
export default class EventContext {
    constructor(domEvent) {
        this.domEvent = domEvent;
        this.activeElement = document.activeElement;
    }
    static createSynthetic() {
        return new EventContext(new window.Event('synthetic'));
    }
    /**
     * DOM (Scenery) nodes set dataset.sceneryAllowInput on their container if they don't want preventDefault to be called,
     * or other effects that block input (e.g. setPointerCapture). We search up the tree to detect this.
     */
    allowsDOMInput() {
        const target = this.domEvent?.target;
        if (target instanceof Element) {
            let element = target;
            while (element) {
                // For DOM nodes, we can check for a data attribute
                if (element instanceof HTMLElement && element.dataset?.sceneryAllowInput === 'true') {
                    return true;
                }
                element = element.parentNode;
            }
        }
        return false;
    }
}
export const EventContextIO = new IOType('EventContextIO', {
    valueType: EventContext,
    documentation: 'A DOM event and its context',
    toStateObject: eventContext => {
        return {
            domEvent: Input.serializeDomEvent(eventContext.domEvent)
            // Ignores the activeElement, since we don't have a good way of serializing that at this point?
        };
    },
    fromStateObject: stateObject => {
        return new EventContext(Input.deserializeDomEvent(stateObject.domEvent));
    },
    // This should remain the same as Input.domEventPropertiesToSerialize (local var). Each key can be null depending on
    // what Event interface is being serialized (which depends on what DOM Event the instance is).
    stateSchema: () => ({
        domEvent: EventIO
        // Ignores the activeElement, since we don't have a good way of serializing that at this point?
    })
});
scenery.register('EventContext', EventContext);
//# sourceMappingURL=EventContext.js.map