"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.runRmCommand = runRmCommand;
/**
 * `smartperfetto rm <sessionId>` — delete the local session folder.
 *
 * Scope note: this ONLY touches `~/.smartperfetto/sessions/<id>/` and the
 * global index. The backend-side persisted state (`backend/data/sessions/
 * sessions.db`) is intentionally untouched — users may still want to see
 * the session in the web UI, and removing it from SQLite is the backend's
 * responsibility (a separate cleanup command could be added later if needed).
 */
const fs = __importStar(require("fs"));
const readline = __importStar(require("readline"));
const bootstrap_1 = require("../bootstrap");
const sessionStore_1 = require("../io/sessionStore");
const indexJson_1 = require("../io/indexJson");
async function runRmCommand(args) {
    const { paths } = (0, bootstrap_1.bootstrap)({ envFile: args.envFile, sessionDir: args.sessionDir, requireLlm: false });
    const { sp, config } = (0, sessionStore_1.loadSession)(paths, args.sessionId);
    if (!config) {
        console.error(`Error: no session found at ${sp.dir}`);
        return 1;
    }
    if (!args.yes) {
        // readline.question hangs forever on stdin when it's not a TTY (CI,
        // piped input). Require --yes explicitly in that case so scripted use
        // fails fast instead of appearing to stall.
        if (!process.stdin.isTTY) {
            console.error(`Error: non-interactive shell — pass --yes to delete ${args.sessionId} without confirmation.`);
            return 1;
        }
        const ok = await confirm(`Delete session ${args.sessionId} (${config.turnCount} turn${config.turnCount === 1 ? '' : 's'}, trace: ${config.tracePath})? [y/N] `);
        if (!ok) {
            console.log('Cancelled.');
            return 0;
        }
    }
    fs.rmSync(sp.dir, { recursive: true, force: true });
    const idx = (0, indexJson_1.readIndex)(paths);
    delete idx.sessions[args.sessionId];
    (0, indexJson_1.writeIndex)(paths, idx);
    console.log(`Removed ${args.sessionId}`);
    return 0;
}
function confirm(prompt) {
    return new Promise((resolve) => {
        const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
        rl.question(prompt, (answer) => {
            rl.close();
            resolve(/^y(es)?$/i.test(answer.trim()));
        });
    });
}
//# sourceMappingURL=rm.js.map