export class FallbackResolver {
    strategies;
    log;
    constructor(strategies, log) {
        this.strategies = strategies;
        this.log = log;
    }
    resolve(userContent, validAgentIds) {
        for (const strategy of this.strategies) {
            switch (strategy.type) {
                case 'pattern_match': {
                    const match = this.matchPatterns(userContent, strategy.patterns);
                    if (match && validAgentIds.includes(match)) {
                        this.log.info('Fallback resolved via pattern match', { agentId: match });
                        return {
                            agentId: match,
                            confidence: 0.5,
                            reasoning: `Pattern match fallback: matched "${match}"`,
                            routingSource: 'fallback',
                        };
                    }
                    break;
                }
                case 'fixed': {
                    if (validAgentIds.includes(strategy.agentId)) {
                        this.log.info('Fallback resolved via fixed strategy', { agentId: strategy.agentId });
                        return {
                            agentId: strategy.agentId,
                            confidence: 0,
                            reasoning: 'Fixed fallback strategy',
                            routingSource: 'fallback',
                        };
                    }
                    break;
                }
                case 'first_route': {
                    const first = validAgentIds[0];
                    if (first !== undefined) {
                        this.log.info('Fallback resolved via first_route strategy', { agentId: first });
                        return {
                            agentId: first,
                            confidence: 0,
                            reasoning: 'First route fallback strategy',
                            routingSource: 'fallback',
                        };
                    }
                    break;
                }
                default: {
                    const _exhaustive = strategy;
                    throw new Error(`Unhandled fallback strategy: ${_exhaustive.type}`);
                }
            }
        }
        this.log.error('All fallback strategies exhausted');
        return {
            agentId: validAgentIds[0] ?? '',
            confidence: 0,
            reasoning: 'All fallback strategies exhausted',
            routingSource: 'hard_fail',
        };
    }
    matchPatterns(userContent, patterns) {
        const lower = userContent.toLowerCase();
        let bestMatch = null;
        for (const [agentId, keywords] of patterns) {
            let score = 0;
            for (const keyword of keywords) {
                if (lower.includes(keyword.toLowerCase())) {
                    score++;
                }
            }
            if (score > 0 && (!bestMatch || score > bestMatch.score)) {
                bestMatch = { agentId, score };
            }
        }
        return bestMatch?.agentId ?? null;
    }
}
//# sourceMappingURL=fallback.js.map