// engine/ears-gate.ts — SPEC-631: Hard-block gate for vague modal verbs in acceptance criteria
// SPEC-724: Also checks for copulative verbs outside fenced blocks.
import { containsCopula } from './spec-format/copula-blacklist.js';
import { stripFencedBlocks } from './spec-format/text-fences.js';
const EARS_VAGUE_VERBS = ['should', 'may', 'could', 'might'];
/**
 * SPEC-724: Scan scenario text for copulative verbs outside fenced code blocks.
 * Returns a CopulaViolation if found, or null if the text is clean.
 */
export function checkCopulaViolation(scenarioText) {
    const stripped = stripFencedBlocks(scenarioText);
    const result = containsCopula(stripped, 'auto');
    if (!result.hit || result.verb === undefined) {
        return null;
    }
    // Find the offending line for context
    const lines = stripped.split('\n');
    const offendingLine = lines.find((l) => new RegExp(`\\b${result.verb}\\b`, 'i').test(l)) ?? '';
    return {
        code: 'BDD_COPULA_VIOLATION',
        severity: 'error',
        verb: result.verb,
        line: offendingLine.trim(),
    };
}
function stripLeadingBullet(line) {
    return line.replace(/^-\s*(\[[ x]\]\s*)?/i, '').trim();
}
function rewriteToGwt(text, vagueWord) {
    const withoutModal = text.replace(new RegExp(`\\b${vagueWord}\\s*`, 'gi'), '').trim();
    return `GIVEN <precondition> WHEN <trigger> THEN ${withoutModal}`;
}
/**
 * SPEC-631: Scan acceptance criteria lines for vague modal verbs.
 * Returns formatted blocker strings (one per violation) with original line and GWT rewrite.
 * Empty array = gate passed.
 */
export function runEarsGate(criteriaLines) {
    const blockers = [];
    for (const line of criteriaLines) {
        if (line === 'bdd-scenario') {
            continue;
        }
        const lower = line.toLowerCase();
        const found = EARS_VAGUE_VERBS.filter((verb) => new RegExp(`\\b${verb}\\b`).test(lower));
        if (found.length === 0) {
            continue;
        }
        const clean = stripLeadingBullet(line);
        const primaryVerb = found.at(0) ?? 'should';
        const suggestion = rewriteToGwt(clean, primaryVerb);
        blockers.push(`EARS gate: criterion uses vague verb "${found.join('", "')}" — must use imperative language.\n` +
            `  Original: "${clean.slice(0, 120)}"\n` +
            `  Suggested: ${suggestion}`);
    }
    return blockers;
}
//# sourceMappingURL=ears-gate.js.map