"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StackTrace = exports.StackTraceCall = void 0;
class StackTraceCall {
    constructor(stackTrace) {
        this.method = "";
        this.source = "";
        this.line = -1;
        this.column = 0;
        if (stackTrace.indexOf("(") > 0) {
            // eslint-disable-next-line no-useless-escape
            const traceLine = /\s*at ([^(]*)\(([^\:]*)\:(\d+)\:(\d+)\)/.exec(stackTrace);
            if (traceLine && traceLine.length === 5) {
                this.method = traceLine[1].trim();
                this.source = traceLine[2].trim();
                this.line = parseInt(traceLine[3]);
                this.column = parseInt(traceLine[4]);
            }
        }
        else {
            // eslint-disable-next-line no-useless-escape
            const traceLine = /\s*at ([^\:]*)\:(\d+)\:(\d+)/.exec(stackTrace);
            if (traceLine && traceLine.length === 4) {
                this.method = "";
                this.source = traceLine[1].trim();
                this.line = parseInt(traceLine[2]);
                this.column = parseInt(traceLine[3]);
            }
        }
    }
    get isValid() {
        return this.line >= 0;
    }
}
exports.StackTraceCall = StackTraceCall;
class StackTrace {
    constructor() {
        this.stackTrace = [];
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        const stack = Error().stack;
        const trace = stack.split("\n");
        this.stackTrace = [];
        for (const traceLine of trace) {
            const trace = new StackTraceCall(traceLine);
            if (trace.isValid) {
                this.stackTrace.push(trace);
            }
        }
    }
    get stack() {
        let trace = "";
        for (const call of this.stackTrace) {
            if (call.method != "") {
                trace += `${call.method}\n`;
            }
        }
        return trace;
    }
    find(method, functionOffset = 0) {
        for (let i = 0; i < this.stackTrace.length; i++) {
            if (this.stackTrace[i].method === method) {
                if (i + functionOffset >= 0 && i + functionOffset < this.stackTrace.length) {
                    return this.stackTrace[i + functionOffset];
                }
            }
        }
        return undefined;
    }
}
exports.StackTrace = StackTrace;
//# sourceMappingURL=strackTrace.js.map