/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { m as model_entity_pbExports, a as mapMeasureResponseOrThrow, g as geometry_pbExports } from './geometry_pb-BPrLQ_K_.js';
import { s as scene_view_api_pbExports, w as wrappers_pbExports } from './wrappers_pb-aFAwKqIf.js';
import { r as requestUnary, c as createMetadata } from './grpc-CZAYqbTu.js';
import { v as vertexvis } from './bundle-CLE0fYge.js';
import { m as mapper } from './bundle-DLfw87TV.js';
import { n as fromPbVector3f } from './streamAttributes-B6xi3kRV.js';

/**
 * The `MeasurementController` is responsible for performing measurements of
 * registered entities, and updating the model with their measurement results.
 */
class MeasurementController {
    constructor(model, client, jwtProvider, deviceId) {
        this.model = model;
        this.client = client;
        this.jwtProvider = jwtProvider;
        this.deviceId = deviceId;
        this.outcome = Promise.resolve(undefined);
        this.debugLogs = false;
    }
    /**
     * Registers an entity to measure and performs a measurement if this entity
     * has not been previously registered.
     *
     * @param entity The entity to measure.
     * @returns A promise that resolves with the results after registering this
     * entity.
     */
    addEntity(entity) {
        if (this.debugLogs) {
            const deserializedEntity = model_entity_pbExports.ModelEntity.deserializeBinary(entity.modelEntity);
            this.log(`Adding ModelEntity. [entityId={${deserializedEntity.getEntityId()}}, sceneItemId={${deserializedEntity.getSceneItemId()}}]`);
        }
        return this.performMeasurement(() => this.model.addEntity(entity));
    }
    /**
     * Clears all entities and returns a promise that resolves with an empty list
     * of measurement results.
     */
    clearEntities() {
        this.log('Clearing all ModelEntities.');
        return this.performMeasurement(() => {
            this.model.clearEntities();
            this.model.clearOutcome();
            return true;
        });
    }
    /**
     * Deregisters an entity and performs a measurement if this entity was
     * removed.
     *
     * @param entity The entity to remove.
     * @returns A promise that resolves with the results after removing this
     * entity.
     */
    removeEntity(entity) {
        if (this.debugLogs) {
            const deserializedEntity = model_entity_pbExports.ModelEntity.deserializeBinary(entity.modelEntity);
            this.log(`Removing ModelEntity. [entityId={${deserializedEntity.getEntityId()}}, sceneItemId={${deserializedEntity.getSceneItemId()}}]`);
        }
        return this.performMeasurement(() => this.model.removeEntity(entity));
    }
    /**
     * Registers a set of entities and performs a measurement
     *
     * @param entities The entities to measure.
     * @returns A promise that resolves with the results after registering these
     * entities.
     */
    setEntities(entities) {
        if (this.debugLogs) {
            const deserializedEntities = Array.from(entities).map((e) => model_entity_pbExports.ModelEntity.deserializeBinary(e.modelEntity));
            this.log(`Setting ${deserializedEntities.length} ModelEntities. [entityIds=[${deserializedEntities.map((e) => e.getEntityId()).join(', ')}], sceneItemIds=[${deserializedEntities.map((e) => e.getSceneItemId()).join(', ')}]]`);
        }
        return this.performMeasurement(() => this.model.setEntities(entities));
    }
    setDebugLogs(debugLogs) {
        this.debugLogs = debugLogs;
    }
    performMeasurement(effect) {
        const previous = this.model.getEntities();
        const changed = effect();
        const entities = this.model.getEntities();
        if (changed) {
            this.measureAndUpdateModel(entities);
            this.highlightEntities(previous, entities);
        }
        return this.outcome;
    }
    measureAndUpdateModel(entities) {
        if (entities.length > 0) {
            this.outcome = this.measureEntities(entities).then((outcome) => {
                this.model.setOutcome(outcome);
                return this.model.getOutcome();
            });
        }
        else {
            this.outcome = Promise.resolve(undefined);
        }
    }
    async measureEntities(entities) {
        if (entities.length > 0) {
            const res = await requestUnary(async (handler) => {
                const meta = await createMetadata(this.jwtProvider, this.deviceId);
                const req = new scene_view_api_pbExports.MeasureRequest();
                req.setEntitiesList(entities.map((e) => e.toProto()));
                this.client.measure(req, meta, handler);
            });
            return mapMeasureResponseOrThrow(res.toObject());
        }
        else {
            return undefined;
        }
    }
    async highlightEntities(previous, entities) {
        await requestUnary(async (handler) => {
            const meta = await createMetadata(this.jwtProvider, this.deviceId);
            const entitySet = new Set(entities);
            const newPrevious = previous.filter((e) => !entitySet.has(e));
            const clearEntities = newPrevious.map((e) => {
                const update = new scene_view_api_pbExports.ModelEntityUpdate();
                update.setModelEntity(model_entity_pbExports.ModelEntity.deserializeBinary(e.modelEntity));
                update.setHighlight(new wrappers_pbExports.BoolValue().setValue(false));
                return update;
            });
            const highlightEntities = entities.map((e) => {
                const update = new scene_view_api_pbExports.ModelEntityUpdate();
                update.setModelEntity(model_entity_pbExports.ModelEntity.deserializeBinary(e.modelEntity));
                update.setHighlight(new wrappers_pbExports.BoolValue().setValue(true));
                return update;
            });
            const req = new scene_view_api_pbExports.UpdateModelEntitiesRequest();
            req.setUpdatesList([...clearEntities, ...highlightEntities]);
            this.client.updateModelEntities(req, meta, handler);
        });
    }
    log(message, ...optionalParams) {
        if (this.debugLogs) {
            console.debug(message, optionalParams);
        }
    }
}

class MeasurementEntity {
    constructor(point, modelEntity) {
        this.point = point;
        this.modelEntity = modelEntity;
    }
    static fromHit(hit) {
        if (hit.hitPoint != null && hit.modelEntity != null) {
            const hitPoint = mapper.ifInvalidThrow(fromPbVector3f)(hit.hitPoint);
            const modelEntity = vertexvis.protobuf.core.ModelEntity.encode(hit.modelEntity).finish();
            return new MeasurementEntity(hitPoint, modelEntity);
        }
        else {
            throw new Error('Cannot create MeasurementEntity from Hit. Hit is missing hit point and model entity');
        }
    }
    toProto() {
        const entity = new scene_view_api_pbExports.MeasureEntity();
        const point = new geometry_pbExports.Vector3f();
        point.setX(this.point.x);
        point.setY(this.point.y);
        point.setZ(this.point.z);
        entity.setPoint(point);
        const modelEntity = model_entity_pbExports.ModelEntity.deserializeBinary(this.modelEntity);
        entity.setModelEntity(modelEntity);
        return entity;
    }
}

export { MeasurementController as M, MeasurementEntity as a };
//# sourceMappingURL=entities-DUXv_nvW.js.map

//# sourceMappingURL=entities-DUXv_nvW.js.map