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

var bundle$1 = require('./bundle-Dsc8KJRQ.js');
var bundle = require('./bundle-96sYCpzz.js');
var bundle$2 = require('./bundle-BhTmftd-.js');

class VolumeIntersectionQueryController {
    constructor(model, viewer) {
        this.model = model;
        this.viewer = viewer;
        this.additionalTransforms = [];
        this.operationInFlight = false;
        this.operationAborted = false;
        this.executeStarted = new bundle.EventDispatcher();
        this.executeComplete = new bundle.EventDispatcher();
        this.executeAborted = new bundle.EventDispatcher();
        this.additionalTransforms = [
            (op) => op.items.where((q) => q.all()).deselect(),
        ];
        this.operationTransform = (builder) => builder.select();
    }
    setStartPoint(point) {
        this.previousViewerCameraControls = this.viewer.cameraControls;
        this.viewer.cameraControls = false;
        this.model.setStartPoint(point);
    }
    setEndPoint(point) {
        this.model.setEndPoint(point);
    }
    /**
     * Updates the operation that will be applied based on the volume intersection
     * query. Defaults to `(builder) => builder.select()`, which will select any
     * of the results of the query.
     */
    setOperationTransform(operationTransform) {
        this.operationTransform = operationTransform;
    }
    /**
     * Updates any additional operations that should be performed independent of
     * the volume intersection query. This can be used to perform an operation on
     * the entirety of the scene prior to the operation on the result of the
     * volume intersection query.
     * Defaults to `[(op) => op.items.where((q) => q.all()).deselect()]`, which will
     * clear any prior selection before the default selection.
     */
    setAdditionalTransforms(additionalTransforms) {
        this.additionalTransforms = additionalTransforms;
    }
    onExecuteStarted(listener) {
        return this.executeStarted.on(listener);
    }
    onExecuteComplete(listener) {
        return this.executeComplete.on(listener);
    }
    onExecuteAborted(listener) {
        return this.executeAborted.on(listener);
    }
    async execute() {
        var _a, _b, _c;
        const screenBounds = this.model.getScreenBounds();
        const type = this.model.getType();
        if (screenBounds != null && !this.operationInFlight) {
            this.model.complete();
            try {
                this.operationInFlight = true;
                this.executeStarted.emit();
                const additionalTransforms = (op) => this.additionalTransforms.map((t) => t(op)).flat();
                const operationTransforms = (op) => [
                    this.operationTransform(op.items.where((q) => q.withVolumeIntersection(screenBounds, type === 'exclusive'))),
                ].flat();
                const scene = await this.viewer.scene();
                await scene
                    .elements((op) => [
                    ...additionalTransforms(op),
                    ...operationTransforms(op),
                ])
                    .execute({
                    skipAwaitCorrelatedFrame: true,
                });
            }
            catch (e) {
                if (e instanceof bundle$1.StreamRequestError &&
                    ((_a = e.summary) === null || _a === void 0 ? void 0 : _a.toLocaleLowerCase().includes('operation aborted'))) {
                    this.executeAborted.emit(e);
                    this.operationAborted = true;
                }
                else {
                    console.error('Failed to perform volume intersection query', e);
                    throw e;
                }
            }
            finally {
                this.viewer.cameraControls = (_b = this.previousViewerCameraControls) !== null && _b !== void 0 ? _b : true;
                this.previousViewerCameraControls = undefined;
                this.operationInFlight = false;
                this.executeComplete.emit({
                    aborted: this.operationAborted,
                });
                this.operationAborted = false;
            }
        }
        else if (this.operationInFlight) {
            this.model.cancel();
            throw new Error(`Unable to perform volume intersection query as there is already one in-flight.`);
        }
        else {
            this.model.cancel();
            this.viewer.cameraControls = (_c = this.previousViewerCameraControls) !== null && _c !== void 0 ? _c : true;
        }
    }
}

class VolumeIntersectionQueryModel {
    constructor(mode) {
        this.mode = mode;
        this.dragStarted = new bundle.EventDispatcher();
        this.dragComplete = new bundle.EventDispatcher();
        this.dragCancelled = new bundle.EventDispatcher();
        this.screenBoundsChanged = new bundle.EventDispatcher();
    }
    setStartPoint(point) {
        this.startPoint = point;
        this.dragStarted.emit();
    }
    setEndPoint(point) {
        if (this.startPoint != null &&
            bundle$2.point.distance(this.startPoint, point) >= 2) {
            this.endPoint = point;
            this.updateQueryType();
            this.screenBoundsChanged.emit(this.getQueryDetails());
        }
    }
    setMode(mode) {
        this.mode = mode;
    }
    complete() {
        if (this.startPoint != null && this.endPoint != null) {
            this.screenBoundsChanged.emit(undefined);
            this.dragComplete.emit(this.getQueryDetails());
            this.reset();
        }
    }
    cancel() {
        this.screenBoundsChanged.emit(undefined);
        this.dragCancelled.emit();
        this.reset();
    }
    reset() {
        this.startPoint = undefined;
        this.endPoint = undefined;
        this.type = undefined;
    }
    getScreenBounds() {
        return this.startPoint != null && this.endPoint != null
            ? bundle$2.rectangle.fromPoints(this.startPoint, this.endPoint)
            : undefined;
    }
    getType() {
        return this.type;
    }
    onScreenBoundsChanged(listener) {
        return this.screenBoundsChanged.on(listener);
    }
    onDragStarted(listener) {
        return this.dragStarted.on(listener);
    }
    onDragComplete(listener) {
        return this.dragComplete.on(listener);
    }
    onDragCancelled(listener) {
        return this.dragCancelled.on(listener);
    }
    getQueryDetails() {
        if (this.startPoint != null && this.endPoint != null && this.type != null) {
            return {
                screenBounds: bundle$2.rectangle.fromPoints(this.startPoint, this.endPoint),
                type: this.type,
            };
        }
        else {
            throw new Error('Failed to create query details, the start and end points must be set.');
        }
    }
    updateQueryType() {
        var _a;
        if (this.startPoint != null && this.endPoint != null) {
            const directionalType = bundle$2.point.subtract(this.endPoint, this.startPoint).x > 0
                ? 'exclusive'
                : 'inclusive';
            this.type = (_a = this.mode) !== null && _a !== void 0 ? _a : directionalType;
        }
    }
}

exports.VolumeIntersectionQueryController = VolumeIntersectionQueryController;
exports.VolumeIntersectionQueryModel = VolumeIntersectionQueryModel;
//# sourceMappingURL=model-DPwH082N.js.map

//# sourceMappingURL=model-DPwH082N.js.map