/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
'use strict';

var index = require('./index-C_J7n4Dv.js');
var model = require('./model-DPwH082N.js');
var cursors = require('./cursors-BjfBRMe4.js');
var dom = require('./dom-CXWXs9Hx.js');
require('./bundle-Dsc8KJRQ.js');
require('./_commonjsHelpers-CBbBosIU.js');
require('./bundle-96sYCpzz.js');
require('./bundle-BhTmftd-.js');

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(dom.getMouseClientPosition(event, this.elementBounds));
            window.addEventListener('pointermove', this.handleDrag);
            window.addEventListener('pointerup', this.handleDragEnd);
        }
    }
    handleDrag(event) {
        if (this.enabled) {
            this.controller.setEndPoint(dom.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(cursors.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');
    }
}

const viewerBoxQueryToolCss = () => `:host{--viewer-box-query-outline-exclusive-color:#0099cc;--viewer-box-query-outline-exclusive-border-style:solid;--viewer-box-query-outline-inclusive-color:#00cc00;--viewer-box-query-outline-inclusive-border-style:dashed;--viewer-box-query-outline-border-radius:0.25rem;--viewer-box-query-outline-fill-opacity:0.25}.bounds{position:absolute}.outline{position:absolute;top:0;left:0;width:100%;height:100%;box-sizing:border-box;border-radius:var(--viewer-box-query-outline-border-radius)}:host([exclusive="true"]) .outline{border:3px var(--viewer-box-query-outline-exclusive-border-style) var(--viewer-box-query-outline-exclusive-color)}:host([inclusive="true"]) .outline{border:3px var(--viewer-box-query-outline-inclusive-border-style) var(--viewer-box-query-outline-inclusive-color)}.fill{position:absolute;top:0;left:0;width:100%;height:100%;opacity:var(--viewer-box-query-outline-fill-opacity);border-radius:var(--viewer-box-query-outline-border-radius)}:host([exclusive="true"]) .fill{background-color:var(--viewer-box-query-outline-exclusive-color)}:host([inclusive="true"]) .fill{background-color:var(--viewer-box-query-outline-inclusive-color)}`;

const ViewerBoxQueryTool = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.controllerChanged = index.createEvent(this, "controllerChanged");
        /**
         * The default operation to perform when a drag has completed and the intersection
         * query will be run. Defaults to `clearAndSelect`, and can be changed to `select` or `deselect`.
         *
         * `clearAndSelect` will clear all existing selection, and select the results of the query.
         * `select` will maintain existing selection, and select the results of the query.
         * `deselect` will maintain existing selection, and deselect the results of the query.
         *
         * The operation behavior for this intersection query tool can also be changed by
         * providing a custom implementation of the `VolumeIntersectionQueryController`, or
         * by using the `setOperationTransform` method of the default controller.
         */
        this.operationType = 'clearAndSelect';
        this.handleScreenBoundsChanged = this.handleScreenBoundsChanged.bind(this);
        this.handleExecuteStarted = this.handleExecuteStarted.bind(this);
        this.handleExecuteComplete = this.handleExecuteComplete.bind(this);
    }
    componentWillLoad() {
        var _a;
        this.model = (_a = this.model) !== null && _a !== void 0 ? _a : new model.VolumeIntersectionQueryModel(this.mode);
        this.screenBoundsChangedDisposable = this.model.onScreenBoundsChanged(this.handleScreenBoundsChanged);
        this.handleViewerChanged(this.viewer);
        this.handleControllerChange(this.controller);
        this.handleDefaultOperationChange(this.operationType);
    }
    disconnectedCallback() {
        var _a, _b, _c, _d;
        (_a = this.model) === null || _a === void 0 ? void 0 : _a.reset();
        (_b = this.screenBoundsChangedDisposable) === null || _b === void 0 ? void 0 : _b.dispose();
        (_c = this.operationStartedDisposable) === null || _c === void 0 ? void 0 : _c.dispose();
        (_d = this.operationCompleteDisposable) === null || _d === void 0 ? void 0 : _d.dispose();
        this.deregisterInteractionHandler();
    }
    /**
     * @ignore
     */
    handleViewerChanged(newViewer) {
        this.deregisterInteractionHandler();
        if (this.model != null && newViewer != null) {
            this.controller = new model.VolumeIntersectionQueryController(this.model, newViewer);
            this.handleDefaultOperationChange(this.operationType);
            this.registerInteractionHandler(this.controller, newViewer);
        }
    }
    /**
     * @ignore
     */
    handleDefaultOperationChange(updatedOperationType) {
        switch (updatedOperationType) {
            case 'clearAndSelect':
                this.setDefaultClearAndSelectOperation();
                break;
            case 'select':
                this.setDefaultSelectOperation();
                break;
            case 'deselect':
                this.setDefaultDeselectOperation();
                break;
        }
    }
    /**
     * @ignore
     */
    handleModeChange(updatedMode) {
        var _a;
        (_a = this.model) === null || _a === void 0 ? void 0 : _a.setMode(updatedMode);
    }
    /**
     * @ignore
     */
    handleControllerChange(controller) {
        var _a, _b;
        (_a = this.operationStartedDisposable) === null || _a === void 0 ? void 0 : _a.dispose();
        (_b = this.operationCompleteDisposable) === null || _b === void 0 ? void 0 : _b.dispose();
        this.operationStartedDisposable = controller === null || controller === void 0 ? void 0 : controller.onExecuteStarted(this.handleExecuteStarted);
        this.operationStartedDisposable = controller === null || controller === void 0 ? void 0 : controller.onExecuteComplete(this.handleExecuteComplete);
        this.controllerChanged.emit(this.controller);
    }
    /**
     * @ignore
     */
    render() {
        return (index.h(index.Host, { key: '52fd0844f7a089687af0de575fc24c771408f7cc' }, index.h("vertex-viewer-layer", { key: '9f914b377d42c4a2250629df57ea4aae8e05305e' }, this.details != null && (index.h("div", { key: '498adff4a807104567bf3debdee741be3d3a2dd6', class: "bounds", style: {
                left: `${this.details.screenBounds.x}px`,
                top: `${this.details.screenBounds.y}px`,
                width: `${this.details.screenBounds.width}px`,
                height: `${this.details.screenBounds.height}px`,
            } }, index.h("slot", { key: '56a15f3ae338a20ccf5138f6f8c9d1269f063135', name: "bounds" }, index.h("div", { key: '28875acaed488318990aacb3761fad930dfdf059', class: "outline" }), index.h("div", { key: 'e49c3189e0d1051dd66a6d54d1eff23044f7f5b7', class: "fill" })))))));
    }
    handleScreenBoundsChanged(details) {
        this.details = details;
        this.updateTypeAttribute(details === null || details === void 0 ? void 0 : details.type);
    }
    handleExecuteStarted() {
        var _a;
        (_a = this.interactionHandler) === null || _a === void 0 ? void 0 : _a.disable();
    }
    handleExecuteComplete() {
        var _a;
        (_a = this.interactionHandler) === null || _a === void 0 ? void 0 : _a.enable();
    }
    async registerInteractionHandler(controller, viewer) {
        this.interactionHandler = new VolumeIntersectionQueryInteractionHandler(controller);
        this.interactionHandlerDisposable = await viewer.registerInteractionHandler(this.interactionHandler);
    }
    deregisterInteractionHandler() {
        var _a, _b;
        (_a = this.interactionHandlerDisposable) === null || _a === void 0 ? void 0 : _a.dispose();
        (_b = this.interactionHandler) === null || _b === void 0 ? void 0 : _b.dispose();
        this.interactionHandler = undefined;
    }
    updateTypeAttribute(type) {
        this.hostEl.setAttribute('inclusive', `${type === 'inclusive'}`);
        this.hostEl.setAttribute('exclusive', `${type === 'exclusive'}`);
    }
    setDefaultClearAndSelectOperation() {
        var _a, _b;
        (_a = this.controller) === null || _a === void 0 ? void 0 : _a.setOperationTransform((builder) => builder.select());
        (_b = this.controller) === null || _b === void 0 ? void 0 : _b.setAdditionalTransforms([
            (op) => op.items.where((q) => q.withSelected()).deselect(),
        ]);
    }
    setDefaultSelectOperation() {
        var _a, _b;
        (_a = this.controller) === null || _a === void 0 ? void 0 : _a.setOperationTransform((builder) => builder.select());
        (_b = this.controller) === null || _b === void 0 ? void 0 : _b.setAdditionalTransforms([]);
    }
    setDefaultDeselectOperation() {
        var _a, _b;
        (_a = this.controller) === null || _a === void 0 ? void 0 : _a.setOperationTransform((builder) => builder.deselect());
        (_b = this.controller) === null || _b === void 0 ? void 0 : _b.setAdditionalTransforms([]);
    }
    get hostEl() { return index.getElement(this); }
    static get watchers() { return {
        "viewer": [{
                "handleViewerChanged": 0
            }],
        "operationType": [{
                "handleDefaultOperationChange": 0
            }],
        "mode": [{
                "handleModeChange": 0
            }],
        "controller": [{
                "handleControllerChange": 0
            }]
    }; }
};
ViewerBoxQueryTool.style = viewerBoxQueryToolCss();

exports.vertex_viewer_box_query_tool = ViewerBoxQueryTool;
//# sourceMappingURL=vertex-viewer-box-query-tool.entry.cjs.js.map

//# sourceMappingURL=vertex-viewer-box-query-tool.cjs.entry.js.map