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