export class ChromeTraceExporter {
    static create(cpu, gpu, options = {}) {
        const profileName = options.profileName?.trim() || "Aura3D profile";
        const processId = positiveInteger(options.processId, 1);
        const cpuThreadId = positiveInteger(options.cpuThreadId, 1);
        const gpuThreadId = positiveInteger(options.gpuThreadId, 2);
        const events = [
            metadataEvent("process_name", processId, cpuThreadId, { name: "Aura3D" }),
            metadataEvent("thread_name", processId, cpuThreadId, { name: "CPU" }),
            metadataEvent("thread_name", processId, gpuThreadId, { name: "GPU" })
        ];
        for (const marker of cpu.markers) {
            events.push(cpuMarkerEvent(marker, processId, cpuThreadId));
        }
        if (gpu) {
            for (const sample of gpu.samples) {
                events.push(gpuSampleEvent(sample, processId, gpuThreadId));
            }
            if (gpu.supported === false) {
                events.push({
                    name: "gpu_timing_unavailable",
                    cat: "gpu",
                    ph: "i",
                    ts: 0,
                    pid: processId,
                    tid: gpuThreadId,
                    args: { reason: gpu.unavailableReason ?? "GPU timing unavailable" }
                });
            }
        }
        if (options.includeCounters !== false) {
            events.push({
                name: "profile_counters",
                cat: "counters",
                ph: "C",
                ts: 0,
                pid: processId,
                tid: cpuThreadId,
                args: {
                    cpuMarkerCount: cpu.markerCount,
                    cpuTotalDurationMs: fixed(cpu.totalDurationMs),
                    gpuSampleCount: gpu?.sampleCount ?? 0,
                    gpuSupported: gpu?.supported ?? false
                }
            });
        }
        return {
            traceEvents: events,
            displayTimeUnit: "ms",
            metadata: {
                source: "aura3d-debug-chrome-trace-exporter",
                profileName,
                cpuMarkerCount: cpu.markerCount,
                gpuSampleCount: gpu?.sampleCount ?? 0,
                claimBoundary: "Chrome trace export is bounded local profiling evidence; it is not Unity/Unreal profiler parity."
            }
        };
    }
    static toJson(trace) {
        return `${JSON.stringify(trace, null, 2)}\n`;
    }
}
function metadataEvent(name, pid, tid, args) {
    return {
        name,
        cat: "__metadata",
        ph: "M",
        ts: 0,
        pid,
        tid,
        args
    };
}
function cpuMarkerEvent(marker, pid, tid) {
    return {
        name: marker.name,
        cat: "cpu",
        ph: "X",
        ts: millisecondsToMicroseconds(marker.startMs),
        dur: millisecondsToMicroseconds(marker.durationMs),
        pid,
        tid,
        args: {
            depth: marker.depth,
            endMs: fixed(marker.endMs)
        }
    };
}
function gpuSampleEvent(sample, pid, tid) {
    return {
        name: sample.label,
        cat: "gpu",
        ph: "X",
        ts: 0,
        dur: millisecondsToMicroseconds(sample.durationMs),
        pid,
        tid,
        args: { durationMs: fixed(sample.durationMs) }
    };
}
function millisecondsToMicroseconds(value) {
    return Math.max(0, Math.round(value * 1_000));
}
function positiveInteger(value, fallback) {
    return Number.isInteger(value) && Number(value) > 0 ? Number(value) : fallback;
}
function fixed(value) {
    return Number(value.toFixed(4));
}
//# sourceMappingURL=ChromeTraceExporter.js.map