import { promisify } from 'node:util';
import { hashText } from '../cache.js';
import { redactGraphText } from '../query.js';
export async function extractGitGraph(args) {
    try {
        const childProcess = await import('node:child_process');
        if (typeof childProcess.execFile !== 'function') {
            return { nodes: [], edges: [] };
        }
        const execFileAsync = promisify(childProcess.execFile);
        const { stdout } = await execFileAsync('git', ['log', '--max-count=20', '--pretty=format:%H%x09%s'], { cwd: args.projectPath });
        const nodes = [];
        const edges = [];
        for (const line of stdout.split('\n').filter(Boolean)) {
            const [sha, ...messageParts] = line.split('\t');
            if (sha === undefined) {
                continue;
            }
            const message = messageParts.join('\t');
            const commitId = `commit:${sha.slice(0, 12)}`;
            nodes.push({
                id: commitId,
                type: 'commit',
                label: redactGraphText(message, args.policy),
                source: 'git-extractor',
                evidence: [{ path: '.git/log', selector: sha }],
                updatedAt: new Date().toISOString(),
            });
            const specId = /SPEC-\d+/i.exec(message)?.[0]?.toUpperCase();
            if (specId !== undefined) {
                edges.push({
                    id: `edge:${hashText(`${commitId}:references:${specId}`)}`,
                    from: commitId,
                    to: `spec:${specId}`,
                    type: 'references',
                    source: 'git-extractor',
                    confidence: 'medium',
                    classification: 'inferred',
                    evidence: { path: '.git/log', selector: sha },
                    updatedAt: new Date().toISOString(),
                });
            }
        }
        return { nodes, edges };
    }
    catch {
        return { nodes: [], edges: [] };
    }
}
export function extractReleaseGraph(source, policy) {
    const nodes = [];
    const edges = [];
    for (const line of source.content.split('\n').filter(Boolean).slice(-20)) {
        try {
            const item = JSON.parse(line);
            if (typeof item.version !== 'string') {
                continue;
            }
            const releaseId = `release:${item.version}`;
            nodes.push({
                id: releaseId,
                type: 'release',
                label: redactGraphText(item.version, policy),
                source: 'git-extractor',
                evidence: [{ path: source.path, hash: source.hash }],
                metadata: { commitSha: item.commitSha ?? null, timestamp: item.timestamp ?? null },
                updatedAt: new Date().toISOString(),
            });
            if (typeof item.commitSha === 'string') {
                edges.push({
                    id: `edge:${hashText(`${releaseId}:released_in:${item.commitSha}`)}`,
                    from: `commit:${item.commitSha.slice(0, 12)}`,
                    to: releaseId,
                    type: 'released_in',
                    source: 'git-extractor',
                    confidence: 'medium',
                    classification: 'extracted',
                    evidence: { path: source.path, hash: source.hash },
                    updatedAt: new Date().toISOString(),
                });
            }
        }
        catch {
            continue;
        }
    }
    return { nodes, edges };
}
//# sourceMappingURL=git-extractor.js.map