// src/engine/gherkin/exporter.ts — Export spec BDD criteria as a valid Cucumber-compatible .feature file.
const BDD_KEYWORDS = ['Given', 'When', 'Then', 'And', 'But'];
function isBddKeyword(word) {
    return BDD_KEYWORDS.includes(word);
}
function isBddCriterion(criterion) {
    const firstLine = criterion.trim().split('\n')[0] ?? '';
    return BDD_KEYWORDS.some((kw) => firstLine.startsWith(kw) || firstLine.startsWith(`Scenario`));
}
function formatScenario(criterion, index) {
    const lines = criterion.trim().split('\n');
    const firstLine = lines[0] ?? '';
    // Determine scenario name
    let scenarioName;
    let stepLines;
    if (firstLine.startsWith('Scenario:')) {
        scenarioName = firstLine.slice('Scenario:'.length).trim();
        stepLines = lines.slice(1);
    }
    else if (firstLine.startsWith('Scenario')) {
        scenarioName = firstLine.slice('Scenario'.length).trim();
        stepLines = lines.slice(1);
    }
    else {
        // criterion starts directly with a step keyword
        scenarioName = `Scenario ${index + 1}`;
        stepLines = lines;
    }
    const formattedSteps = stepLines
        .map((line) => line.trim())
        .filter((line) => line.length > 0)
        .map((line) => {
        const spaceIdx = line.indexOf(' ');
        const keyword = spaceIdx >= 0 ? line.slice(0, spaceIdx) : line;
        return isBddKeyword(keyword) ? `    ${line}` : `    ${line}`;
    });
    const nameLabel = scenarioName
        ? `  Scenario: ${scenarioName}`
        : `  Scenario: Scenario ${index + 1}`;
    return [nameLabel, ...formattedSteps].join('\n');
}
/**
 * Generate a valid Cucumber-compatible .feature file from spec criteria.
 * Only criteria that follow BDD (Given/When/Then) patterns are included.
 * Non-BDD criteria are skipped.
 */
export function exportCriteriaAsFeature(specTitle, criteria) {
    const bddCriteria = criteria.filter(isBddCriterion);
    if (bddCriteria.length === 0) {
        return `Feature: ${specTitle}\n\n  # No BDD criteria found\n`;
    }
    const scenarioBlocks = bddCriteria.map((c, i) => formatScenario(c, i));
    return `Feature: ${specTitle}\n\n${scenarioBlocks.join('\n\n')}\n`;
}
//# sourceMappingURL=exporter.js.map