/**
 * Phase: structure
 *
 * Builds File and Folder nodes in the graph from scanned paths.
 *
 * @deps    scan
 * @reads   allPaths (from scan phase)
 * @writes  graph (File, Folder nodes + CONTAINS edges)
 */
import { getPhaseOutput } from './types.js';
import { processStructure } from '../structure-processor.js';
export const structurePhase = {
    name: 'structure',
    deps: ['scan'],
    async execute(ctx, deps) {
        const { scannedFiles, allPaths, totalFiles } = getPhaseOutput(deps, 'scan');
        ctx.onProgress({
            phase: 'structure',
            percent: 15,
            message: 'Analyzing project structure...',
            stats: { filesProcessed: 0, totalFiles, nodesCreated: ctx.graph.nodeCount },
        });
        const focusSet = ctx.options?.focusPaths
            ? new Set(ctx.options.focusPaths.map((p) => p.replace(/\\/g, '/')))
            : null;
        const graphPaths = focusSet ? allPaths.filter((p) => focusSet.has(p)) : allPaths;
        processStructure(ctx.graph, graphPaths);
        ctx.onProgress({
            phase: 'structure',
            percent: 20,
            message: 'Project structure analyzed',
            stats: {
                filesProcessed: graphPaths.length,
                totalFiles: focusSet ? graphPaths.length : totalFiles,
                nodesCreated: ctx.graph.nodeCount,
            },
        });
        // Build the set once here so cobol, markdown, and cross-file propagation
        // can all reuse it instead of re-materializing `new Set(allPaths)` each.
        const allPathSet = new Set(allPaths);
        const focusedScannedFiles = focusSet
            ? scannedFiles.filter((f) => focusSet.has(f.path))
            : scannedFiles;
        return {
            scannedFiles: focusedScannedFiles,
            allPaths,
            allPathSet,
            totalFiles: focusSet ? focusedScannedFiles.length : totalFiles,
        };
    },
};
