/**
 * Git Tool Adapters
 *
 * ITool-compliant adapter for GitTool operations.
 * This adapter wraps the existing GitTool methods to conform
 * to the formal ITool interface for use with the FormalToolRegistry.
 */
import { GitTool } from '../git-tool.js';
// ============================================================================
// Shared GitTool Instance
// ============================================================================
let gitInstance = null;
function getGit() {
    if (!gitInstance) {
        gitInstance = new GitTool();
    }
    return gitInstance;
}
/**
 * Reset the shared GitTool instance (for testing)
 */
export function resetGitInstance() {
    gitInstance = null;
}
// ============================================================================
// Valid operations
// ============================================================================
const VALID_OPERATIONS = [
    'status',
    'diff',
    'add',
    'commit',
    'push',
    'pull',
    'branch',
    'checkout',
    'stash',
    'auto_commit',
    'blame',
    'cherry_pick',
    'bisect_start',
    'bisect_step',
    'bisect_reset',
];
// ============================================================================
// GitOperationTool
// ============================================================================
/**
 * GitOperationTool - ITool adapter for Git operations
 *
 * Unified tool that handles all Git operations via an operation parameter.
 */
export class GitOperationTool {
    name = 'git';
    description = 'Execute Git version control operations: status, diff, add, commit, push, pull, branch, checkout, stash, blame, cherry-pick, bisect';
    async execute(input) {
        const operation = input.operation;
        const args = input.args || {};
        const git = getGit();
        switch (operation) {
            case 'status': {
                const status = await git.getStatus();
                return {
                    success: true,
                    output: git.formatStatus(status),
                    data: status,
                };
            }
            case 'diff':
                return {
                    success: true,
                    output: await git.getDiff(args.staged) || 'No differences',
                };
            case 'add':
                return await git.add(args.files || 'all');
            case 'commit':
                return await git.commit(args.message);
            case 'push':
                return await git.push();
            case 'pull':
                return await git.pull();
            case 'branch':
                return await git.branch(args.branch, args.delete);
            case 'checkout':
                return await git.checkout(args.branch, args.create);
            case 'stash':
                if (args.pop) {
                    return await git.stashPop();
                }
                return await git.stash(args.message);
            case 'auto_commit':
                return await git.autoCommit({
                    message: args.message,
                    push: args.push,
                });
            case 'blame':
                return await git.blame(args.file, {
                    startLine: args.start_line,
                    endLine: args.end_line,
                });
            case 'cherry_pick':
                return await git.cherryPick(args.commit, {
                    noCommit: args.no_commit,
                });
            case 'bisect_start':
                return await git.bisectStart(args.bad_ref, args.good_ref);
            case 'bisect_step':
                return await git.bisectStep(args.result);
            case 'bisect_reset':
                return await git.bisectReset();
            default:
                return {
                    success: false,
                    error: `Unknown git operation: ${operation}`,
                };
        }
    }
    getSchema() {
        return {
            name: this.name,
            description: this.description,
            parameters: {
                type: 'object',
                properties: {
                    operation: {
                        type: 'string',
                        description: 'Git operation to perform',
                        enum: [...VALID_OPERATIONS],
                    },
                    args: {
                        type: 'object',
                        description: 'Operation-specific arguments',
                    },
                },
                required: ['operation'],
            },
        };
    }
    validate(input) {
        if (typeof input !== 'object' || input === null) {
            return { valid: false, errors: ['Input must be an object'] };
        }
        const data = input;
        if (typeof data.operation !== 'string' || data.operation.trim() === '') {
            return { valid: false, errors: ['operation must be a non-empty string'] };
        }
        if (!VALID_OPERATIONS.includes(data.operation)) {
            return { valid: false, errors: [`Unknown operation: ${data.operation}`] };
        }
        // Validate required args for specific operations
        const args = data.args || {};
        switch (data.operation) {
            case 'commit':
                if (typeof args.message !== 'string' || args.message.trim() === '') {
                    return { valid: false, errors: ['commit requires args.message'] };
                }
                break;
            case 'blame':
                if (typeof args.file !== 'string' || args.file.trim() === '') {
                    return { valid: false, errors: ['blame requires args.file'] };
                }
                break;
            case 'cherry_pick':
                if (typeof args.commit !== 'string' || args.commit.trim() === '') {
                    return { valid: false, errors: ['cherry_pick requires args.commit'] };
                }
                break;
            case 'bisect_step':
                if (!['good', 'bad', 'skip'].includes(args.result)) {
                    return { valid: false, errors: ['bisect_step requires args.result to be "good", "bad", or "skip"'] };
                }
                break;
            case 'checkout':
                if (typeof args.branch !== 'string' || args.branch.trim() === '') {
                    return { valid: false, errors: ['checkout requires args.branch'] };
                }
                break;
        }
        return { valid: true };
    }
    getMetadata() {
        return {
            name: this.name,
            description: this.description,
            category: 'git',
            keywords: ['git', 'version-control', 'commit', 'branch', 'diff', 'blame', 'cherry-pick', 'bisect'],
            priority: 7,
            requiresConfirmation: true,
            modifiesFiles: false,
            makesNetworkRequests: false,
        };
    }
    isAvailable() {
        return true;
    }
    dispose() {
        resetGitInstance();
    }
}
// ============================================================================
// Factory Function
// ============================================================================
/**
 * Create all git tool instances
 */
export function createGitTools() {
    return [
        new GitOperationTool(),
    ];
}
//# sourceMappingURL=git-tools.js.map