"use strict";
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.generateSessionId = generateSessionId;
exports.sessionDir = sessionDir;
exports.sessionFilePath = sessionFilePath;
exports.createSession = createSession;
exports.readSession = readSession;
exports.writeSession = writeSession;
exports.deleteSession = deleteSession;
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
function generateSessionId() {
    return `mabl-debug-${Date.now()}`;
}
function sessionDir(sessionId) {
    return path.join(os.homedir(), '.mabl', 'debug', sessionId);
}
function sessionFilePath(sessionId) {
    return path.join(sessionDir(sessionId), 'session.json');
}
function createSession(params) {
    const sessionId = params.sessionId ?? generateSessionId();
    const session = {
        sessionId,
        testId: params.testId,
        createdAt: new Date().toISOString(),
        wsEndpoint: params.wsEndpoint,
        browserPort: params.browserPort,
        browserPid: params.browserPid,
        userDataDir: params.userDataDir,
        currentStepIndex: 0,
        variables: {},
        stepResults: [],
        snapshot: params.snapshot,
    };
    fs.mkdirSync(sessionDir(sessionId), { recursive: true });
    fs.writeFileSync(sessionFilePath(sessionId), JSON.stringify(session, undefined, 2), 'utf8');
    return session;
}
function readSession(sessionId) {
    const filePath = sessionFilePath(sessionId);
    if (!fs.existsSync(filePath)) {
        throw new Error(`Debug session not found: ${sessionId} (looked at ${filePath})`);
    }
    return JSON.parse(fs.readFileSync(filePath, 'utf8'));
}
function writeSession(session) {
    fs.writeFileSync(sessionFilePath(session.sessionId), JSON.stringify(session, undefined, 2), 'utf8');
}
function deleteSession(sessionId) {
    const dir = sessionDir(sessionId);
    if (fs.existsSync(dir)) {
        fs.rmSync(dir, { recursive: true, force: true });
    }
}
