"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.matchPhaseHintForNextPhase = matchPhaseHintForNextPhase;
/**
 * Resolve the hint to inject into the response of a phase transition.
 * Returns `undefined` when no hint is applicable (e.g. agent's phase
 * matches no keywords AND every critical hint has already been covered).
 */
function matchPhaseHintForNextPhase(input) {
    const { hints, nextPhase, finishedPhases } = input;
    if (hints.length === 0)
        return undefined;
    const phaseName = nextPhase.name.toLowerCase();
    const phaseGoal = (nextPhase.goal ?? '').toLowerCase();
    const keywordWeight = (keyword) => {
        const normalized = keyword.trim();
        if (normalized.length <= 1)
            return 0;
        if (normalized.length <= 2)
            return 4;
        return 6 + Math.min(normalized.length, 12);
    };
    const scoreHint = (hint) => {
        let score = 0;
        for (const keyword of hint.keywords) {
            const kw = keyword.toLowerCase();
            if (!kw)
                continue;
            const weight = keywordWeight(kw);
            if (phaseName.includes(kw))
                score += weight * 5;
            if (phaseGoal.includes(kw))
                score += weight;
        }
        return score;
    };
    const keywordMatches = hints
        .map((hint, index) => ({ hint, index, score: scoreHint(hint) }))
        .filter(item => item.score > 0)
        .sort((a, b) => b.score - a.score || a.index - b.index);
    if (keywordMatches.length > 0)
        return keywordMatches[0].hint;
    return undefined;
}
//# sourceMappingURL=phaseHintMatcher.js.map