import { matchKeyword } from '../keyword-matcher/index.js';
const CRITERIA_LINE_REGEX = /^- \[[ x]\] (.+)$/;
const STOP_WORDS = new Set([
    'that',
    'this',
    'with',
    'from',
    'have',
    'been',
    'will',
    'should',
    'must',
    'when',
    'then',
    'each',
    'they',
    'their',
    'there',
    'where',
    'which',
    'while',
    'about',
    'after',
    'before',
    'below',
    'above',
    'given',
    'other',
    'more',
    'into',
    'only',
    'also',
    'both',
    'such',
    'some',
    'than',
    'over',
    'under',
]);
/**
 * Extract criteria lines from spec.md content.
 * Returns the text after the `- [ ] ` or `- [x] ` prefix.
 */
export function extractCriteriaLines(specMdContent) {
    const lines = specMdContent.split('\n');
    const result = [];
    for (const line of lines) {
        const match = CRITERIA_LINE_REGEX.exec(line.trimEnd());
        if (match !== null) {
            const text = match[1];
            if (text !== undefined && text.trim().length > 0) {
                result.push(text.trim());
            }
        }
    }
    return result;
}
/**
 * Extract meaningful keywords from a criterion string.
 * Words must be longer than 4 chars and not in the stop-words list.
 */
function extractKeywords(criterion) {
    return criterion
        .toLowerCase()
        .split(/\W+/)
        .filter((word) => word.length > 4 && !STOP_WORDS.has(word));
}
/**
 * Check if any export name or file path contains a given keyword.
 *
 * Decision: requireBoundary:true (not substring).
 * Reason: file paths like "src/author.ts" should NOT match the keyword "auth",
 * and "src/application.ts" should NOT match "app". Boundary matching prevents
 * these false positives while still correctly matching exact token occurrences
 * (e.g. "src/auth.ts" correctly matches "auth", "authService" matches "auth").
 * minWordLength:5 matches the existing extraction threshold (words >4 chars).
 */
function findKeywordInFiles(keyword, fileResults) {
    const matchOpts = { minWordLength: 5, requireBoundary: true };
    for (const file of fileResults) {
        if (!file.exists) {
            continue;
        }
        if (matchKeyword(file.path.toLowerCase(), keyword, matchOpts)) {
            return file.path;
        }
        for (const exportName of file.exportsFound) {
            if (matchKeyword(exportName.toLowerCase(), keyword, matchOpts)) {
                return file.path;
            }
        }
    }
    return null;
}
/**
 * Match each criterion against the scanned file results.
 * A criterion is "met" if at least one keyword is found in any file export or path.
 */
export function matchCriteria(criteriaLines, fileResults) {
    return criteriaLines.map((criterion) => {
        const keywords = extractKeywords(criterion);
        if (keywords.length === 0) {
            return { criterion, met: false, evidence: 'No match' };
        }
        for (const keyword of keywords) {
            const foundIn = findKeywordInFiles(keyword, fileResults);
            if (foundIn !== null) {
                return { criterion, met: true, evidence: `Found in ${foundIn}` };
            }
        }
        return { criterion, met: false, evidence: 'No match' };
    });
}
//# sourceMappingURL=criteria-matcher.js.map