// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
//   Copyright 2018-2021 Cruise LLC
//
//   This source code is licensed under the Apache License, Version 2.0,
//   found at http://www.apache.org/licenses/LICENSE-2.0
//   You may not use this file except in compliance with the License.
import { reportError } from "@foxglove/studio-base/reportError";
import { AppError } from "@foxglove/studio-base/util/errors";
import { inWebWorker } from "@foxglove/studio-base/util/workers";
const defaultNotificationHandler = (message, details, type, severity) => {
    if (inWebWorker()) {
        const webWorkerError = "Web Worker has uninitialized sendNotification function; this means this error message cannot show up in the UI (so we show it here in the console instead).";
        if (process.env.NODE_ENV === "test") {
            throw new Error(webWorkerError);
        }
        else {
            const consoleFn = severity === "error" ? console.error : severity === "warn" ? console.warn : console.info;
            consoleFn(webWorkerError, message, details, type);
        }
        return;
    }
    else if (process.env.NODE_ENV === "test") {
        return;
    }
    console.error("Notification before error display is mounted", message, details, type);
};
let addNotification = defaultNotificationHandler;
export function setNotificationHandler(handler) {
    if (addNotification !== defaultNotificationHandler) {
        throw new Error("Tried to overwrite existing NotificationHandler");
    }
    addNotification = handler;
}
export function unsetNotificationHandler() {
    if (addNotification === defaultNotificationHandler) {
        throw new Error("Tried to unset NotificationHandler but it was already the default");
    }
    addNotification = defaultNotificationHandler;
}
// Call this to add an notification to the application nav bar error component if mounted.
// If the component is not mounted, use the console as a fallback.
export default function sendNotification(message, details, type, severity) {
    // We only want to send non-user errors and warnings to Sentry
    if (type === "app") {
        if (severity === "warn" || severity === "error") {
            reportError(new AppError(details, message));
        }
    }
    addNotification(message, details, type, severity);
}
sendNotification.expectCalledDuringTest = () => {
    throw new Error("Should be overriden in setupTestFramework.ts");
};
//# sourceMappingURL=sendNotification.js.map