Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("lxutil/@sentry/utils");
/** Rewrite event frames paths */
var RewriteFrames = /** @class */ (function () {
    /**
     * @inheritDoc
     */
    function RewriteFrames(options) {
        var _this = this;
        if (options === void 0) { options = {}; }
        /**
         * @inheritDoc
         */
        this.name = RewriteFrames.id;
        /**
         * @inheritDoc
         */
        this._iteratee = function (frame) {
            if (frame.filename && frame.filename.startsWith('/')) {
                var base = _this._root ? utils_1.relative(_this._root, frame.filename) : utils_1.basename(frame.filename);
                frame.filename = "app:///" + base;
            }
            return frame;
        };
        if (options.root) {
            this._root = options.root;
        }
        if (options.iteratee) {
            this._iteratee = options.iteratee;
        }
    }
    /**
     * @inheritDoc
     */
    RewriteFrames.prototype.setupOnce = function (addGlobalEventProcessor, getCurrentHub) {
        addGlobalEventProcessor(function (event) {
            var self = getCurrentHub().getIntegration(RewriteFrames);
            if (self) {
                return self.process(event);
            }
            return event;
        });
    };
    /** JSDoc */
    RewriteFrames.prototype.process = function (event) {
        var frames = this._getFramesFromEvent(event);
        if (frames) {
            for (var i in frames) {
                // tslint:disable-next-line
                frames[i] = this._iteratee(frames[i]);
            }
        }
        return event;
    };
    /** JSDoc */
    RewriteFrames.prototype._getFramesFromEvent = function (event) {
        var exception = event.exception;
        if (exception) {
            try {
                // tslint:disable-next-line:no-unsafe-any
                return exception.values[0].stacktrace.frames;
            }
            catch (_oO) {
                return undefined;
            }
        }
        else if (event.stacktrace) {
            return event.stacktrace.frames;
        }
        return undefined;
    };
    /**
     * @inheritDoc
     */
    RewriteFrames.id = 'RewriteFrames';
    return RewriteFrames;
}());
exports.RewriteFrames = RewriteFrames;
//# sourceMappingURL=rewriteframes.js.map