"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.discoverYcmdCommands = discoverYcmdCommands;
const client_1 = require("../../../src/agents/tools/ycmd/client");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const serverManager_1 = require("src/agents/tools/ycmd/serverManager");
async function discoverYcmdCommands() {
    console.log("🔍 Discovering available ycmd commands...\n");
    const workspaceRoot = process.cwd();
    const testDir = path.join(workspaceRoot, "tests", "ycmd", "fixtures");
    if (!fs.existsSync(testDir)) {
        fs.mkdirSync(testDir, { recursive: true });
    }
    const testFile = path.join(testDir, "test_discovery.ts");
    const testContent = `interface User {
    name: string;
    age: number;
}

function createUser(name: string, age: number): User {
    return { name, age };
}

const user = createUser("Alice", 30);
console.log(user.name);
`;
    fs.writeFileSync(testFile, testContent);
    const { client } = await serverManager_1.ycmdServerManager.setupClientAndNotifyFile({
        filepath: testFile,
        fileContents: testContent,
    });
    try {
        console.log("\n📋 Testing command availability...\n");
        const filetypes = (0, client_1.getFileTypes)(testFile);
        console.log("1. Testing RefactorRename...");
        try {
            const renameResult = await client.refactorRename(testFile, 6, 10, testContent, filetypes, "buildUser");
            console.log("✅ RefactorRename is available");
            console.log("Response:", JSON.stringify(renameResult, null, 2));
        }
        catch (error) {
            console.log("❌ RefactorRename failed:", error.message);
        }
        console.log("\n2. Testing RefactorExtractMethod...");
        try {
            const extractResult = await client.refactorExtractMethod(testFile, 7, 5, testContent, filetypes);
            console.log("✅ RefactorExtractMethod is available");
            console.log("Response:", JSON.stringify(extractResult, null, 2));
        }
        catch (error) {
            console.log("❌ RefactorExtractMethod failed:", error.message);
        }
        console.log("\n3. Testing RefactorOrganizeImports...");
        try {
            const organizeResult = await client.refactorOrganizeImports(testFile, 1, 1, testContent, filetypes);
            console.log("✅ RefactorOrganizeImports is available");
            console.log("Response:", JSON.stringify(organizeResult, null, 2));
        }
        catch (error) {
            console.log("❌ RefactorOrganizeImports failed:", error.message);
        }
        console.log("\n5. Testing Format...");
        try {
            const formatResult = await client.request("/run_completer_command", {
                command_arguments: ["Format"],
                filepath: testFile,
                line_num: 1,
                column_num: 1,
                file_data: {
                    [testFile]: {
                        contents: testContent,
                        filetypes,
                    },
                },
            });
            console.log("✅ Format is available");
            console.log("Response:", JSON.stringify(formatResult, null, 2));
        }
        catch (error) {
            console.log("❌ Format failed:", error.message);
        }
        console.log("\n6. Testing GetType...");
        try {
            const typeResult = await client.getSignatureHelp(testFile, 9, 7, testContent, filetypes);
            console.log("✅ GetType is available");
            console.log("Response:", JSON.stringify(typeResult, null, 2));
        }
        catch (error) {
            console.log("❌ GetType failed:", error.message);
        }
        console.log("\n7. Testing GoToReferences...");
        try {
            const referencesResult = await client.goToReferences(testFile, 6, 10, testContent, filetypes);
            console.log("✅ GoToReferences is available");
            console.log("Response:", JSON.stringify(referencesResult, null, 2));
        }
        catch (error) {
            console.log("❌ GoToReferences failed:", error.message);
        }
        console.log("\n8. Testing GoToDefinition...");
        try {
            const definitionResult = await client.goToDefinition(testFile, 9, 15, testContent, filetypes);
            console.log("✅ GoToDefinition is available");
            console.log("Response:", JSON.stringify(definitionResult, null, 2));
        }
        catch (error) {
            console.log("❌ GoToDefinition failed:", error.message);
        }
        console.log("\n9. Testing GoToDeclaration...");
        try {
            const declarationResult = await client.goToDeclaration(testFile, 9, 15, testContent, filetypes);
            console.log("✅ GoToDeclaration is available");
            console.log("Response:", JSON.stringify(declarationResult, null, 2));
        }
        catch (error) {
            console.log("❌ GoToDeclaration failed:", error.message);
        }
        console.log("\n10. Testing GoToImplementation...");
        try {
            const implementationResult = await client.request("/run_completer_command", {
                command_arguments: ["GoToImplementation"],
                filepath: testFile,
                line_num: 9,
                column_num: 15,
                file_data: {
                    [testFile]: {
                        contents: testContent,
                        filetypes,
                    },
                },
            });
            console.log("✅ GoToImplementation is available");
            console.log("Response:", JSON.stringify(implementationResult, null, 2));
        }
        catch (error) {
            console.log("❌ GoToImplementation failed:", error.message);
        }
        console.log("\n11. Testing Completions...");
        try {
            const completionRequest = {
                filepath: testFile,
                line_num: 10,
                column_num: 15,
                file_data: {
                    [testFile]: {
                        contents: testContent,
                        filetypes,
                    },
                },
            };
            const completionResult = await client.getCompletions(completionRequest);
            console.log("✅ Completions is available");
            console.log(`Found ${completionResult.completions.length} completions`);
            const firstFew = completionResult.completions.slice(0, 3);
            firstFew.forEach((comp, i) => {
                console.log(`   ${i + 1}. ${comp.insertion_text} (${comp.kind})`);
            });
        }
        catch (error) {
            console.log("❌ Completions failed:", error.message);
        }
        console.log("\n12. Testing Diagnostics...");
        try {
            const diagnostics = await client.getDiagnostics(testFile, testContent, filetypes);
            console.log("✅ Diagnostics is available");
            console.log(`Found ${diagnostics.length} diagnostics`);
            diagnostics.forEach((diag, i) => {
                console.log(`   ${i + 1}. Line ${diag.location.line_num}: ${diag.text} (${diag.kind})`);
                if (diag.fixit_available) {
                    console.log(`      - Fixit available!`);
                }
            });
        }
        catch (error) {
            console.log("❌ Diagnostics failed:", error.message);
        }
        console.log("\n13. Testing GetRefactorCommands...");
        try {
            const commands = await client.getRefactorCommands(testFile, 6, 10, testContent, filetypes);
            console.log("✅ GetRefactorCommands is available");
            console.log(`Found ${commands.length} available commands:`);
            commands.forEach((cmd, i) => {
                console.log(`   ${i + 1}. ${cmd}`);
            });
        }
        catch (error) {
            console.log("❌ GetRefactorCommands failed:", error.message);
        }
        console.log("\n14. Testing Shutdown...");
        try {
            const shutdownResult = await client.shutdown();
            console.log("✅ Shutdown is available");
            console.log("Response:", JSON.stringify(shutdownResult, null, 2));
        }
        catch (error) {
            console.log("❌ Shutdown failed:", error.message);
        }
    }
    catch (error) {
        console.error("❌ Error during command discovery:", error);
    }
    try {
        fs.unlinkSync(testFile);
        console.log("✅ Test file cleaned up");
    }
    catch (error) {
        console.log("⚠️ Could not clean up test file");
    }
    console.log("\n🏁 Command discovery complete!");
    process.exit(0);
}
if (require.main === module) {
    discoverYcmdCommands().catch((error) => {
        console.error("Command discovery failed:", error);
        process.exit(1);
    });
}
//# sourceMappingURL=test_discover_commands.js.map