import _ from 'lodash';
import '../../../assert/js/assert.js';
import '../../../globals.js';
// Copyright 2020-2024, University of Colorado Boulder
/**
 * A trait to be mixed into PressListeners for identifying which SpriteInstance of a given Sprites node was interacted
 * with, AND will prevent interactions that are NOT over any SpriteInstances.
 *
 * @author Jonathan Olson <jonathan.olson@colorado.edu>
 */
import inheritance from '../../../phet-core/js/inheritance.js';
import memoize from '../../../phet-core/js/memoize.js';
import { PressListener, scenery, Sprites } from '../imports.js';
/**
 * @param type - Should be a PressListener-based type
 */
const SpriteListenable = memoize((type) => {
    assert && assert(_.includes(inheritance(type), PressListener), 'Only PressListener subtypes should mix SpriteListenable');
    return class extends type {
        constructor(...args) {
            super(...args);
            this.spriteInstance = null;
        }
        /**
         * @override - see PressListener
         */
        press(event, targetNode, callback) {
            // If pressed, then the press would be exited later AND we wouldn't want to override our spriteInstance anyway.
            if (this.isPressed) {
                return false;
            }
            // Zero it out, so we only respond to Sprites instances.
            this.spriteInstance = null;
            if (event.currentTarget instanceof Sprites) {
                const sprites = event.currentTarget;
                this.spriteInstance = sprites.getSpriteInstanceFromPoint(sprites.globalToLocalPoint(event.pointer.point));
            }
            // If we have no instance, don't super-call (same behavior for never starting a press)
            if (this.spriteInstance) {
                return super.press(event, targetNode, callback);
            }
            else {
                return false;
            }
        }
    };
});
scenery.register('SpriteListenable', SpriteListenable);
export default SpriteListenable;
//# sourceMappingURL=SpriteListenable.js.map