"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildIoNetworkWakeup = buildIoNetworkWakeup;
exports.aggregateNetworkByEndpoint = aggregateNetworkByEndpoint;
/**
 * IO / Network / Wakelock / Wakeup Aggregator (Spark Plan 15)
 *
 * Combines four orthogonal attribution facets into one
 * `IoNetworkWakeupContract`. All facets optional; missing facets surface
 * as `unsupportedReason` rather than zero-fill.
 */
const sparkContracts_1 = require("../types/sparkContracts");
/**
 * "Has data" check that rejects both undefined and []. Codex review caught
 * that `!options.facet` is false for empty arrays, so an empty result set
 * was being marked as supported coverage rather than missing evidence.
 */
function hasRows(rows) {
    return Array.isArray(rows) && rows.length > 0;
}
function buildIoNetworkWakeup(options) {
    const hasIo = hasRows(options.ioEvents);
    const hasNet = hasRows(options.networkAttribution);
    const hasWakelock = hasRows(options.wakelockBaseline);
    const hasWakeup = hasRows(options.wakeupEdges);
    const allEmpty = !hasIo && !hasNet && !hasWakelock && !hasWakeup;
    return {
        ...(0, sparkContracts_1.makeSparkProvenance)({
            source: 'io-network-wakeup',
            ...(allEmpty ? { unsupportedReason: 'no IO / network / wakeup facets supplied' } : {}),
        }),
        range: options.range,
        ...(hasIo ? { ioEvents: options.ioEvents } : {}),
        ...(hasNet ? { networkAttribution: options.networkAttribution } : {}),
        ...(hasWakelock ? { wakelockBaseline: options.wakelockBaseline } : {}),
        ...(hasWakeup ? { wakeupEdges: options.wakeupEdges } : {}),
        coverage: [
            { sparkId: 15, planId: '15', status: hasIo ? 'implemented' : 'scaffolded' },
            { sparkId: 18, planId: '15', status: hasWakelock ? 'implemented' : 'scaffolded' },
            { sparkId: 20, planId: '15', status: hasWakeup ? 'implemented' : 'scaffolded' },
            { sparkId: 56, planId: '15', status: hasNet ? 'implemented' : 'scaffolded' },
        ],
    };
}
/**
 * Helper: aggregate network attribution rows by endpoint to surface
 * top hosts for diagnostic UIs.
 */
function aggregateNetworkByEndpoint(rows) {
    if (!rows || rows.length === 0)
        return [];
    const byEndpoint = new Map();
    for (const r of rows) {
        let entry = byEndpoint.get(r.endpoint);
        if (!entry) {
            entry = { endpoint: r.endpoint, totalDurNs: 0, totalBytes: 0, rowCount: 0 };
            byEndpoint.set(r.endpoint, entry);
        }
        entry.totalDurNs += r.durNs;
        entry.totalBytes += (r.bytesIn ?? 0) + (r.bytesOut ?? 0);
        entry.rowCount += 1;
    }
    return Array.from(byEndpoint.values()).sort((a, b) => b.totalDurNs - a.totalDurNs);
}
//# sourceMappingURL=ioNetworkWakeup.js.map