import { writeFileSync, readFileSync, mkdirSync, existsSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { logger } from "../utils/logger.js";
function writeJson(filePath, data) {
    const dir = dirname(filePath);
    if (!existsSync(dir)) {
        mkdirSync(dir, { recursive: true });
    }
    writeFileSync(filePath, JSON.stringify(data, null, 2), "utf-8");
    logger.info(`Wrote ${filePath}`);
}
export function writeOutput(dataDir, output) {
    const apiDir = resolve(dataDir, "api");
    const wikiDir = resolve(dataDir, "wiki");
    writeJson(resolve(apiDir, "enfusion-classes.json"), output.enfusionClasses);
    writeJson(resolve(apiDir, "arma-classes.json"), output.armaClasses);
    writeJson(resolve(apiDir, "hierarchy.json"), output.hierarchy);
    writeJson(resolve(apiDir, "groups.json"), output.groups);
    // Merge wiki pages: preserve existing BI wiki pages, replace only Doxygen-sourced pages
    const pagesPath = resolve(wikiDir, "pages.json");
    let existingPages = [];
    if (existsSync(pagesPath)) {
        try {
            existingPages = JSON.parse(readFileSync(pagesPath, "utf-8"));
        }
        catch {
            // Corrupted file — will be overwritten
        }
    }
    // Keep pages from sources NOT in the current scrape output
    const scrapedSources = new Set(output.wikiPages.map((p) => p.source));
    const preservedPages = existingPages.filter((p) => !scrapedSources.has(p.source));
    const mergedPages = [...preservedPages, ...output.wikiPages];
    writeJson(pagesPath, mergedPages);
    logger.info(`Scrape complete: ${output.enfusionClasses.length} enfusion classes, ${output.armaClasses.length} arma classes, ${output.hierarchy.length} hierarchy nodes, ${output.groups.length} groups, ${mergedPages.length} wiki pages (${output.wikiPages.length} from Doxygen + ${preservedPages.length} preserved)`);
}
//# sourceMappingURL=writer.js.map