// engine/provider-adapters/adapters/markdown.ts — SPEC-670: Generic markdown renderer.
//
// Writes a ≤ 800-token plain-markdown briefing to `planu/context.md`. Used by
// any LLM client without a dedicated adapter (Cursor, Windsurf, "unknown").
import { mkdir, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { MARKDOWN_MAX_TOKENS, MARKDOWN_TRUNCATE_MARKER, TOKENS_PER_CHAR, estimateTokens, } from '../shared/token-estimator.js';
import { stripNonEssentialSections } from '../shared/markdown-utils.js';
const CONTEXT_MD_RELATIVE_PATH = 'planu/context.md';
export class GenericMarkdownRenderer {
    kind = 'markdown';
    async render(input) {
        const { loadedSpec, projectPath } = input;
        const { spec, specBody, warnings } = loadedSpec;
        const title = spec.title || spec.id;
        let body;
        if (specBody.trim().length === 0) {
            body = `# ${title}\n\n_(spec body is empty — author has not yet filled in problem/criteria)_\n`;
        }
        else {
            const stripped = stripNonEssentialSections(specBody);
            body = `# ${title}\n\n${stripped}\n`;
            const cap = computeCharCap();
            if (body.length > cap) {
                body =
                    body.slice(0, Math.max(0, cap - MARKDOWN_TRUNCATE_MARKER.length)) +
                        MARKDOWN_TRUNCATE_MARKER;
            }
        }
        const filePath = join(projectPath, CONTEXT_MD_RELATIVE_PATH);
        await mkdir(dirname(filePath), { recursive: true });
        await writeFile(filePath, body, 'utf-8');
        const tokenEstimate = Math.min(estimateTokens(body), MARKDOWN_MAX_TOKENS);
        return {
            provider: 'markdown',
            content: body,
            tokenEstimate,
            filePath: CONTEXT_MD_RELATIVE_PATH,
            extras: warnings.length > 0 ? { warnings } : undefined,
        };
    }
}
/** Convert MARKDOWN_MAX_TOKENS to a character cap using the TOKENS_PER_CHAR heuristic. */
function computeCharCap() {
    // tokens = chars * TOKENS_PER_CHAR  →  chars = tokens / TOKENS_PER_CHAR
    return Math.floor(MARKDOWN_MAX_TOKENS / TOKENS_PER_CHAR);
}
//# sourceMappingURL=markdown.js.map