// engine/provider-adapters/adapters/gemini.ts — SPEC-670: Gemini renderer.
//
// Emits GEMINI.md-style output:
//  • Header `# GEMINI.md — <title>`
//  • `## Function Declarations` block formatted per Google's JSON-schema-like
//    function-calling format (see https://ai.google.dev/gemini-api/docs/function-calling).
//  • BDD scenarios rewritten with explicit `Inputs:` / `Outputs:` H3 headers instead of
//    GIVEN/WHEN/THEN prose.
//
// API version pinned to v1beta (Gemini 1.5) via GEMINI_API_VERSION constant — bumped
// here when Google ships a stable v1.
import { estimateTokens } from '../shared/token-estimator.js';
import { bddToInputsOutputs } from '../shared/markdown-utils.js';
/**
 * Gemini API version targeted by this adapter. Pinned to v1beta (Gemini 1.5
 * function declarations). Update when Google ships a stable v1 with the same shape.
 */
export const GEMINI_API_VERSION = 'v1beta';
export class GeminiRenderer {
    kind = 'gemini';
    render(input) {
        const { loadedSpec } = input;
        const { spec, specBody, signatures, scenarios, warnings } = loadedSpec;
        const lines = [];
        lines.push(`# GEMINI.md — ${spec.title}`);
        lines.push('');
        lines.push(`> Gemini API target: \`${GEMINI_API_VERSION}\``);
        lines.push('');
        if (specBody.trim().length === 0) {
            lines.push(`_(spec body is empty — author has not yet filled in problem/criteria)_`);
        }
        else {
            // Drop the "## Acceptance Criteria" section — replaced by IO blocks below.
            lines.push(stripAcceptanceSection(specBody.trim()));
        }
        if (scenarios.length > 0) {
            lines.push('');
            lines.push('## Acceptance Criteria (Inputs/Outputs)');
            lines.push('');
            for (const sc of scenarios) {
                lines.push(bddToInputsOutputs(sc));
            }
        }
        lines.push('## Function Declarations');
        lines.push('');
        if (signatures.length === 0) {
            lines.push('_(no function declarations parsed from Technical section)_');
        }
        else {
            for (const sig of signatures) {
                lines.push(formatFunctionDeclaration(sig.name, sig.signature));
            }
        }
        lines.push('');
        const content = lines.join('\n').replace(/\n{3,}/g, '\n\n');
        const tokenEstimate = estimateTokens(content);
        return Promise.resolve({
            provider: 'gemini',
            content,
            tokenEstimate,
            extras: {
                apiVersion: GEMINI_API_VERSION,
                functionDeclarationCount: signatures.length,
                ...(warnings.length > 0 ? { warnings } : {}),
            },
        });
    }
}
/**
 * Format a single function declaration as a fenced JSON block following Google's
 * function-calling schema (best-effort — parameters are described as `string` since
 * we can't infer concrete types from a regex-extracted signature).
 */
function formatFunctionDeclaration(name, signature) {
    const declaration = {
        name,
        description: signature.replace(/\s+/g, ' ').trim(),
        parameters: {
            type: 'OBJECT',
            properties: {},
        },
    };
    return ['```json', JSON.stringify(declaration, null, 2), '```', ''].join('\n');
}
/** Drop the entire `## Acceptance Criteria` section so we can re-emit it as IO blocks. */
function stripAcceptanceSection(body) {
    const lines = body.split('\n');
    const startIdx = lines.findIndex((l) => /^##\s+Acceptance Criteria\b/i.test(l));
    if (startIdx === -1) {
        return body;
    }
    let endIdx = lines.length;
    for (let i = startIdx + 1; i < lines.length; i += 1) {
        if (/^##\s+/.test(lines[i] ?? '')) {
            endIdx = i;
            break;
        }
    }
    return [...lines.slice(0, startIdx), ...lines.slice(endIdx)].join('\n');
}
//# sourceMappingURL=gemini.js.map