import path from 'node:path';
import { normalizePath } from './languages.js';
import { toNormalizedSymbolKind, } from './types.js';
function toRepoRelative(repoRoot, candidate) {
    return normalizePath(path.isAbsolute(candidate) ? path.relative(repoRoot, candidate) : candidate);
}
function packageRoot(specifier) {
    const segments = specifier.split('/');
    return segments.slice(0, specifier.startsWith('@') ? 2 : 1).join('/');
}
/**
 * Project an index down to the shape hosts typically feed to models or graph
 * views: per-file exported symbols, the local import graph, and external
 * dependency roots. Pure and synchronous — no filesystem access.
 */
export function buildCodeContext(index) {
    const files = [];
    const importGraph = {};
    for (const file of index.files) {
        const relativePath = toRepoRelative(index.repoRoot, file.relativePath !== '' ? file.relativePath : file.path);
        const exports = [];
        for (const symbol of file.symbols) {
            if (symbol.exported !== true)
                continue;
            const nodeId = symbol.nodeId;
            exports.push({
                name: symbol.name,
                kind: toNormalizedSymbolKind(symbol.type, symbol.containerName),
                type: symbol.type,
                line: symbol.line,
                ...(nodeId !== undefined ? { nodeId } : {}),
                ...(symbol.signature !== undefined ? { signature: symbol.signature } : {}),
                ...(symbol.fullSignature !== undefined ? { fullSignature: symbol.fullSignature } : {}),
                ...(symbol.returnType !== undefined ? { returnType: symbol.returnType } : {}),
                ...(symbol.jsdoc !== undefined ? { jsdoc: symbol.jsdoc } : {}),
                ...(symbol.containerName !== undefined ? { containerName: symbol.containerName } : {}),
            });
        }
        const localImports = [];
        const externalDeps = new Set();
        for (const entry of file.imports) {
            if (entry.kind === 'local' && typeof entry.resolvedPath === 'string') {
                localImports.push({
                    resolvedPath: toRepoRelative(index.repoRoot, entry.resolvedPath),
                    bindings: entry.bindings.map(binding => binding.localName),
                });
            }
            else if (entry.kind === 'external') {
                externalDeps.add(packageRoot(entry.specifier));
            }
        }
        files.push({
            relativePath,
            ...(file.language !== '' ? { language: file.language } : {}),
            exports,
            localImports,
            externalDeps: [...externalDeps],
        });
        if (localImports.length > 0) {
            // Value and type imports of one module arrive as separate records;
            // the graph view wants each dependency once.
            importGraph[relativePath] = [...new Set(localImports.map(entry => entry.resolvedPath))];
        }
    }
    return { files, importGraph };
}
//# sourceMappingURL=context.js.map