"use strict";
// src/orchestrator/graph/GraphBuilder.ts
// v1.14.0 — Walk the codebase, run extractors, persist via GraphStore.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphBuilder = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const TreeSitterRegistry_1 = require("./TreeSitterRegistry");
const SymbolExtractor_1 = require("./SymbolExtractor");
const DependencyExtractor_1 = require("./DependencyExtractor");
const DEFAULT_IGNORE = new Set([
    'node_modules', 'dist', 'build', '.git', '.openlife',
    '.artifacts', '.catalog', '.cache', '.next', 'coverage',
    '__pycache__',
]);
const MAX_BYTES = 1024 * 1024;
function walk(root) {
    const out = [];
    function visit(dir) {
        let entries;
        try {
            entries = fs.readdirSync(dir, { withFileTypes: true });
        }
        catch {
            return;
        }
        for (const ent of entries) {
            if (ent.isDirectory()) {
                if (DEFAULT_IGNORE.has(ent.name))
                    continue;
                visit(path.join(dir, ent.name));
            }
            else if (ent.isFile()) {
                const full = path.join(dir, ent.name);
                if (!(0, TreeSitterRegistry_1.detectLanguage)(full))
                    continue;
                try {
                    if (fs.statSync(full).size > MAX_BYTES)
                        continue;
                }
                catch {
                    continue;
                }
                out.push(full);
            }
        }
    }
    visit(root);
    return out;
}
class GraphBuilder {
    build(root, store, onProgress) {
        const files = walk(path.resolve(root));
        const report = { scannedFiles: files.length, symbols: 0, edges: 0, failedFiles: [] };
        for (let i = 0; i < files.length; i++) {
            const file = files[i];
            onProgress?.(file, i + 1, files.length);
            try {
                const source = fs.readFileSync(file, 'utf-8');
                const syms = (0, SymbolExtractor_1.extractSymbols)(file, source);
                const edges = (0, DependencyExtractor_1.extractDependencies)(file, source);
                store.clearFile(file);
                store.putSymbols(syms);
                store.putEdges(edges);
                report.symbols += syms.length;
                report.edges += edges.length;
            }
            catch (err) {
                if ((process.env.OPENLIFE_DEBUG || '').toLowerCase() === 'true') {
                    console.warn(`[GRAPH] failed ${file}: ${err instanceof Error ? err.message : String(err)}`);
                }
                report.failedFiles.push(file);
            }
        }
        return report;
    }
}
exports.GraphBuilder = GraphBuilder;
