// engine/facilitate/intent-classifier.ts — SPEC-264 Request intent classifier
// ---------------------------------------------------------------------------
// Keyword patterns
// ---------------------------------------------------------------------------
const BUGFIX_RE = /\b(?:fix|bug|error|broken|crash|issue|fail|failing|exception|traceback)\b/i;
// Note: "what is" removed — handled by short-question path instead
const EXPLORATION_RE = /\b(?:explore|understand|how does|explain|learn|why does)\b/i;
const REFACTOR_RE = /\b(?:refactor|clean up|reorganize|restructure|simplify|rename|move)\b/i;
const ARCHITECTURAL_TERMS_RE = /\b(?:migrate|migration|replace|redesign|overhaul|rewrite|platform|framework)\b/i;
const COMPLEXITY_TERMS_PATTERN = '\\b(?:service|worker|queue|cache|database|schema|api|endpoints?|integration|auth|payment)\\b';
const TRIVIAL_THRESHOLD_WORDS = 15;
// Minute estimates per complexity
const MINUTES = {
    trivial: 5,
    simple: 30,
    feature: 120,
    architectural: 480,
};
// ---------------------------------------------------------------------------
// Classification helpers
// ---------------------------------------------------------------------------
function classifyType(request) {
    if (BUGFIX_RE.test(request)) {
        return 'bugfix';
    }
    // Exploration takes priority when explicit exploration keywords present
    if (EXPLORATION_RE.test(request)) {
        return 'exploration';
    }
    // Short question without exploration keywords → question
    const wordCount = request.trim().split(/\s+/).length;
    if (request.trim().endsWith('?') && wordCount <= 8) {
        return 'question';
    }
    if (REFACTOR_RE.test(request)) {
        return 'refactor';
    }
    return 'feature';
}
function countComplexTerms(request) {
    return (request.match(new RegExp(COMPLEXITY_TERMS_PATTERN, 'gi')) ?? []).length;
}
function classifyComplexity(request, type) {
    const words = request.trim().split(/\s+/);
    if (ARCHITECTURAL_TERMS_RE.test(request)) {
        return 'architectural';
    }
    if (type === 'exploration' || type === 'question') {
        return 'trivial';
    }
    const complexCount = countComplexTerms(request);
    if (words.length <= TRIVIAL_THRESHOLD_WORDS && complexCount === 0) {
        return 'trivial';
    }
    if (complexCount >= 2) {
        return 'feature';
    }
    if (complexCount >= 1) {
        return 'simple';
    }
    return 'feature';
}
function extractKeywords(request) {
    // Remove common stop words and keep meaningful terms
    const stopWords = new Set([
        'a',
        'an',
        'the',
        'and',
        'or',
        'but',
        'in',
        'on',
        'at',
        'to',
        'for',
        'of',
        'with',
        'by',
        'from',
        'is',
        'it',
        'i',
        'we',
        'my',
        'our',
        'this',
        'that',
        'be',
        'are',
        'was',
        'were',
        'will',
        'can',
        'do',
        'does',
        'how',
        'what',
        'why',
        'when',
        'where',
        'want',
        'need',
        'have',
        'would',
        'should',
        'could',
    ]);
    return request
        .toLowerCase()
        .replace(/[^\w\s]/g, ' ')
        .split(/\s+/)
        .filter((w) => w.length >= 3 && !stopWords.has(w))
        .slice(0, 10);
}
function computeConfidence(request, type) {
    // Higher confidence when multiple signals align
    const signals = [
        BUGFIX_RE.test(request) && type === 'bugfix',
        EXPLORATION_RE.test(request) && type === 'exploration',
        request.trim().endsWith('?') && type === 'question',
        REFACTOR_RE.test(request) && type === 'refactor',
        type === 'feature',
    ];
    const matching = signals.filter(Boolean).length;
    if (matching >= 2) {
        return 0.95;
    }
    if (matching === 1) {
        return 0.8;
    }
    return 0.6;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Classify a natural-language request into type, complexity, and keywords.
 */
export function classifyIntent(request) {
    const type = classifyType(request);
    const complexity = classifyComplexity(request, type);
    const keywords = extractKeywords(request);
    const confidence = computeConfidence(request, type);
    return {
        type,
        complexity,
        estimatedMinutes: MINUTES[complexity],
        keywords,
        confidence,
    };
}
//# sourceMappingURL=intent-classifier.js.map