const core = require('@sentry/core');
const electron = require('electron');

/** Capture unhandled errors. */
const onUncaughtExceptionIntegration = core.defineIntegration(() => {
    return {
        name: 'OnUncaughtException',
        setup(client) {
            const options = client.getOptions();
            global.process.on('uncaughtException', (error) => {
                core.captureException(error, {
                    originalException: error,
                    captureContext: {
                        level: 'fatal',
                    },
                    data: {
                        mechanism: {
                            handled: false,
                            type: 'generic',
                        },
                    },
                });
                client.flush(options.shutdownTimeout || 2000).then(() => {
                    if (options?.onFatalError) {
                        options.onFatalError(error);
                    }
                    else if (global.process.listenerCount('uncaughtException') <= 2) {
                        // In addition to this handler there is always one in Electron
                        // The dialog is only shown if there are no other handlers
                        // eslint-disable-next-line no-console
                        console.error('Uncaught Exception:');
                        // eslint-disable-next-line no-console
                        console.error(error);
                        const ref = error.stack;
                        const stack = ref !== undefined ? ref : `${error.name}: ${error.message}`;
                        const message = `Uncaught Exception:\n${stack}`;
                        electron.dialog.showErrorBox('A JavaScript error occurred in the main process', message);
                    }
                }, () => {
                    // ignore
                });
            });
        },
    };
});

exports.onUncaughtExceptionIntegration = onUncaughtExceptionIntegration;//# sourceMappingURL=http://go/sourcemap/sourcemaps/042b3c1a4c53f2c3808067f519fbfc67b72cad80/node_modules/@sentry/electron/main/integrations/onuncaughtexception.js.map
