"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.resolveStrategyPrebuiltContext = resolveStrategyPrebuiltContext;
exports.shouldSkipStageForPrebuilt = shouldSkipStageForPrebuilt;
function resolveStrategyPrebuiltContext(strategy, ctx) {
    const prebuiltIntervals = getPrebuiltIntervals(ctx);
    const hasPrebuiltContext = prebuiltIntervals.length > 0;
    const prebuiltEntityType = inferPrebuiltEntityType(prebuiltIntervals, hasPrebuiltContext);
    const stagesToRun = strategy.stages.filter(stage => {
        if (!hasPrebuiltContext)
            return true;
        const skip = shouldSkipStageForPrebuilt(stage, {
            hasPrebuiltContext,
            prebuiltEntityType,
        });
        return !skip.skip;
    });
    return {
        prebuiltIntervals,
        hasPrebuiltContext,
        prebuiltEntityType,
        effectiveTotalStages: stagesToRun.length,
    };
}
function shouldSkipStageForPrebuilt(stage, context) {
    if (!context.hasPrebuiltContext) {
        return { skip: false };
    }
    if (context.prebuiltEntityType === 'frame' && stage.name === 'session_overview') {
        return {
            skip: true,
            reason: 'Pre-built frame intervals (skip session overview)',
        };
    }
    const isDiscoveryStage = !!stage.extractIntervals;
    const allTasksGlobal = stage.tasks.every(t => t.scope === 'global');
    if (isDiscoveryStage && allTasksGlobal) {
        return {
            skip: true,
            reason: 'Using pre-built intervals from follow-up',
        };
    }
    return { skip: false };
}
function inferPrebuiltEntityType(prebuiltIntervals, hasPrebuiltContext) {
    if (!hasPrebuiltContext)
        return 'unknown';
    const meta = prebuiltIntervals[0]?.metadata || {};
    if (meta.sourceEntityType === 'frame' || meta.frame_id || meta.frameId)
        return 'frame';
    if (meta.sourceEntityType === 'session' || meta.session_id || meta.sessionId)
        return 'session';
    return 'unknown';
}
function getPrebuiltIntervals(ctx) {
    const optionIntervals = ctx.options.prebuiltIntervals;
    if (Array.isArray(optionIntervals) && optionIntervals.length > 0) {
        return optionIntervals;
    }
    const incrementalIntervals = ctx.incrementalScope?.focusIntervals;
    if (Array.isArray(incrementalIntervals) && incrementalIntervals.length > 0) {
        return incrementalIntervals;
    }
    return [];
}
//# sourceMappingURL=strategyStagePlanner.js.map