// technical-enricher/index.ts — Agnostic enricher + disk runner (SPEC-696)
import { readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { parseTechnicalMd } from './parse-technical.js';
import { detectSkip } from './skip-detector.js';
import { extractSignatures } from './signature-extractor.js';
import { scanTestPattern } from './test-pattern-scanner.js';
import { renderEnriched } from './render-enriched.js';
export async function enrichTechnicalContent(input) {
    const { technicalContent, specScope, projectPath } = input;
    const parsed = parseTechnicalMd(technicalContent);
    const skipReason = detectSkip(parsed, specScope);
    if (skipReason) {
        return { enriched: false, reason: skipReason };
    }
    const allSourceFiles = [...parsed.createFiles, ...parsed.modifyFiles];
    const signaturesPromises = allSourceFiles
        .filter((f) => !f.startsWith('tests/'))
        .slice(0, 6)
        .map((f) => extractSignatures(projectPath, f));
    const signatures = await Promise.all(signaturesPromises);
    const testSample = parsed.testFiles[0]
        ? await scanTestPattern(projectPath, parsed.testFiles[0])
        : null;
    const enrichedContent = renderEnriched({
        originalContent: technicalContent,
        signatures,
        testSample,
    });
    return { enriched: true, reason: 'ok', enrichedContent };
}
export async function runTechnicalEnricher(opts) {
    // SPEC-752: Remove technical.md-first branch — unified spec.md is the SOLE source of truth.
    // SPEC-630: Always read from the `## Technical` section in unified spec.md.
    // Any stale technical.md on disk is ignored here and cleaned up by runSsrBackMigration.
    await runEnricherFromSpecSection(opts);
}
async function runEnricherFromSpecSection(opts) {
    const { glob } = await import('glob');
    const specPattern = join(opts.projectPath, 'planu/specs', `${opts.specId}-*`, 'spec.md');
    const [specPath] = await glob(specPattern);
    if (!specPath) {
        return;
    }
    const specRaw = await readFile(specPath, 'utf8');
    const sectionMatch = /\n## Technical\n([\s\S]*)$/.exec(specRaw);
    if (!sectionMatch?.[1]) {
        return;
    }
    const result = await enrichTechnicalContent({
        specId: opts.specId,
        projectPath: opts.projectPath,
        technicalContent: sectionMatch[1],
        specScope: opts.specScope,
    });
    if (result.enriched && result.enrichedContent) {
        const updated = specRaw.replace(/\n## Technical\n[\s\S]*$/, `\n## Technical\n\n${result.enrichedContent}`);
        await writeFile(specPath, updated, 'utf8');
    }
}
//# sourceMappingURL=index.js.map