"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitIntegration = void 0;
const simple_git_1 = __importDefault(require("simple-git"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
class GitIntegration {
    constructor() {
        this.git = null;
        this.diffCache = new Map();
        this.cacheTimeout = 5000; // 5 seconds
    }
    async initialize(repoPath) {
        try {
            this.git = (0, simple_git_1.default)(repoPath);
            const isRepo = await this.git.checkIsRepo();
            if (!isRepo) {
                console.log('Not a git repository, git features disabled');
                this.git = null;
                return false;
            }
            console.log('Git integration initialized');
            return true;
        }
        catch (error) {
            console.error('Error initializing git:', error);
            this.git = null;
            return false;
        }
    }
    async getFileDiff(filePath) {
        if (!this.git) {
            return null;
        }
        // Check cache
        const cached = this.diffCache.get(filePath);
        if (cached && Date.now() - cached.timestamp < this.cacheTimeout) {
            return cached.diff;
        }
        try {
            // Check if file exists in git
            const status = await this.git.status();
            const relativePath = path.relative(await this.getRepoRoot(), filePath);
            // File is untracked
            if (status.not_added.includes(relativePath)) {
                const content = fs.readFileSync(filePath, 'utf-8');
                const diff = `New file (not tracked in git)\n\n${content}`;
                this.diffCache.set(filePath, { diff, timestamp: Date.now() });
                return diff;
            }
            // Get diff for modified files
            if (status.modified.includes(relativePath) || status.created.includes(relativePath)) {
                const diff = await this.git.diff([relativePath]);
                this.diffCache.set(filePath, { diff, timestamp: Date.now() });
                return diff;
            }
            // File is tracked but unchanged
            return null;
        }
        catch (error) {
            console.error(`Error getting diff for ${filePath}:`, error);
            return null;
        }
    }
    async getRecentChanges() {
        if (!this.git) {
            return [];
        }
        try {
            const status = await this.git.status();
            const repoRoot = await this.getRepoRoot();
            const changed = [
                ...status.modified,
                ...status.created,
                ...status.not_added
            ].map(file => path.join(repoRoot, file));
            return changed;
        }
        catch (error) {
            console.error('Error getting recent changes:', error);
            return [];
        }
    }
    async getRepoRoot() {
        if (!this.git) {
            throw new Error('Git not initialized');
        }
        try {
            const root = await this.git.revparse(['--show-toplevel']);
            return root.trim();
        }
        catch (error) {
            throw new Error('Failed to get repo root');
        }
    }
    clearCache() {
        this.diffCache.clear();
    }
    isInitialized() {
        return this.git !== null;
    }
}
exports.GitIntegration = GitIntegration;
//# sourceMappingURL=GitIntegration.js.map