import { ACTION_KEYWORDS, ACTION_PRIORITY, matchesKeyword } from '../../intents.js';
import { RESOURCE_ALIASES } from '../../synonyms.js';
export class IntentDetector {
    detect(query) {
        const lower = query.toLowerCase();
        // Action Detection
        let action = undefined;
        for (const actionType of ACTION_PRIORITY) {
            const keywords = ACTION_KEYWORDS[actionType];
            if (keywords.some(k => matchesKeyword(lower, k))) {
                action = actionType;
                break;
            }
        }
        // Resource Detection
        let resource = undefined;
        const potentialResources = [];
        for (const [resType, aliases] of Object.entries(RESOURCE_ALIASES)) {
            const typeName = resType.replace(/-/g, ' ');
            if (lower.includes(typeName)) {
                potentialResources.push({ type: resType, length: typeName.length });
            }
            for (const alias of aliases) {
                if (lower.includes(alias)) {
                    potentialResources.push({ type: resType, length: alias.length });
                }
            }
        }
        potentialResources.sort((a, b) => b.length - a.length);
        if (potentialResources.length > 0) {
            resource = potentialResources[0].type;
        }
        if (!action && resource) {
            action = 'list';
        }
        if (!resource && lower.includes('save work')) {
            resource = 'project';
        }
        if (['login', 'logout'].includes(action || '') && resource !== 'oauth') {
            resource = 'auth';
        }
        return { action, resource };
    }
}
