"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 });
const client_1 = require("../../../src/agents/tools/ycmd/client");
const serverManager_1 = require("../../../src/agents/tools/ycmd/serverManager");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
async function testTSServerWorkflow() {
    console.log('Testing TSServer workflow...');
    console.log('Getting server info from manager...');
    let serverInfo = serverManager_1.ycmdServerManager.getServerInfo();
    if (!serverInfo) {
        console.log('No server info found, checking if server is running...');
        const isRunning = await serverManager_1.ycmdServerManager.isRunning();
        console.log('Server running status:', isRunning);
        serverInfo = serverManager_1.ycmdServerManager.getServerInfo();
    }
    if (!serverInfo) {
        console.log('Still no server info, trying to start server...');
        serverInfo = await serverManager_1.ycmdServerManager.start(process.cwd());
    }
    console.log('Using server info:', {
        host: serverInfo.host,
        port: serverInfo.port,
        hasSecret: !!serverInfo.hmacSecret,
        status: serverInfo.status
    });
    const testFile = path.resolve(process.cwd(), 'src/agents/tools/ycmd/utils/pathUtils.ts');
    console.log('Testing file:', testFile);
    const fileContents = fs.readFileSync(testFile, 'utf8');
    const filetypes = ['typescript'];
    console.log('\n1. Creating YcmdClient...');
    const client = new client_1.YcmdClient(serverInfo);
    console.log('\n2. Checking if server is ready...');
    try {
        const isReady = await client.isReady();
        console.log('Server ready:', isReady);
        if (!isReady) {
            throw new Error('Server is not ready');
        }
    }
    catch (error) {
        console.error('Server readiness check failed:', error);
        return;
    }
    console.log('\n2.5. Opening tsconfig.json to help TSServer recognize project...');
    try {
        const tsconfigPath = path.resolve(process.cwd(), 'tsconfig.json');
        const tsconfigContents = fs.readFileSync(tsconfigPath, 'utf8');
        await client.notifyFileEvent('BufferVisit', tsconfigPath, tsconfigContents, ['json']);
        console.log('tsconfig.json BufferVisit successful');
        await new Promise(resolve => setTimeout(resolve, 1000));
    }
    catch (error) {
        console.error('tsconfig.json BufferVisit failed:', error);
    }
    console.log('\n3. Opening file via BufferVisit...');
    try {
        await client.notifyFileEvent('BufferVisit', testFile, fileContents, filetypes);
        console.log('BufferVisit successful');
    }
    catch (error) {
        console.error('BufferVisit failed:', error);
        return;
    }
    console.log('\n4. Getting diagnostics to ensure project is loaded...');
    try {
        const diagnostics = await client.getDiagnostics(testFile, fileContents, filetypes);
        console.log('Diagnostics response:', diagnostics);
    }
    catch (error) {
        console.error('Diagnostics failed:', error);
    }
    console.log('\n4.5. Opening file that references findProjectRoot...');
    try {
        const referencingFile = path.resolve(process.cwd(), 'src/agents/tools/ycmd/tools/start.ts');
        const referencingFileContents = fs.readFileSync(referencingFile, 'utf8');
        await client.notifyFileEvent('BufferVisit', referencingFile, referencingFileContents, filetypes);
        console.log('Referencing file BufferVisit successful');
        await new Promise(resolve => setTimeout(resolve, 500));
    }
    catch (error) {
        console.error('Referencing file BufferVisit failed:', error);
    }
    console.log('\n5. Testing GoToReferences for findProjectRoot function...');
    try {
        const references = await client.goToReferences(testFile, 40, 17, fileContents, filetypes);
        console.log('GoToReferences successful!');
        console.log('References found:', JSON.stringify(references, null, 2));
        if (references && references.length > 0) {
            console.log(`\n✅ SUCCESS: Found ${references.length} references for findProjectRoot`);
        }
        else {
            console.log('\n⚠️  WARNING: No references found, but no error occurred');
        }
    }
    catch (error) {
        console.error('GoToReferences failed:', error);
        return;
    }
    console.log('\n🎉 TSServer workflow test completed successfully!');
}
testTSServerWorkflow().catch(console.error).finally(() => {
    process.exit(0);
});
//# sourceMappingURL=test_tsserver_workflow.js.map