import _ from 'lodash';
import '../../assert/js/assert.js';
import '../../globals.js';
import { Mouse } from '../../scenery/js/imports.js';
import sun from './sun.js';
export default class ClosestDragForwardingListener {
    constructor(touchThreshold, mouseThreshold) {
        this.touchThreshold = touchThreshold;
        this.mouseThreshold = mouseThreshold;
        this.items = [];
    }
    /**
     * Adds an item that can be dragged.
     */
    addDraggableItem(item) {
        this.items.push(item);
    }
    /**
     * Removes a previously-added item.
     */
    removeDraggableItem(item) {
        const index = _.indexOf(this.items, item);
        assert && assert(index >= 0);
        this.items.splice(index, 1);
    }
    /**
     * Called on pointer down.
     */
    down(event) {
        // If there was nothing else in the way
        if (event.target === event.currentTarget) {
            let threshold = 0;
            if (event.pointer.isTouchLike()) {
                threshold = this.touchThreshold;
            }
            if (event.pointer instanceof Mouse) {
                threshold = this.mouseThreshold;
            }
            if (threshold) {
                // search for the closest item
                let currentItem = null;
                let currentDistance = Number.POSITIVE_INFINITY;
                const globalPoint = event.pointer.point;
                const numItems = this.items.length;
                for (let i = 0; i < numItems; i++) {
                    const item = this.items[i];
                    const distance = item.computeDistance(globalPoint);
                    if (distance < currentDistance) {
                        currentDistance = distance;
                        currentItem = item;
                    }
                }
                // if we have a closest item under the threshold, attempt to start a drag on it
                if (currentItem && currentDistance < threshold) {
                    currentItem.startDrag(event);
                }
            }
        }
    }
}
sun.register('ClosestDragForwardingListener', ClosestDragForwardingListener);
//# sourceMappingURL=ClosestDragForwardingListener.js.map