// Planu — Architecture, API Reference, and User Guide generators
import { generateArchitectureDiagram, generateDependencyDiagram } from './diagrams.js';
function buildArchOverviewSection(arch) {
    return {
        title: 'Architecture Overview',
        level: 2,
        content: [
            `This project follows the **${arch.primary}** architecture pattern.`,
            '',
            arch.secondary.length > 0 ? `Secondary patterns: ${arch.secondary.join(', ')}.` : '',
            '',
            `Communication patterns: ${arch.communicationPatterns.join(', ')}.`,
            '',
            `Deployment units: ${arch.deploymentUnits.join(', ')}.`,
        ]
            .filter(Boolean)
            .join('\n'),
    };
}
function buildLayersSection(arch) {
    return {
        title: 'Layers',
        level: 2,
        content: arch.layers
            .map((layer) => [
            `### ${layer.name}`,
            '',
            `**Responsibility:** ${layer.responsibility}`,
            '',
            `**Directories:** ${layer.directories.map((d) => `\`${d}\``).join(', ')}`,
            '',
            layer.dependsOn.length > 0
                ? `**Depends on:** ${layer.dependsOn.join(', ')}`
                : '**Depends on:** nothing (foundation layer)',
            '',
            layer.neverDependsOn.length > 0
                ? `**NEVER depends on:** ${layer.neverDependsOn.join(', ')}`
                : '',
        ]
            .filter(Boolean)
            .join('\n'))
            .join('\n\n'),
    };
}
export function generateArchitectureDoc(knowledge, _language) {
    const arch = knowledge.architecture;
    const sections = [buildArchOverviewSection(arch), buildLayersSection(arch)];
    if (arch.boundaries.length > 0) {
        sections.push({
            title: 'Module Boundaries',
            level: 2,
            content: arch.boundaries
                .map((b) => [
                `### ${b.name}`,
                '',
                `Directories: ${b.directories.map((d) => `\`${d}\``).join(', ')}`,
                '',
                b.publicApi.length > 0
                    ? `Public API: ${b.publicApi.map((a) => `\`${a}\``).join(', ')}`
                    : '',
            ]
                .filter(Boolean)
                .join('\n'))
                .join('\n\n'),
        });
    }
    const diagrams = [
        generateArchitectureDiagram(knowledge),
        generateDependencyDiagram(knowledge),
    ];
    sections.push({
        title: 'Architecture Diagram',
        level: 2,
        content: diagrams
            .map((d) => `### ${d.title}\n\n\`\`\`mermaid\n${d.content}\n\`\`\``)
            .join('\n\n'),
    });
    const content = `# Architecture Documentation\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'architecture-doc',
        title: 'Architecture Documentation',
        path: 'docs/architecture.md',
        content,
        audience: 'developer',
        sections,
        diagrams,
        relatedSpecs: [],
    };
}
export function generateApiReference(knowledge, specs, _language) {
    const sections = [];
    sections.push({
        title: 'API Overview',
        level: 2,
        content: [
            `This project uses ${knowledge.framework ?? knowledge.language} for its API layer.`,
            '',
            knowledge.apiContracts.length > 0
                ? `API contracts: ${knowledge.apiContracts.map((c) => c.type).join(', ')}.`
                : 'No formal API contracts detected. Consider adding OpenAPI/GraphQL schema.',
        ].join('\n'),
    });
    for (const contract of knowledge.apiContracts) {
        sections.push({
            title: `${contract.type.toUpperCase()} API`,
            level: 2,
            content: [
                `Type: ${contract.type}`,
                contract.path ? `Schema: \`${contract.path}\`` : '',
                '',
                contract.endpoints.length > 0
                    ? `Endpoints:\n${contract.endpoints.map((e) => `- ${e}`).join('\n')}`
                    : 'Endpoints: auto-detected from code.',
            ]
                .filter(Boolean)
                .join('\n'),
        });
    }
    sections.push({
        title: 'Authentication',
        level: 2,
        content: 'Document your authentication strategy here. Common patterns: JWT, API keys, OAuth 2.0, session-based.',
    });
    sections.push({
        title: 'Error Handling',
        level: 2,
        content: [
            'All API errors should follow a consistent format:',
            '',
            '```json',
            '{',
            '  "error": {',
            '    "code": "ERROR_CODE",',
            '    "message": "Human-readable message",',
            '    "details": {}',
            '  }',
            '}',
            '```',
        ].join('\n'),
    });
    const content = `# API Reference\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'api-reference',
        title: 'API Reference',
        path: 'docs/api-reference.md',
        content,
        audience: 'developer',
        sections,
        diagrams: [],
        relatedSpecs: specs
            .filter((s) => s.target === 'backend' || s.target === 'fullstack')
            .map((s) => s.id),
    };
}
export function generateUserGuide(knowledge, specs, _language) {
    const sections = [];
    const projectName = 
    /* v8 ignore next -- defensive nullish coalescing */
    knowledge.projectPath.split('/').pop() ?? 'Project';
    sections.push({
        title: `Welcome to ${projectName}`,
        level: 2,
        content: `This guide will help you get started with ${projectName}.`,
    });
    sections.push({
        title: 'Features',
        level: 2,
        content: specs.length > 0
            ? specs
                .filter((s) => s.status === 'done')
                .map((s) => `- **${s.title}**`)
                .join('\n')
            : 'Feature list will be populated from completed specs.',
    });
    sections.push({
        title: 'Getting Started',
        level: 2,
        content: 'Follow the setup guide to install and configure the application.',
    });
    const content = `# User Guide\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'user-guide',
        title: `${projectName} User Guide`,
        path: 'docs/user-guide.md',
        content,
        audience: 'end-user',
        sections,
        diagrams: [],
        relatedSpecs: specs.map((s) => s.id),
    };
}
//# sourceMappingURL=arch-api-user-generators.js.map