/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { boxQueryCursor } from "../cursors";
import { getMouseClientPosition } from "../dom";
export class VolumeIntersectionQueryInteractionHandler {
    constructor(controller) {
        this.controller = controller;
        this.handleDragBegin = this.handleDragBegin.bind(this);
        this.handleDrag = this.handleDrag.bind(this);
        this.handleDragEnd = this.handleDragEnd.bind(this);
        this.enabled = true;
    }
    initialize(element, api) {
        this.element = element;
        this.api = api;
        this.element.addEventListener('pointerdown', this.handleDragBegin);
        this.addCrosshairCursor();
    }
    dispose() {
        var _a, _b, _c;
        (_a = this.element) === null || _a === void 0 ? void 0 : _a.removeEventListener('pointerdown', this.handleDragBegin);
        window.removeEventListener('pointermove', this.handleDrag);
        window.removeEventListener('pointerup', this.handleDragEnd);
        (_b = this.crosshairCursorDisposable) === null || _b === void 0 ? void 0 : _b.dispose();
        (_c = this.waitCursorDisposable) === null || _c === void 0 ? void 0 : _c.dispose();
        this.element = undefined;
        this.api = undefined;
        this.enabled = true;
    }
    disable() {
        this.enabled = false;
    }
    enable() {
        this.enabled = true;
    }
    handleDragBegin(event) {
        var _a;
        if (this.enabled &&
            this.isPrimaryMouseButtonClick(event) &&
            !this.isInteracting) {
            this.elementBounds = (_a = this.element) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
            this.isInteracting = true;
            this.controller.setStartPoint(getMouseClientPosition(event, this.elementBounds));
            window.addEventListener('pointermove', this.handleDrag);
            window.addEventListener('pointerup', this.handleDragEnd);
        }
    }
    handleDrag(event) {
        if (this.enabled) {
            this.controller.setEndPoint(getMouseClientPosition(event, this.elementBounds));
        }
    }
    async handleDragEnd() {
        var _a, _b;
        this.isInteracting = false;
        window.removeEventListener('pointermove', this.handleDrag);
        window.removeEventListener('pointerup', this.handleDragEnd);
        (_a = this.crosshairCursorDisposable) === null || _a === void 0 ? void 0 : _a.dispose();
        if (this.enabled) {
            this.addWaitCursor();
            try {
                await this.controller.execute();
            }
            finally {
                (_b = this.waitCursorDisposable) === null || _b === void 0 ? void 0 : _b.dispose();
                this.addCrosshairCursor();
            }
        }
    }
    isPrimaryMouseButtonClick(event) {
        return event.buttons === 1;
    }
    addCrosshairCursor() {
        var _a, _b;
        (_a = this.crosshairCursorDisposable) === null || _a === void 0 ? void 0 : _a.dispose();
        this.crosshairCursorDisposable = (_b = this.api) === null || _b === void 0 ? void 0 : _b.addCursor(boxQueryCursor);
    }
    addWaitCursor() {
        var _a, _b;
        (_a = this.waitCursorDisposable) === null || _a === void 0 ? void 0 : _a.dispose();
        this.waitCursorDisposable = (_b = this.api) === null || _b === void 0 ? void 0 : _b.addCursor('wait');
    }
}
//# sourceMappingURL=interactions.js.map
