"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NextActionPredictor = void 0;
class NextActionPredictor {
    transitionMatrix = new Map();
    actionHistory = [];
    feedbackScores = new Map();
    commonSequences = [
        ['model', 'validation', 'routes', 'tests'],
        ['component', 'styles', 'tests'],
        ['hook', 'component', 'tests'],
        ['interface', 'implementation', 'tests'],
        ['schema', 'migration', 'seed'],
        ['api_route', 'service', 'repository', 'tests'],
    ];
    recordAction(action) {
        if (this.actionHistory.length > 0) {
            const prev = this.actionHistory[this.actionHistory.length - 1];
            if (!this.transitionMatrix.has(prev)) {
                this.transitionMatrix.set(prev, new Map());
            }
            const counts = this.transitionMatrix.get(prev);
            counts.set(action, (counts.get(action) || 0) + 1);
        }
        this.actionHistory.push(action);
        if (this.actionHistory.length > 100) {
            this.actionHistory = this.actionHistory.slice(-50);
        }
    }
    predictNext(patterns) {
        const predictions = [];
        if (this.actionHistory.length === 0) {
            return this.getDefaultSuggestions(patterns);
        }
        const lastAction = this.actionHistory[this.actionHistory.length - 1];
        const transitions = this.transitionMatrix.get(lastAction);
        if (transitions && transitions.size > 0) {
            const total = Array.from(transitions.values()).reduce((a, b) => a + b, 0);
            for (const [action, count] of transitions) {
                const prob = count / total;
                if (prob >= 0.15) {
                    predictions.push({
                        action,
                        description: this.describeAction(action),
                        confidence: Math.round(prob * 100) / 100,
                        isBulk: false,
                    });
                }
            }
        }
        for (const sequence of this.commonSequences) {
            const idx = sequence.indexOf(lastAction);
            if (idx >= 0 && idx < sequence.length - 1) {
                const nextInSeq = sequence[idx + 1];
                if (!predictions.some((p) => p.action === nextInSeq)) {
                    predictions.push({
                        action: nextInSeq,
                        description: this.describeAction(nextInSeq),
                        confidence: 0.5,
                        isBulk: false,
                    });
                }
            }
        }
        predictions.sort((a, b) => b.confidence - a.confidence);
        return predictions.slice(0, 5);
    }
    getBulkSuggestion(predictions) {
        if (predictions.length >= 2) {
            const actions = predictions.map((p) => p.action).join(' → ');
            return {
                action: 'generate_all',
                description: `Generate All: ${actions}`,
                confidence: predictions.length > 0 ? Math.round(predictions.reduce((s, p) => s + p.confidence, 0) / predictions.length * 100) / 100 : 0,
                isBulk: true,
            };
        }
        return null;
    }
    recordFeedback(action, wasUseful) {
        if (!this.feedbackScores.has(action)) {
            this.feedbackScores.set(action, []);
        }
        this.feedbackScores.get(action).push(wasUseful ? 1 : 0);
    }
    getFeedbackScore(action) {
        const scores = this.feedbackScores.get(action);
        if (!scores || scores.length === 0)
            return 0;
        return scores.reduce((a, b) => a + b, 0) / scores.length;
    }
    getStats() {
        let transitions = 0;
        for (const [, counts] of this.transitionMatrix) {
            transitions += counts.size;
        }
        return {
            transitions,
            sequences: this.commonSequences.length,
            feedbackEntries: Array.from(this.feedbackScores.values()).reduce((a, b) => a + b.length, 0),
        };
    }
    getDefaultSuggestions(pattern) {
        const suggestions = [
            { action: 'model', description: 'Create data model', confidence: 0.6, isBulk: false },
            { action: 'component', description: 'Create UI component', confidence: 0.5, isBulk: false },
            { action: 'tests', description: 'Write tests', confidence: 0.55, isBulk: false },
        ];
        if (pattern.some((p) => p.category === 'variable_declaration')) {
            suggestions.push({ action: 'validation', description: 'Add input validation', confidence: 0.45, isBulk: false });
        }
        return suggestions;
    }
    describeAction(action) {
        const descriptions = {
            model: 'Create data model',
            validation: 'Add input validation',
            routes: 'Define API routes',
            tests: 'Write tests',
            component: 'Create UI component',
            styles: 'Add component styles',
            hook: 'Create custom hook',
            interface: 'Define TypeScript interface',
            implementation: 'Implement the interface',
            schema: 'Define database schema',
            migration: 'Create database migration',
            seed: 'Add seed data',
            api_route: 'Create API route handler',
            service: 'Create service layer',
            repository: 'Create data repository',
        };
        return descriptions[action] || action;
    }
}
exports.NextActionPredictor = NextActionPredictor;
//# sourceMappingURL=next-action-predictor.js.map