1/**
2 * Copyright (c) 650 Industries.
3 * Copyright (c) Meta Platforms, Inc. and affiliates.
4 *
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8import * as LogBoxSymbolication from './LogBoxSymbolication';
9function componentStackToStack(componentStack) {
10    return componentStack.map((stack) => ({
11        file: stack.fileName,
12        methodName: stack.content,
13        lineNumber: stack.location?.row ?? 0,
14        column: stack.location?.column ?? 0,
15        arguments: [],
16    }));
17}
18export class LogBoxLog {
19    message;
20    type;
21    category;
22    componentStack;
23    stack;
24    count;
25    level;
26    codeFrame;
27    isComponentError;
28    symbolicated = {
29        stack: {
30            error: null,
31            stack: null,
32            status: 'NONE',
33        },
34        component: {
35            error: null,
36            stack: null,
37            status: 'NONE',
38        },
39    };
40    callbacks = new Map();
41    constructor(data) {
42        this.level = data.level;
43        this.type = data.type ?? 'error';
44        this.message = data.message;
45        this.stack = data.stack;
46        this.category = data.category;
47        this.componentStack = data.componentStack;
48        this.codeFrame = data.codeFrame;
49        this.isComponentError = data.isComponentError;
50        this.count = 1;
51        this.symbolicated = data.symbolicated ?? this.symbolicated;
52    }
53    incrementCount() {
54        this.count += 1;
55    }
56    getAvailableStack(type) {
57        if (this.symbolicated[type].status === 'COMPLETE') {
58            return this.symbolicated[type].stack;
59        }
60        return this.getStack(type);
61    }
62    flushCallbacks(type) {
63        const callbacks = this.callbacks.get(type);
64        const status = this.symbolicated[type].status;
65        if (callbacks) {
66            for (const callback of callbacks) {
67                callback(status);
68            }
69            callbacks.clear();
70        }
71    }
72    pushCallback(type, callback) {
73        let callbacks = this.callbacks.get(type);
74        if (!callbacks) {
75            callbacks = new Set();
76            this.callbacks.set(type, callbacks);
77        }
78        callbacks.add(callback);
79    }
80    retrySymbolicate(type, callback) {
81        this._symbolicate(type, true, callback);
82    }
83    symbolicate(type, callback) {
84        this._symbolicate(type, false, callback);
85    }
86    _symbolicate(type, retry, callback) {
87        if (callback) {
88            this.pushCallback(type, callback);
89        }
90        const status = this.symbolicated[type].status;
91        if (status === 'COMPLETE') {
92            return this.flushCallbacks(type);
93        }
94        if (retry) {
95            LogBoxSymbolication.deleteStack(this.getStack(type));
96            this.handleSymbolicate(type);
97        }
98        else {
99            if (status === 'NONE') {
100                this.handleSymbolicate(type);
101            }
102        }
103    }
104    componentStackCache = null;
105    getStack(type) {
106        if (type === 'component') {
107            if (this.componentStackCache == null) {
108                this.componentStackCache = componentStackToStack(this.componentStack);
109            }
110            return this.componentStackCache;
111        }
112        return this.stack;
113    }
114    handleSymbolicate(type) {
115        if (type === 'component' && !this.componentStack?.length) {
116            return;
117        }
118        if (this.symbolicated[type].status !== 'PENDING') {
119            this.updateStatus(type, null, null, null);
120            LogBoxSymbolication.symbolicate(this.getStack(type)).then((data) => {
121                this.updateStatus(type, null, data?.stack, data?.codeFrame);
122            }, (error) => {
123                this.updateStatus(type, error, null, null);
124            });
125        }
126    }
127    updateStatus(type, error, stack, codeFrame) {
128        const lastStatus = this.symbolicated[type].status;
129        if (error != null) {
130            this.symbolicated[type] = {
131                error,
132                stack: null,
133                status: 'FAILED',
134            };
135        }
136        else if (stack != null) {
137            if (codeFrame) {
138                this.codeFrame = codeFrame;
139            }
140            this.symbolicated[type] = {
141                error: null,
142                stack,
143                status: 'COMPLETE',
144            };
145        }
146        else {
147            this.symbolicated[type] = {
148                error: null,
149                stack: null,
150                status: 'PENDING',
151            };
152        }
153        const status = this.symbolicated[type].status;
154        if (lastStatus !== status) {
155            if (['COMPLETE', 'FAILED'].includes(status)) {
156                this.flushCallbacks(type);
157            }
158        }
159    }
160}
161//# sourceMappingURL=LogBoxLog.js.map