"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PredictiveService = void 0;
class PredictiveService {
    aiService;
    fileManager;
    constructor(aiService, fileManager) {
        this.aiService = aiService;
        this.fileManager = fileManager;
    }
    async predictNextCode(model, currentFile, cursorPosition) {
        const fileContent = await this.fileManager.readFile(currentFile);
        if (!fileContent) {
            throw new Error('Could not read current file');
        }
        const lines = fileContent.content.split('\n');
        const contextLines = lines.slice(Math.max(0, cursorPosition.line - 20), cursorPosition.line);
        const context = contextLines.join('\n');
        const messages = [
            {
                role: 'system',
                content: `You are a predictive coding engine. Analyze the current code context and predict what the developer will code next. Consider patterns, style, and logical flow.`
            },
            {
                role: 'user',
                content: `Based on this code context, predict what will be coded next:

\`\`\`${fileContent.language}
${context}
\`\`\`

Cursor position: Line ${cursorPosition.line + 1}, Column ${cursorPosition.character + 1}

Provide:
1. Predicted next code (5-20 lines)
2. Confidence level (0-100%)
3. Reasoning for the prediction
4. Alternative suggestions
5. Estimated time to implement`
            }
        ];
        const response = await this.aiService.chat(model, messages, 'ask', '');
        return this.parsePrediction(response.content);
    }
    parsePrediction(content) {
        const codeMatch = content.match(/```[\s\S]*?```/);
        const nextCode = codeMatch ? codeMatch[0].replace(/```\w*\n?/g, '').replace(/```/g, '').trim() : '';
        const confidenceMatch = content.match(/confidence[:\s]+(\d+)/i);
        const confidence = confidenceMatch ? parseInt(confidenceMatch[1]) / 100 : 0.7;
        const reasoningMatch = content.match(/reasoning[:\s]+([^\n]+(?:\n(?!\d+\.)[^\n]+)*)/i);
        const reasoning = reasoningMatch ? reasoningMatch[1].trim() : 'Based on code patterns and context';
        const suggestions = [];
        const suggestionMatches = content.match(/(?:alternative|suggestion)[s]?[:\s]+([^\n]+)/gi);
        if (suggestionMatches) {
            suggestions.push(...suggestionMatches.map(s => s.replace(/(?:alternative|suggestion)[s]?:?\s*/i, '')));
        }
        const timeMatch = content.match(/time[:\s]+([^\n]+)/i);
        const estimatedTime = timeMatch ? timeMatch[1].trim() : '5-10 minutes';
        return {
            nextCode: nextCode || content.substring(0, 500),
            confidence,
            reasoning,
            suggestions: suggestions.length > 0 ? suggestions : ['Continue current pattern', 'Add error handling', 'Implement next feature'],
            estimatedTime
        };
    }
}
exports.PredictiveService = PredictiveService;
//# sourceMappingURL=PredictiveService.js.map