import { METRIC_FIELDS, formatPayloadJson, hasKey } from '../utils.js';
import { formatStructureJson } from './tree.js';
export function formatJson(stats, sccData = null, structureResults, options = {}) {
    const { columns } = stats;
    const mapRow = (r) => {
        const entry = {
            type: r.fileType,
            ...(r.fileName ? { name: r.fileName } : {}),
            ...(r.filePath ? { path: r.filePath } : {}),
            count: r.files,
        };
        for (const f of METRIC_FIELDS) {
            if (r[hasKey(f)])
                entry[f] = r[f] || 0;
        }
        entry.size = r.size;
        if (stats.showConfidence && r.confidence) {
            const conf = {};
            for (const f of METRIC_FIELDS) {
                if (r[hasKey(f)] && r.confidence[f])
                    conf[f] = r.confidence[f];
            }
            if (Object.keys(conf).length > 0)
                entry.confidence = conf;
        }
        return entry;
    };
    const mapTotals = (t) => {
        const entry = { files: t.files };
        for (const f of METRIC_FIELDS) {
            if (columns[hasKey(f)])
                entry[f] = t[f];
        }
        entry.size = t.size;
        return entry;
    };
    const output = {
        documents: {
            files: stats.rows.map(mapRow),
            totals: mapTotals(stats.totals),
        },
        skippedFiles: options.skippedFiles ?? 0,
        diagnostics: options.diagnostics ?? [],
        // Null is distinct from an scc run that legitimately returned no rows.
        code: sccData,
    };
    if (structureResults && structureResults.length > 0) {
        output.structures = formatStructureJson(structureResults);
    }
    return formatPayloadJson(output, options.pretty);
}
//# sourceMappingURL=json.js.map