import { readFileSync, existsSync } from "node:fs";
import { resolve } from "node:path";
import { logger } from "../utils/logger.js";
function loadJson(filePath, fallback) {
    if (!existsSync(filePath)) {
        logger.warn(`Index file not found: ${filePath}`);
        return fallback;
    }
    try {
        const raw = readFileSync(filePath, "utf-8");
        return JSON.parse(raw);
    }
    catch (e) {
        logger.error(`Failed to parse index file ${filePath}: ${e}`);
        return fallback;
    }
}
export function loadIndex(dataDir) {
    const apiDir = resolve(dataDir, "api");
    const wikiDir = resolve(dataDir, "wiki");
    const data = {
        enfusionClasses: loadJson(resolve(apiDir, "enfusion-classes.json"), []),
        armaClasses: loadJson(resolve(apiDir, "arma-classes.json"), []),
        hierarchy: loadJson(resolve(apiDir, "hierarchy.json"), []),
        groups: loadJson(resolve(apiDir, "groups.json"), []),
        wikiPages: loadJson(resolve(wikiDir, "pages.json"), []),
    };
    const total = data.enfusionClasses.length + data.armaClasses.length;
    logger.info(`Loaded index: ${data.enfusionClasses.length} enfusion + ${data.armaClasses.length} arma classes (${total} total), ${data.groups.length} groups, ${data.wikiPages.length} wiki pages`);
    return data;
}
//# sourceMappingURL=loader.js.map