/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { ensureDir } from '../../file-writer.js';
const OUTPUT_DIR = '.setup-agents';
const OUTPUT_FILE = 'sf-metadata-index.md';
export function renderMarkdown(cwd, index) {
    const outputDir = join(cwd, OUTPUT_DIR);
    ensureDir(outputDir);
    const lines = [];
    lines.push('# Salesforce Metadata Index');
    lines.push('');
    lines.push(`Generated: ${index.generatedAt}`);
    lines.push(`Mode: ${index.mode} | Source: ${index.sourceRoot}`);
    lines.push(`Total entries: ${index.stats.totalEntries} | Dependencies: ${index.stats.totalDependencies} | Orphans: ${index.stats.orphanEntries}`);
    lines.push('');
    // Stats by type
    lines.push('## Summary by Type');
    lines.push('');
    lines.push('| Type | Count |');
    lines.push('|------|-------|');
    for (const [type, count] of Object.entries(index.stats.byType)) {
        if (count > 0) {
            lines.push(`| ${type} | ${count} |`);
        }
    }
    lines.push('');
    // Entries grouped by type
    const types = [...new Set(index.entries.map((e) => e.type))];
    for (const type of types) {
        const typeEntries = index.entries.filter((e) => e.type === type);
        lines.push(`## ${type} (${typeEntries.length})`);
        lines.push('');
        lines.push('| API Name | Category | Dependencies | Dependents |');
        lines.push('|----------|----------|--------------|------------|');
        for (const entry of typeEntries) {
            const deps = entry.dependencies.length > 0 ? entry.dependencies.slice(0, 5).join(', ') : '-';
            const depents = entry.dependents && entry.dependents.length > 0 ? entry.dependents.slice(0, 5).join(', ') : '-';
            lines.push(`| ${entry.apiName} | ${entry.category ?? '-'} | ${deps} | ${depents} |`);
        }
        lines.push('');
    }
    const outputPath = join(outputDir, OUTPUT_FILE);
    writeFileSync(outputPath, lines.join('\n') + '\n', 'utf8');
    return outputPath;
}
//# sourceMappingURL=markdown-renderer.js.map