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 */
9var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10    if (k2 === undefined) k2 = k;
11    var desc = Object.getOwnPropertyDescriptor(m, k);
12    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
13      desc = { enumerable: true, get: function() { return m[k]; } };
14    }
15    Object.defineProperty(o, k2, desc);
16}) : (function(o, m, k, k2) {
17    if (k2 === undefined) k2 = k;
18    o[k2] = m[k];
19}));
20var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21    Object.defineProperty(o, "default", { enumerable: true, value: v });
22}) : function(o, v) {
23    o["default"] = v;
24});
25var __importStar = (this && this.__importStar) || function (mod) {
26    if (mod && mod.__esModule) return mod;
27    var result = {};
28    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29    __setModuleDefault(result, mod);
30    return result;
31};
32Object.defineProperty(exports, "__esModule", { value: true });
33exports.LogBoxLog = void 0;
34const LogBoxSymbolication = __importStar(require("./LogBoxSymbolication"));
35function componentStackToStack(componentStack) {
36    return componentStack.map((stack) => ({
37        file: stack.fileName,
38        methodName: stack.content,
39        lineNumber: stack.location?.row ?? 0,
40        column: stack.location?.column ?? 0,
41        arguments: [],
42    }));
43}
44class LogBoxLog {
45    message;
46    type;
47    category;
48    componentStack;
49    stack;
50    count;
51    level;
52    codeFrame;
53    isComponentError;
54    symbolicated = {
55        stack: {
56            error: null,
57            stack: null,
58            status: 'NONE',
59        },
60        component: {
61            error: null,
62            stack: null,
63            status: 'NONE',
64        },
65    };
66    callbacks = new Map();
67    constructor(data) {
68        this.level = data.level;
69        this.type = data.type ?? 'error';
70        this.message = data.message;
71        this.stack = data.stack;
72        this.category = data.category;
73        this.componentStack = data.componentStack;
74        this.codeFrame = data.codeFrame;
75        this.isComponentError = data.isComponentError;
76        this.count = 1;
77        this.symbolicated = data.symbolicated ?? this.symbolicated;
78    }
79    incrementCount() {
80        this.count += 1;
81    }
82    getAvailableStack(type) {
83        if (this.symbolicated[type].status === 'COMPLETE') {
84            return this.symbolicated[type].stack;
85        }
86        return this.getStack(type);
87    }
88    flushCallbacks(type) {
89        const callbacks = this.callbacks.get(type);
90        const status = this.symbolicated[type].status;
91        if (callbacks) {
92            for (const callback of callbacks) {
93                callback(status);
94            }
95            callbacks.clear();
96        }
97    }
98    pushCallback(type, callback) {
99        let callbacks = this.callbacks.get(type);
100        if (!callbacks) {
101            callbacks = new Set();
102            this.callbacks.set(type, callbacks);
103        }
104        callbacks.add(callback);
105    }
106    retrySymbolicate(type, callback) {
107        this._symbolicate(type, true, callback);
108    }
109    symbolicate(type, callback) {
110        this._symbolicate(type, false, callback);
111    }
112    _symbolicate(type, retry, callback) {
113        if (callback) {
114            this.pushCallback(type, callback);
115        }
116        const status = this.symbolicated[type].status;
117        if (status === 'COMPLETE') {
118            return this.flushCallbacks(type);
119        }
120        if (retry) {
121            LogBoxSymbolication.deleteStack(this.getStack(type));
122            this.handleSymbolicate(type);
123        }
124        else {
125            if (status === 'NONE') {
126                this.handleSymbolicate(type);
127            }
128        }
129    }
130    componentStackCache = null;
131    getStack(type) {
132        if (type === 'component') {
133            if (this.componentStackCache == null) {
134                this.componentStackCache = componentStackToStack(this.componentStack);
135            }
136            return this.componentStackCache;
137        }
138        return this.stack;
139    }
140    handleSymbolicate(type) {
141        if (type === 'component' && !this.componentStack?.length) {
142            return;
143        }
144        if (this.symbolicated[type].status !== 'PENDING') {
145            this.updateStatus(type, null, null, null);
146            LogBoxSymbolication.symbolicate(this.getStack(type)).then((data) => {
147                this.updateStatus(type, null, data?.stack, data?.codeFrame);
148            }, (error) => {
149                this.updateStatus(type, error, null, null);
150            });
151        }
152    }
153    updateStatus(type, error, stack, codeFrame) {
154        const lastStatus = this.symbolicated[type].status;
155        if (error != null) {
156            this.symbolicated[type] = {
157                error,
158                stack: null,
159                status: 'FAILED',
160            };
161        }
162        else if (stack != null) {
163            if (codeFrame) {
164                this.codeFrame = codeFrame;
165            }
166            this.symbolicated[type] = {
167                error: null,
168                stack,
169                status: 'COMPLETE',
170            };
171        }
172        else {
173            this.symbolicated[type] = {
174                error: null,
175                stack: null,
176                status: 'PENDING',
177            };
178        }
179        const status = this.symbolicated[type].status;
180        if (lastStatus !== status) {
181            if (['COMPLETE', 'FAILED'].includes(status)) {
182                this.flushCallbacks(type);
183            }
184        }
185    }
186}
187exports.LogBoxLog = LogBoxLog;
188//# sourceMappingURL=LogBoxLog.js.map