"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.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.runAnalyzeCommand = runAnalyzeCommand;
/**
 * `smartperfetto analyze <trace>` — one-shot analysis.
 *
 * Thin wrapper: owns the CLI process lifecycle (bootstrap, service
 * construction, shutdown) and delegates the actual work to
 * `turnRunner.startSession`. The same runner is shared by `resume`
 * (via continueSession) and the REPL.
 */
const path = __importStar(require("path"));
const bootstrap_1 = require("../bootstrap");
const cliAnalyzeService_1 = require("../services/cliAnalyzeService");
const renderer_1 = require("../repl/renderer");
const turnRunner_1 = require("../services/turnRunner");
const runtimeGuard_1 = require("../services/runtimeGuard");
const stdio_1 = require("../io/stdio");
async function runAnalyzeCommand(args) {
    // Resolve tracePath against the *user's* cwd before bootstrap runs — bootstrap
    // pins cwd to the backend root for consistent service-layer path resolution,
    // which would otherwise change how a relative trace argument gets interpreted.
    const tracePath = path.resolve(args.trace);
    const { paths } = (0, bootstrap_1.bootstrap)({ envFile: args.envFile, sessionDir: args.sessionDir });
    const renderer = (0, renderer_1.createRenderer)({ verbose: args.verbose, useColor: !args.noColor, format: args.format });
    const service = new cliAnalyzeService_1.CliAnalyzeService();
    let exitCode = 0;
    try {
        await (0, stdio_1.withConsoleLogToStderr)(renderer.format !== 'text', async () => {
            (0, runtimeGuard_1.assertAnalysisRuntimeReady)();
            const turn = await (0, turnRunner_1.startSession)({ paths, service, renderer }, {
                tracePath,
                query: args.query,
                analysisMode: args.analysisMode,
                codeAwareMode: args.codeAwareMode,
                codebaseIds: args.codebaseIds,
            });
            exitCode = turn.success ? 0 : 1;
        });
        return exitCode;
    }
    catch (err) {
        renderer.printError(err.message);
        return 1;
    }
    finally {
        await service.shutdown();
    }
}
//# sourceMappingURL=analyze.js.map