// engine/ears/rewriter.ts — Template-driven EARS rewrite suggestions (SPEC-410)
// Simple regex-based extraction helpers
function extractNounPhrase(text) {
    // Strip leading action words and extract a subject noun phrase
    const stripped = text.replace(/^(the system shall|when|while|if|then|where)\s+/i, '').trim();
    // Take the first clause up to a comma, period, or conjunction
    const match = /^([^,.(]+?)(?:\s+(?:should|must|shall|will|can|is|are|has|have)|[,.(]|$)/i.exec(stripped);
    const raw = match?.[1]?.trim() ?? stripped.slice(0, 60).trim();
    return raw.length > 0 ? raw : 'the user performs an action';
}
function extractVerbPhrase(text) {
    // Look for a verb phrase after common trigger words
    const patterns = [
        /the system shall\s+(.+?)(?:\s+within\s+\d+|\s*$)/i,
        /(?:should|must|will)\s+(?:be able to\s+)?(.+?)(?:\s+within\s+\d+|\s*$)/i,
        /(?:displays?|shows?|returns?|navigates?\s+to|validates?|saves?|loads?|creates?|updates?|deletes?)\s+.+/i,
    ];
    for (const pattern of patterns) {
        const match = pattern.exec(text);
        if (match?.[1] !== undefined) {
            return match[1].trim().slice(0, 80);
        }
        if (match?.[0] !== undefined && pattern === patterns[2]) {
            return match[0].trim().slice(0, 80);
        }
    }
    // Fallback: strip leading qualifiers
    const stripped = text
        .replace(/^(the system shall|when|while|if|then|where|the user|user)\s+/i, '')
        .replace(/\s+(should|must|shall|will)\s+/, ' ')
        .trim();
    return stripped.slice(0, 80) || 'complete the requested operation';
}
/**
 * Generate 2 EARS-format rewrite suggestions using templates.
 * Template-driven only — no LLM calls.
 */
export function generateRewrites(criterion, _pattern) {
    const nounPhrase = extractNounPhrase(criterion);
    const verbPhrase = extractVerbPhrase(criterion);
    const suggestion1 = `WHEN ${nounPhrase}, THE SYSTEM SHALL ${verbPhrase}`;
    const suggestion2 = `WHEN ${nounPhrase}, THE SYSTEM SHALL ${verbPhrase} within 2 seconds`;
    return [suggestion1, suggestion2];
}
//# sourceMappingURL=rewriter.js.map