"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.GitOperations = void 0;
const simple_git_1 = require("simple-git");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class GitOperations {
    git;
    constructor(workingDirectory = process.cwd()) {
        this.git = (0, simple_git_1.simpleGit)(workingDirectory);
    }
    async getGitInfo() {
        try {
            // Check if we're in a git repository
            const isRepo = await this.isGitRepository();
            if (!isRepo) {
                return {
                    status: 'Not a git repository',
                    branch: 'none',
                    isGitRepo: false,
                };
            }
            // Get status and branch info
            const [status, branch] = await Promise.all([this.git.status(), this.git.branch()]);
            return {
                status: JSON.stringify({
                    modified: status.modified,
                    created: status.created,
                    deleted: status.deleted,
                    staged: status.staged,
                    not_added: status.not_added, // untracked files
                    ahead: status.ahead,
                    behind: status.behind,
                }),
                branch: branch.current,
                isGitRepo: true,
            };
        }
        catch (_error) {
            // Handle any git errors gracefully
            return {
                status: `Git error: ${_error instanceof Error ? _error.message : String(_error)}`,
                branch: 'error',
                isGitRepo: false,
            };
        }
    }
    async getCurrentBranch() {
        try {
            // First try using git command
            const branch = await this.git.branch();
            if (branch.current && branch.current.trim() !== '') {
                return branch.current;
            }
            // Fallback to reading .git/HEAD
            const gitHeadPath = path.join(process.cwd(), '.git', 'HEAD');
            if (fs.existsSync(gitHeadPath)) {
                const headContent = fs.readFileSync(gitHeadPath, 'utf8').trim();
                if (headContent.startsWith('ref: refs/heads/')) {
                    return headContent.replace('ref: refs/heads/', '');
                }
            }
            return null;
        }
        catch (_error) {
            return null;
        }
    }
    async safeCommit(message) {
        try {
            // Check if we're in a git repository
            const isRepo = await this.isGitRepository();
            if (!isRepo) {
                return {
                    success: false,
                    error: 'Not a git repository',
                };
            }
            // Check if there are changes to commit
            const status = await this.git.status();
            if (status.files.length === 0) {
                return {
                    success: false,
                    error: 'No changes to commit',
                };
            }
            // Add all changes and commit
            await this.git.add('.');
            const commitResult = await this.git.commit(message);
            return {
                success: true,
                commit: commitResult.commit,
            };
        }
        catch (_error) {
            return {
                success: false,
                error: _error instanceof Error ? _error.message : String(_error),
            };
        }
    }
    async isGitRepository() {
        try {
            await this.git.checkIsRepo();
            return true;
        }
        catch {
            return false;
        }
    }
}
exports.GitOperations = GitOperations;
