import { extractPerSpecData } from './spec-data-extractor.js';
import { buildSpecHeaderSection, buildAffectedFilesSection, buildArchDecisionsSection, buildSpecFooterSection, buildImpactAnalysisSection, buildEstimationReasoningSection, buildLivingSpecSection, buildAcceptanceCriteriaSection, } from './spec-section-builders.js';
import { renderMermaidDiagrams, getMermaidScript } from './mermaid-renderer.js';
import { wrapPerSpecHtml } from './html-wrapper.js';
/**
 * Generate a self-contained HTML technical report for a single spec.
 * Includes: header, scope, diagrams, impact analysis, affected files,
 * architectural decisions, acceptance criteria, living spec, estimation reasoning.
 * All rich sections are conditionally rendered based on data availability.
 */
export function generatePerSpecTechnicalReport(spec, diagrams, affectedFiles, architecturalDecisions) {
    const data = extractPerSpecData(spec, affectedFiles, architecturalDecisions);
    const scopeInfo = buildScopeSection(data.scope, data.target, data.gitBranch);
    const diagramsSection = diagrams.length > 0
        ? `<div class="section"><h2>Diagrams</h2>${renderMermaidDiagrams(diagrams)}</div>`
        : '';
    const impactSection = data.impactAnalysis !== undefined ? buildImpactAnalysisSection(data.impactAnalysis) : '';
    const acSection = data.acceptanceCriteria !== undefined && data.acceptanceCriteria.length > 0
        ? buildAcceptanceCriteriaSection(data.acceptanceCriteria)
        : '';
    const livingSection = data.livingSpec !== undefined ? buildLivingSpecSection(data.livingSpec) : '';
    const reasoningSection = data.estimationReasoning !== undefined && data.estimationReasoning.length > 0
        ? buildEstimationReasoningSection(data.estimationReasoning)
        : '';
    const body = [
        buildSpecHeaderSection(data),
        scopeInfo,
        diagramsSection,
        impactSection,
        buildAffectedFilesSection(data.affectedFiles),
        buildArchDecisionsSection(data.architecturalDecisions),
        acSection,
        livingSection,
        reasoningSection,
        buildSpecFooterSection(),
    ]
        .filter(Boolean)
        .join('\n');
    const title = `Technical Report — ${data.specId}: ${data.title}`;
    const mermaidScript = diagrams.length > 0 ? getMermaidScript() : '';
    const content = wrapPerSpecHtml(title, body, mermaidScript);
    return {
        type: 'per-spec-technical',
        title,
        path: `technical-report.html`,
        content,
        audience: 'developer',
        sections: [],
        diagrams,
        relatedSpecs: [data.specId],
    };
}
function buildScopeSection(scope, target, gitBranch) {
    return `<div class="section">
  <h2>Scope</h2>
  <p><strong>Scope:</strong> ${escapeHtml(scope)} &middot; <strong>Target:</strong> ${escapeHtml(target)}</p>
  ${gitBranch ? `<p><strong>Branch:</strong> <code>${escapeHtml(gitBranch)}</code></p>` : ''}
</div>`;
}
function escapeHtml(str) {
    return str
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;');
}
//# sourceMappingURL=technical-report.js.map