/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { toProtoDuration } from "@vertexvis/stream-api";
import { UUID } from "@vertexvis/utils";
import { DEFAULT_TIMEOUT_IN_MS } from "../stream/dispatcher";
import { CameraRenderResult } from "./cameraRenderResult";
import { buildSceneViewStateIdentifier, toPbSceneViewStateFeatures, } from "./mapper";
export class SceneViewStateLoader {
    constructor(stream, decodeFrame, encodeCameraType, sceneId, sceneViewId) {
        this.stream = stream;
        this.decodeFrame = decodeFrame;
        this.encodeCameraType = encodeCameraType;
        this.sceneId = sceneId;
        this.sceneViewId = sceneViewId;
    }
    async applySceneViewState(sceneViewStateId, opts = {}) {
        const pbIdField = buildSceneViewStateIdentifier(sceneViewStateId);
        await this.animateToSceneViewState(pbIdField, opts);
        return await this.stream.loadSceneViewState({
            ...pbIdField,
            frameCorrelationId: opts.suppliedCorrelationId
                ? { value: opts.suppliedCorrelationId }
                : undefined,
            cameraType: opts.cameraTypeOverride
                ? this.encodeCameraType(opts.cameraTypeOverride)
                : undefined,
        }, true);
    }
    async applyPartialSceneViewState(sceneViewStateId, featuresToApply, opts = {}) {
        const pbIdField = buildSceneViewStateIdentifier(sceneViewStateId);
        const pbFeatures = toPbSceneViewStateFeatures(featuresToApply);
        if (featuresToApply.includes('camera')) {
            await this.animateToSceneViewState(pbIdField, opts);
        }
        return await this.stream.loadSceneViewState({
            ...pbIdField,
            frameCorrelationId: opts.suppliedCorrelationId
                ? { value: opts.suppliedCorrelationId }
                : undefined,
            sceneViewStateFeatureSubset: pbFeatures,
            cameraType: opts.cameraTypeOverride
                ? this.encodeCameraType(opts.cameraTypeOverride)
                : undefined,
        }, true);
    }
    async animateToSceneViewState(identifier, opts) {
        var _a, _b, _c;
        if (opts.animation != null) {
            const corrId = UUID.create();
            const flyToResult = await this.stream.flyTo({
                sceneViewStateIdentifier: identifier,
                animation: {
                    duration: toProtoDuration(opts.animation.milliseconds),
                },
                frameCorrelationId: {
                    value: corrId,
                },
            }, true);
            if (opts.waitForAnimation == null || opts.waitForAnimation) {
                const renderResult = new CameraRenderResult(this.stream, this.decodeFrame, {
                    animationId: (_c = (_b = (_a = flyToResult.flyTo) === null || _a === void 0 ? void 0 : _a.animationId) === null || _b === void 0 ? void 0 : _b.hex) !== null && _c !== void 0 ? _c : undefined,
                    correlationId: corrId,
                }, opts.animation.milliseconds + DEFAULT_TIMEOUT_IN_MS);
                await renderResult.onAnimationCompleted.once();
            }
        }
    }
}
//# sourceMappingURL=sceneViewStateLoader.js.map
