import { defineIntegration } from '@sentry/core';
import { app } from 'electron';

function gpuDeviceToGpuContext(device) {
    return {
        name: device.deviceString || 'GPU',
        active: device.active,
        id: `0x${device.deviceId.toString(16).padStart(4, '0')}`,
        vendor_id: `0x${device.vendorId.toString(16).padStart(4, '0')}`,
        vendor_name: device.vendorString,
        driver_version: device.driverVersion,
    };
}
/**
 * Adds GPU context to events
 */
const gpuContextIntegration = defineIntegration((options = { infoLevel: 'basic' }) => {
    let gpuContexts;
    return {
        name: 'GpuContext',
        processEvent: async (event) => {
            if (gpuContexts === undefined) {
                try {
                    const result = (await app.getGPUInfo(options.infoLevel));
                    gpuContexts = result.gpuDevice.map(gpuDeviceToGpuContext);
                }
                catch {
                    // getGPUInfo can throw on Linux when hardware acceleration is disabled (Electron v40+)
                    // In this case, we just don't add GPU context to events
                    gpuContexts = [];
                }
            }
            if (gpuContexts.length === 1) {
                event.contexts = { ...event.contexts, gpu: gpuContexts[0] };
            }
            else if (gpuContexts.length > 1) {
                event.contexts = { ...event.contexts };
                for (let i = 0; i < gpuContexts.length; i++) {
                    const gpuContext = gpuContexts[i];
                    gpuContext.type = 'gpu';
                    event.contexts[`gpu_${i + 1}`] = gpuContext;
                }
            }
            return event;
        },
    };
});

export { gpuContextIntegration };
//# sourceMappingURL=gpu-context.js.map
