/**
 * Digest build helper — resolves block sources + SHAs and drives digest-emitter.
 *
 * Runs outside compile() so the narrative (potentially LLM-backed) stays an
 * async post-process step and doesn't push async up through the core compiler.
 */
import { execSync } from 'node:child_process';
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { buildManifest } from '@duckcodeailabs/dql-core';
import { buildDigest, } from '@duckcodeailabs/dql-compiler';
export function isDigestOutput(output) {
    return Boolean(output.metadata?.narrative);
}
export async function runDigestBuild(output, projectRoot, llm) {
    const narrative = output.metadata?.narrative;
    if (!narrative) {
        throw new Error('runDigestBuild called on non-digest output');
    }
    const sources = loadBlockSources(projectRoot, narrative.sources);
    const digestIR = {
        title: output.metadata.title,
        narrative,
        // The digest-emitter only needs `title` + `narrative` + `sources` for
        // narrative composition; the dashboard HTML is threaded in separately.
        charts: [],
        filters: [],
        params: [],
        notifications: output.metadata.notifications ?? [],
        alerts: output.metadata.alerts ?? [],
        layout: { type: 'grid', columns: 12, items: [] },
        variables: {},
    };
    return buildDigest(digestIR, output.html, sources, llm);
}
function loadBlockSources(projectRoot, sourceNames) {
    const manifest = loadOrBuildManifest(projectRoot);
    const sources = new Map();
    for (const name of sourceNames) {
        const block = manifest?.blocks?.[name];
        if (!block) {
            sources.set(name, { path: name, description: undefined });
            continue;
        }
        sources.set(name, {
            path: block.filePath,
            description: block.description,
            gitCommitSha: resolveGitSha(projectRoot, block.filePath),
        });
    }
    return sources;
}
function loadOrBuildManifest(projectRoot) {
    const cached = join(projectRoot, 'dql-manifest.json');
    if (existsSync(cached)) {
        try {
            return JSON.parse(readFileSync(cached, 'utf-8'));
        }
        catch {
            // fall through to fresh build
        }
    }
    try {
        return buildManifest({ projectRoot, dqlVersion: '0.0.0' });
    }
    catch {
        return null;
    }
}
function resolveGitSha(projectRoot, filePath) {
    try {
        const sha = execSync(`git log -1 --format=%H -- "${filePath}"`, {
            cwd: projectRoot,
            encoding: 'utf-8',
            stdio: ['ignore', 'pipe', 'ignore'],
        }).trim();
        return sha.length > 0 ? sha : undefined;
    }
    catch {
        return undefined;
    }
}
//# sourceMappingURL=digest.js.map