"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.ContextRestore = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class ContextRestore {
    snapshotPath;
    constructor(cellDir) {
        this.snapshotPath = path.join(cellDir, 'snapshots');
        this.ensureDirectory(this.snapshotPath);
    }
    capture(projectId, openFiles, cursorFile, cursorLine, todos, lastThought, branch) {
        const snapshot = {
            projectId,
            timestamp: new Date().toISOString(),
            openFiles,
            cursorFile,
            cursorLine,
            todos,
            lastThought,
            branch,
        };
        const filePath = path.join(this.snapshotPath, `${this.sanitizeName(projectId)}.json`);
        fs.writeFileSync(filePath, JSON.stringify(snapshot, null, 2), 'utf-8');
        return snapshot;
    }
    restore(projectId) {
        const filePath = path.join(this.snapshotPath, `${this.sanitizeName(projectId)}.json`);
        if (!fs.existsSync(filePath))
            return null;
        try {
            return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
        }
        catch {
            return null;
        }
    }
    detectSwitch(currentProjectId) {
        if (!fs.existsSync(this.snapshotPath))
            return null;
        const files = fs.readdirSync(this.snapshotPath).filter((f) => f.endsWith('.json'));
        for (const file of files) {
            const projectId = file.replace('.json', '');
            if (projectId !== this.sanitizeName(currentProjectId)) {
                return projectId;
            }
        }
        return null;
    }
    getResumePrompt(snapshot) {
        const age = this.getAge(snapshot.timestamp);
        return `Resume ${snapshot.projectId}? Last active ${age} ago. You had ${snapshot.openFiles.length} files open, cursor at ${snapshot.cursorFile}:${snapshot.cursorLine}. Branch: ${snapshot.branch}. ${snapshot.todos.length} TODOs pending.`;
    }
    getFreshStartPrompt() {
        return 'Start fresh with a new session.';
    }
    getAllSnapshots() {
        if (!fs.existsSync(this.snapshotPath))
            return [];
        const files = fs.readdirSync(this.snapshotPath).filter((f) => f.endsWith('.json'));
        return files.map((f) => {
            try {
                return JSON.parse(fs.readFileSync(path.join(this.snapshotPath, f), 'utf-8'));
            }
            catch {
                return null;
            }
        }).filter((s) => s !== null);
    }
    clear(projectId) {
        const filePath = path.join(this.snapshotPath, `${this.sanitizeName(projectId)}.json`);
        if (fs.existsSync(filePath)) {
            fs.unlinkSync(filePath);
        }
    }
    getAge(timestamp) {
        const diff = Date.now() - new Date(timestamp).getTime();
        const minutes = Math.floor(diff / 60000);
        if (minutes < 60)
            return `${minutes}m`;
        const hours = Math.floor(minutes / 60);
        if (hours < 24)
            return `${hours}h`;
        return `${Math.floor(hours / 24)}d`;
    }
    sanitizeName(name) {
        return name.replace(/[^a-zA-Z0-9_-]/g, '_');
    }
    ensureDirectory(dir) {
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true });
        }
    }
}
exports.ContextRestore = ContextRestore;
//# sourceMappingURL=context-restore.js.map