// src/engine/gherkin/scenario-to-criteria.ts — Convert GherkinFeature scenarios to spec criteria strings.
function stepsToText(steps) {
    return steps.map((s) => `${s.keyword} ${s.text}`).join('\n');
}
function interpolateRow(text, headers, row) {
    let result = text;
    for (let i = 0; i < headers.length; i++) {
        const header = headers[i];
        const value = row[i];
        if (header !== undefined && value !== undefined) {
            result = result.replaceAll(`<${header}>`, value);
        }
    }
    return result;
}
function scenarioOutlineToCriteria(scenario) {
    const { name, steps, examples } = scenario;
    if (!examples || examples.rows.length === 0) {
        return [scenarioToCriterion(scenario)];
    }
    return examples.rows.map((row) => {
        const interpolatedName = interpolateRow(name, examples.headers, row);
        const interpolatedSteps = steps.map((step) => ({
            keyword: step.keyword,
            text: interpolateRow(step.text, examples.headers, row),
        }));
        return buildCriterion(interpolatedName, interpolatedSteps);
    });
}
function buildCriterion(name, steps) {
    const header = name ? `Scenario: ${name}` : 'Scenario';
    return `${header}\n${stepsToText(steps)}`;
}
function scenarioToCriterion(scenario) {
    return buildCriterion(scenario.name, scenario.steps);
}
/**
 * Convert all scenarios in a GherkinFeature to spec criteria strings.
 * Each plain Scenario → one criterion.
 * Each Scenario Outline row → one interpolated criterion.
 */
export function scenariosToCriteria(feature) {
    const criteria = [];
    for (const scenario of feature.scenarios) {
        if (scenario.examples && scenario.examples.rows.length > 0) {
            criteria.push(...scenarioOutlineToCriteria(scenario));
        }
        else {
            criteria.push(scenarioToCriterion(scenario));
        }
    }
    return criteria;
}
//# sourceMappingURL=scenario-to-criteria.js.map