import { OAT_MARKER_PREFIX } from '../../engine/markers.js';
import YAML from 'yaml';
function normalizeBody(body) {
    return body.startsWith('\n') ? body.slice(1) : body;
}
function buildMarker(canonicalPath) {
    return `${OAT_MARKER_PREFIX} Source: ${canonicalPath} -->`;
}
export function renderMarkdownWithFrontmatter(frontmatter, body) {
    const normalizedBody = normalizeBody(body);
    if (!frontmatter || Object.keys(frontmatter).length === 0) {
        return normalizedBody;
    }
    // singleQuote keeps generated YAML frontmatter aligned with oxfmt/Prettier
    // formatting (singleQuote: true) so consumer repos don't see drift between
    // `oat sync` output and their markdown formatter on each commit.
    const frontmatterYaml = YAML.stringify(frontmatter, {
        singleQuote: true,
    }).trimEnd();
    return `---\n${frontmatterYaml}\n---\n\n${normalizedBody}`;
}
export function appendGeneratedMarker(content, canonicalPath) {
    if (!canonicalPath) {
        return content;
    }
    return `${content.trimEnd()}\n\n${buildMarker(canonicalPath)}\n`;
}
export function renderCanonicalRuleMarkdown(frontmatter, body) {
    const orderedFrontmatter = {
        ...(frontmatter.description !== undefined
            ? { description: frontmatter.description }
            : {}),
        ...(frontmatter.globs !== undefined ? { globs: frontmatter.globs } : {}),
        activation: frontmatter.activation,
    };
    return renderMarkdownWithFrontmatter(orderedFrontmatter, body);
}
