// engine/provider-adapters/shared/markdown-utils.ts — SPEC-670: Markdown transforms shared by adapters.
/** Replace `<thinking>...</thinking>` blocks with `> Reasoning: ...` quote blocks. */
export function replaceThinkingBlocks(content) {
    return content.replace(/<thinking>([\s\S]*?)<\/thinking>/g, (_match, inner) => {
        const lines = inner
            .trim()
            .split('\n')
            .map((l) => `> Reasoning: ${l}`.trimEnd());
        return lines.join('\n');
    });
}
/**
 * Convert a single BDD scenario block (GIVEN/WHEN/THEN bullets) into Inputs:/Outputs: form
 * preferred by Gemini's tool-use prompts. The "Inputs" capture GIVEN+WHEN, "Outputs"
 * capture THEN.
 */
export function bddToInputsOutputs(scenario) {
    return [
        `### ${scenario.title}`,
        '',
        'Inputs:',
        `- GIVEN: ${scenario.given}`,
        `- WHEN: ${scenario.when}`,
        '',
        'Outputs:',
        `- THEN: ${scenario.then}`,
        '',
    ].join('\n');
}
/**
 * Strip non-essential sections from a spec body (Open questions, Dependencies,
 * Implementation order, Out of scope). Used by GenericMarkdownRenderer to fit the
 * 800-token budget.
 */
export function stripNonEssentialSections(body) {
    const dropHeadings = [
        /^#{2,4}\s+Open questions.*$/im,
        /^#{2,4}\s+Dependencies\b.*$/im,
        /^#{2,4}\s+Implementation order\b.*$/im,
        /^#{2,4}\s+Out of scope\b.*$/im,
        /^#{2,4}\s+Test stubs\b.*$/im,
        /^#{2,4}\s+Configuration constants\b.*$/im,
        /^#{2,4}\s+Definition of Done\b.*$/im,
    ];
    let result = body;
    for (const re of dropHeadings) {
        result = stripSection(result, re);
    }
    return result.trim();
}
/** Drop the section that begins at the first heading matching `re`, up to (but not including) the next heading of equal-or-higher level. */
function stripSection(body, re) {
    const lines = body.split('\n');
    const startIdx = lines.findIndex((l) => re.test(l));
    if (startIdx === -1) {
        return body;
    }
    const headingMatch = /^(#{2,6})\s/.exec(lines[startIdx] ?? '');
    if (!headingMatch?.[1]) {
        return body;
    }
    const startLevel = headingMatch[1].length;
    let endIdx = lines.length;
    for (let i = startIdx + 1; i < lines.length; i += 1) {
        const m = /^(#{2,6})\s/.exec(lines[i] ?? '');
        if (m?.[1] && m[1].length <= startLevel) {
            endIdx = i;
            break;
        }
    }
    return [...lines.slice(0, startIdx), ...lines.slice(endIdx)].join('\n');
}
/**
 * Best-effort regex extraction of top-level TS-like signatures.
 * Captures `export function|interface|type|class|const NAME ...`.
 *
 * For each hit, the "signature" is the matched line trimmed of trailing `{`.
 */
export function extractSignatures(body) {
    const re = /^export\s+(?:async\s+)?(?:function|interface|type|class|const)\s+([A-Za-z_$][\w$]*)([^\n{]*)/gm;
    const out = [];
    let match;
    while ((match = re.exec(body)) !== null) {
        const name = match[1] ?? '';
        const tail = (match[2] ?? '').replace(/\s*\{?\s*$/, '').trim();
        out.push({
            name,
            signature: (match[0].split('\n')[0] ?? '').replace(/\s*\{?\s*$/, '').trim(),
        });
        void tail;
    }
    return out;
}
//# sourceMappingURL=markdown-utils.js.map