"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Error = exports.normalizeStack = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var type_guards_1 = require("../utils/type-guards");
var core_1 = require("@blueprintjs/core");
/**
 * Returns a normalized stack trace for the given error object
 *
 * @export
 * @param {(Error|InitError)} err
 * @returns {string[]}
 */
function normalizeStack(err) {
    var stack;
    if (err.stack instanceof Array) {
        stack = err.stack || [];
    }
    else if (typeof (err.stack) == 'string') {
        stack = (err.stack || "").split("\n");
    }
    else {
        stack = [];
    }
    return stack;
}
exports.normalizeStack = normalizeStack;
/**
 * The Error component displays an error object in a nicely formatted manner
 * @param props
 */
var Error = function (props) {
    var err = props.error;
    if ((0, type_guards_1.isError)(err) || (0, type_guards_1.isInitError)(err)) {
        if (props.errorRenderer) {
            return props.errorRenderer(err);
        }
        else {
            var message = err.message;
            var stack = normalizeStack(err);
            return React.createElement(core_1.Callout, { intent: core_1.Intent.DANGER, icon: "error" },
                React.createElement("h5", { className: "error-header" }, err.message),
                React.createElement("ul", { className: "error-stack" }, stack.map(function (ln, i) { return React.createElement("li", { key: "stack-line-".concat(i) }, ln); })));
        }
    }
    else {
        return React.createElement(core_1.Callout, { intent: core_1.Intent.DANGER, icon: "error" },
            React.createElement("h5", { className: "error-header" }, err));
    }
};
exports.Error = Error;
//# sourceMappingURL=error.js.map