1"use strict";
2/**
3 * Copyright (c) 650 Industries.
4 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10let LogBox;
11/**
12 * LogBox displays logs in the app.
13 */
14if (__DEV__) {
15    const LogBoxData = require('./Data/LogBoxData');
16    const { parseLogBoxLog, parseInterpolation } = require('./Data/parseLogBoxLog');
17    let originalConsoleError;
18    let consoleErrorImpl;
19    let isLogBoxInstalled = false;
20    LogBox = {
21        install() {
22            if (isLogBoxInstalled) {
23                return;
24            }
25            isLogBoxInstalled = true;
26            // Trigger lazy initialization of module.
27            // require("../NativeModules/specs/NativeLogBox");
28            // IMPORTANT: we only overwrite `console.error` and `console.warn` once.
29            // When we uninstall we keep the same reference and only change its
30            // internal implementation
31            const isFirstInstall = originalConsoleError == null;
32            if (isFirstInstall) {
33                originalConsoleError = console.error.bind(console);
34                console.error = (...args) => {
35                    consoleErrorImpl?.(...args);
36                };
37            }
38            consoleErrorImpl = registerError;
39            if (process.env.NODE_ENV === 'test') {
40                LogBoxData.setDisabled(true);
41            }
42        },
43        uninstall() {
44            if (!isLogBoxInstalled) {
45                return;
46            }
47            isLogBoxInstalled = false;
48            // IMPORTANT: we don't re-assign to `console` in case the method has been
49            // decorated again after installing LogBox. E.g.:
50            // Before uninstalling: original > LogBox > OtherErrorHandler
51            // After uninstalling:  original > LogBox (noop) > OtherErrorHandler
52            consoleErrorImpl = originalConsoleError;
53            delete console.disableLogBox;
54        },
55        isInstalled() {
56            return isLogBoxInstalled;
57        },
58        ignoreLogs(patterns) {
59            LogBoxData.addIgnorePatterns(patterns);
60        },
61        ignoreAllLogs(value) {
62            LogBoxData.setDisabled(value == null ? true : value);
63        },
64        clearAllLogs() {
65            LogBoxData.clear();
66        },
67        addLog(log) {
68            if (isLogBoxInstalled) {
69                LogBoxData.addLog(log);
70            }
71        },
72        addException(error) {
73            if (isLogBoxInstalled) {
74                LogBoxData.addException(error);
75            }
76        },
77    };
78    const isWarningModuleWarning = (...args) => {
79        return typeof args[0] === 'string' && args[0].startsWith('Warning: ');
80    };
81    const registerError = (...args) => {
82        // Let errors within LogBox itself fall through.
83        if (LogBoxData.isLogBoxErrorMessage(args[0])) {
84            originalConsoleError?.(...args);
85            return;
86        }
87        try {
88            if (!isWarningModuleWarning(...args)) {
89                // Only show LogBox for the 'warning' module, otherwise pass through.
90                // By passing through, this will get picked up by the React console override,
91                // potentially adding the component stack. React then passes it back to the
92                // React Native ExceptionsManager, which reports it to LogBox as an error.
93                //
94                // The 'warning' module needs to be handled here because React internally calls
95                // `console.error('Warning: ')` with the component stack already included.
96                originalConsoleError?.(...args);
97                return;
98            }
99            const { category, message, componentStack } = parseLogBoxLog(args);
100            if (!LogBoxData.isMessageIgnored(message.content)) {
101                // Interpolate the message so they are formatted for adb and other CLIs.
102                // This is different than the message.content above because it includes component stacks.
103                const interpolated = parseInterpolation(args);
104                originalConsoleError?.(interpolated.message.content);
105                LogBoxData.addLog({
106                    // Always show the static rendering issues as full screen since they
107                    // are too confusing otherwise.
108                    level: /did not match\. Server:/.test(message.content) ? 'fatal' : 'error',
109                    category,
110                    message,
111                    componentStack,
112                });
113            }
114        }
115        catch (err) {
116            LogBoxData.reportUnexpectedLogBoxError(err);
117        }
118    };
119}
120else {
121    LogBox = {
122        install() { },
123        uninstall() { },
124        isInstalled() {
125            return false;
126        },
127        ignoreLogs(patterns) { },
128        ignoreAllLogs(value) { },
129        clearAllLogs() { },
130        addLog(log) { },
131        addException(ex) { },
132    };
133}
134exports.default = LogBox;
135//# sourceMappingURL=LogBox.web.js.map