"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.SceneStage1Runner = void 0;
const sceneIntervalBuilder_1 = require("./sceneIntervalBuilder");
class SceneStage1Runner {
    constructor(deps) {
        this.deps = deps;
    }
    /**
     * Run the scene_reconstruction skill and return scene-shaped results.
     *
     * @param traceId Backend trace identifier
     * @param onEnvelope Optional per-envelope callback so the caller can
     *   broadcast each envelope as a `data` SSE event before the full Stage 1
     *   result is returned. Errors thrown by the callback are swallowed and
     *   logged so they cannot abort the pipeline.
     */
    async run(traceId, onEnvelope) {
        const rawResult = await this.deps.execute('scene_reconstruction', traceId, {
            trace_id: traceId,
        });
        if (!rawResult.success) {
            throw new Error(`scene_reconstruction skill failed: ${rawResult.error ?? 'unknown error'}`);
        }
        const envelopes = this.deps.toEnvelopes(rawResult, traceId);
        if (onEnvelope) {
            for (const env of envelopes) {
                try {
                    onEnvelope(env);
                }
                catch (err) {
                    console.warn('[SceneStage1Runner] onEnvelope callback threw, continuing:', err?.message ?? err);
                }
            }
        }
        const built = (0, sceneIntervalBuilder_1.buildDisplayedScenes)(envelopes);
        return {
            envelopes,
            scenes: built.scenes,
            traceDurationSec: built.traceDurationSec,
            rawResult,
        };
    }
}
exports.SceneStage1Runner = SceneStage1Runner;
//# sourceMappingURL=sceneStage1Runner.js.map