"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.ChangeTracker = void 0;
const vscode = __importStar(require("vscode"));
class ChangeTracker {
    sessions = new Map();
    currentSessionId = null;
    changeIdCounter = 0;
    sessionIdCounter = 0;
    startSession(description = 'AI Session') {
        const sessionId = `session_${++this.sessionIdCounter}_${Date.now()}`;
        this.sessions.set(sessionId, {
            id: sessionId,
            changes: [],
            startTime: new Date(),
            description
        });
        this.currentSessionId = sessionId;
        return sessionId;
    }
    getCurrentSession() {
        if (!this.currentSessionId) {
            return null;
        }
        return this.sessions.get(this.currentSessionId) || null;
    }
    async trackChange(filePath, originalContent, newContent, description) {
        if (!this.currentSessionId) {
            this.startSession();
        }
        const change = {
            id: `change_${++this.changeIdCounter}_${Date.now()}`,
            filePath,
            originalContent,
            newContent,
            timestamp: new Date(),
            description,
            applied: false
        };
        const session = this.sessions.get(this.currentSessionId);
        if (session) {
            session.changes.push(change);
        }
        return change;
    }
    async applyChange(changeId) {
        const change = this.findChange(changeId);
        if (!change || change.applied) {
            return false;
        }
        try {
            const uri = vscode.Uri.file(change.filePath);
            const buffer = Buffer.from(change.newContent, 'utf8');
            await vscode.workspace.fs.writeFile(uri, buffer);
            change.applied = true;
            // Open the file to show the change
            const document = await vscode.workspace.openTextDocument(uri);
            await vscode.window.showTextDocument(document);
            return true;
        }
        catch (error) {
            console.error('Error applying change:', error);
            return false;
        }
    }
    async revertChange(changeId) {
        const change = this.findChange(changeId);
        if (!change || !change.applied) {
            return false;
        }
        try {
            const uri = vscode.Uri.file(change.filePath);
            const buffer = Buffer.from(change.originalContent, 'utf8');
            await vscode.workspace.fs.writeFile(uri, buffer);
            change.applied = false;
            // Open the file to show the revert
            const document = await vscode.workspace.openTextDocument(uri);
            await vscode.window.showTextDocument(document);
            return true;
        }
        catch (error) {
            console.error('Error reverting change:', error);
            return false;
        }
    }
    async revertSession(sessionId) {
        const session = this.sessions.get(sessionId);
        if (!session) {
            return 0;
        }
        let revertedCount = 0;
        // Revert in reverse order
        for (let i = session.changes.length - 1; i >= 0; i--) {
            const change = session.changes[i];
            if (change.applied) {
                const success = await this.revertChange(change.id);
                if (success) {
                    revertedCount++;
                }
            }
        }
        return revertedCount;
    }
    findChange(changeId) {
        for (const session of this.sessions.values()) {
            const change = session.changes.find(c => c.id === changeId);
            if (change) {
                return change;
            }
        }
        return null;
    }
    getPendingChanges() {
        const session = this.getCurrentSession();
        if (!session) {
            return [];
        }
        return session.changes.filter(c => !c.applied);
    }
    getAppliedChanges() {
        const session = this.getCurrentSession();
        if (!session) {
            return [];
        }
        return session.changes.filter(c => c.applied);
    }
    getAllSessions() {
        return Array.from(this.sessions.values());
    }
    clearSession(sessionId) {
        if (sessionId) {
            this.sessions.delete(sessionId);
            if (this.currentSessionId === sessionId) {
                this.currentSessionId = null;
            }
        }
        else if (this.currentSessionId) {
            this.sessions.delete(this.currentSessionId);
            this.currentSessionId = null;
        }
    }
    endSession() {
        this.currentSessionId = null;
    }
}
exports.ChangeTracker = ChangeTracker;
//# sourceMappingURL=ChangeTracker.js.map