/**
 * Parse Slither JSON outputs for various printers and detectors.
 */
/**
 * Parse Slither's function-summary printer output.
 */
export function parseFunctionSummary(json) {
    const results = [];
    if (!json || typeof json !== 'object')
        return results;
    const data = json;
    const printers = data.results?.printers;
    if (!Array.isArray(printers))
        return results;
    for (const printer of printers) {
        const p = printer;
        if (p.printer !== 'function-summary')
            continue;
        const elements = p.elements;
        if (!Array.isArray(elements))
            continue;
        for (const elem of elements) {
            const e = elem;
            const rows = e.rows;
            if (!Array.isArray(rows))
                continue;
            for (const row of rows) {
                const r = row;
                results.push({
                    contract: String(r.contract ?? ''),
                    function: String(r.function ?? ''),
                    visibility: String(r.visibility ?? ''),
                    modifiers: parseStringList(r.modifiers),
                    readVars: parseStringList(r.read),
                    writeVars: parseStringList(r.write),
                    internalCalls: parseStringList(r.internal_calls),
                    externalCalls: parseStringList(r.external_calls),
                });
            }
        }
    }
    return results;
}
/**
 * Parse Slither detector results.
 */
export function parseDetectors(json) {
    if (!json || typeof json !== 'object')
        return [];
    const data = json;
    const results = data.results;
    const detectors = results?.detectors;
    if (!Array.isArray(detectors))
        return [];
    return detectors.map((d) => {
        const det = d;
        return {
            check: String(det.check ?? ''),
            impact: String(det.impact ?? ''),
            confidence: String(det.confidence ?? ''),
            description: String(det.description ?? ''),
            elements: parseElements(det.elements),
        };
    });
}
/**
 * Parse Slither data-dependency printer output.
 */
export function parseDataDependency(json) {
    const results = [];
    if (!json || typeof json !== 'object')
        return results;
    const data = json;
    const printers = data.results?.printers;
    if (!Array.isArray(printers))
        return results;
    for (const printer of printers) {
        const p = printer;
        if (p.printer !== 'data-dependency')
            continue;
        const elements = p.elements;
        if (!Array.isArray(elements))
            continue;
        for (const elem of elements) {
            const e = elem;
            results.push({
                contract: String(e.contract ?? ''),
                variable: String(e.variable ?? ''),
                dependencies: parseStringList(e.dependencies),
            });
        }
    }
    return results;
}
/**
 * Parse Slither call-graph printer output into caller→callee edges.
 */
export function parseCallGraph(json) {
    const edges = [];
    if (!json || typeof json !== 'object')
        return edges;
    const data = json;
    const printers = data.results?.printers;
    if (!Array.isArray(printers))
        return edges;
    for (const printer of printers) {
        const p = printer;
        if (p.printer !== 'call-graph')
            continue;
        const elements = p.elements;
        if (!Array.isArray(elements))
            continue;
        for (const elem of elements) {
            const e = elem;
            edges.push({
                caller: {
                    contract: String(e.caller?.contract ?? ''),
                    function: String(e.caller?.function ?? ''),
                },
                callee: {
                    contract: String(e.callee?.contract ?? ''),
                    function: String(e.callee?.function ?? ''),
                },
                isExternal: Boolean(e.is_external),
            });
        }
    }
    return edges;
}
function parseElements(elements) {
    if (!Array.isArray(elements))
        return [];
    return elements.map((e) => {
        const el = e;
        const sm = (el.source_mapping ?? {});
        return {
            type: String(el.type ?? ''),
            name: String(el.name ?? ''),
            source_mapping: {
                filename_relative: String(sm.filename_relative ?? ''),
                lines: Array.isArray(sm.lines) ? sm.lines : [],
                starting_column: Number(sm.starting_column ?? 0),
                ending_column: Number(sm.ending_column ?? 0),
            },
            type_specific_fields: el.type_specific_fields,
        };
    });
}
function parseStringList(val) {
    if (typeof val === 'string') {
        return val
            .split(',')
            .map((s) => s.trim())
            .filter(Boolean);
    }
    if (Array.isArray(val))
        return val.map(String);
    return [];
}
//# sourceMappingURL=slither.js.map