"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ycmdRefactor = ycmdRefactor;
const serverManager_1 = require("../serverManager");
async function ycmdRefactor(params) {
    try {
        if (!params.filepath) {
            return {
                success: false,
                message: 'filepath is required'
            };
        }
        if (typeof params.line !== 'number' || typeof params.column !== 'number') {
            return {
                success: false,
                message: 'line and column must be numbers'
            };
        }
        if (!['rename', 'extract_method', 'organize_imports', 'fix_it'].includes(params.command)) {
            return {
                success: false,
                message: 'command must be one of: rename, extract_method, organize_imports, fix_it'
            };
        }
        if (params.command === 'rename' && !params.newName) {
            return {
                success: false,
                message: 'newName is required for rename operations'
            };
        }
        if (params.command === 'fix_it' && typeof params.fixitIndex !== 'number') {
            return {
                success: false,
                message: 'fixitIndex is required for fix_it operations'
            };
        }
        const setupResult = await serverManager_1.ycmdServerManager.setupClientAndNotifyFile({
            filepath: params.filepath,
            fileContents: params.contents
        });
        if (!setupResult.success) {
            return {
                success: false,
                message: setupResult.message
            };
        }
        const { client, resolvedFilePath, contents, filetypes } = setupResult;
        let response;
        switch (params.command) {
            case 'rename':
                response = await client.refactorRename(resolvedFilePath, params.line, params.column, contents, filetypes, params.newName);
                break;
            case 'extract_method':
                response = await client.refactorExtractMethod(resolvedFilePath, params.line, params.column, contents, filetypes);
                break;
            case 'organize_imports':
                response = await client.refactorOrganizeImports(resolvedFilePath, params.line, params.column, contents, filetypes);
                break;
            case 'fix_it':
                response = await client.refactorFixIt(resolvedFilePath, params.line, params.column, contents, filetypes, params.fixitIndex);
                break;
        }
        let result;
        if (response && response.fixits && response.fixits.length > 0) {
            const fixit = response.fixits[0];
            result = {
                edits: fixit.chunks.map((chunk) => ({
                    filepath: resolvedFilePath,
                    range: {
                        start: {
                            line: chunk.range.start.line_num,
                            column: chunk.range.start.column_num
                        },
                        end: {
                            line: chunk.range.end.line_num,
                            column: chunk.range.end.column_num
                        }
                    },
                    newText: chunk.replacement_text
                })),
                description: fixit.text
            };
        }
        else if (response && response.chunks) {
            result = {
                edits: response.chunks.map((chunk) => ({
                    filepath: resolvedFilePath,
                    range: {
                        start: {
                            line: chunk.range.start.line_num,
                            column: chunk.range.start.column_num
                        },
                        end: {
                            line: chunk.range.end.line_num,
                            column: chunk.range.end.column_num
                        }
                    },
                    newText: chunk.replacement_text
                })),
                description: response.description
            };
        }
        else {
            return {
                success: false,
                message: `No refactoring available for ${params.command} at this location`
            };
        }
        return {
            success: true,
            result,
            message: `Successfully generated ${params.command} refactoring with ${result.edits.length} edit${result.edits.length === 1 ? '' : 's'}`
        };
    }
    catch (error) {
        return {
            success: false,
            message: `Failed to execute ${params.command}: ${error.message}`
        };
    }
}
//# sourceMappingURL=refactor.js.map