// src/engine/gherkin/parser.ts — Hand-rolled Gherkin .feature parser.
// Supports: Feature, Scenario, Scenario Outline + Examples, Given/When/Then/And/But.
const STEP_KEYWORDS = ['Given', 'When', 'Then', 'And', 'But'];
function isStepKeyword(word) {
    return STEP_KEYWORDS.includes(word);
}
function stripInlineComment(line) {
    const idx = line.indexOf('#');
    return idx >= 0 ? line.slice(0, idx) : line;
}
function trimLine(line) {
    return stripInlineComment(line).trim();
}
function parseExamplesTable(lines, start) {
    let i = start;
    const tableLines = [];
    while (i < lines.length) {
        const raw = trimLine(lines[i] ?? '');
        if (raw.startsWith('|')) {
            tableLines.push(raw);
            i++;
        }
        else if (raw === '' || raw.startsWith('#')) {
            i++;
        }
        else {
            break;
        }
    }
    const parseRow = (row) => row
        .split('|')
        .slice(1, -1)
        .map((cell) => cell.trim());
    const [headerRow, ...dataRows] = tableLines;
    const headers = headerRow ? parseRow(headerRow) : [];
    const rows = dataRows.map(parseRow);
    return { examples: { headers, rows }, nextIdx: i };
}
function parseScenario(lines, start) {
    const nameLine = trimLine(lines[start] ?? '');
    const colonIdx = nameLine.indexOf(':');
    const name = colonIdx >= 0 ? nameLine.slice(colonIdx + 1).trim() : nameLine;
    const steps = [];
    let examples;
    let i = start + 1;
    while (i < lines.length) {
        const raw = trimLine(lines[i] ?? '');
        if (raw === '' || raw.startsWith('#')) {
            i++;
            continue;
        }
        if (raw.startsWith('Scenario') || raw.startsWith('Feature') || raw.startsWith('Background')) {
            break;
        }
        if (raw.startsWith('Examples:') || raw.startsWith('Examples')) {
            i++;
            const result = parseExamplesTable(lines, i);
            examples = result.examples;
            i = result.nextIdx;
            continue;
        }
        const spaceIdx = raw.indexOf(' ');
        const keyword = spaceIdx >= 0 ? raw.slice(0, spaceIdx) : raw;
        const text = spaceIdx >= 0 ? raw.slice(spaceIdx + 1).trim() : '';
        if (isStepKeyword(keyword)) {
            steps.push({ keyword, text });
        }
        i++;
    }
    return { scenario: { name, steps, ...(examples ? { examples } : {}) }, nextIdx: i };
}
/**
 * Parse the contents of a .feature file into a GherkinFeature.
 * Handles: Feature, Scenario, Scenario Outline + Examples, Given/When/Then/And/But.
 */
export function parseGherkin(content) {
    const rawLines = content.split('\n');
    const lines = rawLines;
    let featureName = '';
    const scenarios = [];
    let i = 0;
    while (i < lines.length) {
        const raw = trimLine(lines[i] ?? '');
        if (raw.startsWith('Feature:')) {
            featureName = raw.slice('Feature:'.length).trim();
            i++;
            continue;
        }
        if (raw.startsWith('Scenario Outline:') ||
            raw.startsWith('Scenario:') ||
            raw.startsWith('Scenario ')) {
            const result = parseScenario(lines, i);
            scenarios.push(result.scenario);
            i = result.nextIdx;
            continue;
        }
        i++;
    }
    return { name: featureName, scenarios };
}
//# sourceMappingURL=parser.js.map