"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorHandler = void 0;
exports.ErrorContextWrapper = ErrorContextWrapper;
const react_1 = __importStar(require("react"));
const Text_1 = __importDefault(require("../components/generic/Text"));
const ErrorModal = (0, react_1.lazy)(() => Promise.resolve().then(() => __importStar(require('../components/generic/modal/ErrorModal'))));
const ErrorContext = react_1.default.createContext(null);
exports.default = ErrorContext;
exports.ErrorHandler = {};
function ErrorContextWrapper(props) {
    var _a, _b, _c;
    const [errors, setErrors] = (0, react_1.useState)([]);
    // Improve ErrorHandler
    const pushError = (error) => {
        if (!!(error === null || error === void 0 ? void 0 : error.message)) {
            setErrors((currentErrors) => {
                const exists = currentErrors.some((it) => it.title === error.title && it.message === error.message);
                if (!exists) {
                    error.id = Math.random();
                    return [...currentErrors, error];
                }
                return currentErrors;
            });
        }
    };
    const context = {
        addError: pushError,
    };
    exports.ErrorHandler.add = pushError;
    const onCloseModalHandler = () => {
        if (errors[0].retry) {
            errors[0].retry();
        }
        errors.shift();
        setErrors(() => [...errors]);
    };
    const buttonText = ((_b = (_a = errors === null || errors === void 0 ? void 0 : errors[0]) === null || _a === void 0 ? void 0 : _a.buttonText) !== null && _b !== void 0 ? _b : (_c = errors === null || errors === void 0 ? void 0 : errors[0]) === null || _c === void 0 ? void 0 : _c.retry) ? 'Retry' : 'Close';
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(ErrorContext.Provider, { value: context },
            !!errors.length && (react_1.default.createElement(react_1.Suspense, { fallback: "" },
                react_1.default.createElement(ErrorModal, { title: errors[0].title, closeButtonText: buttonText, open: true, onClose: onCloseModalHandler, icon: errors[0].icon },
                    react_1.default.createElement(Text_1.default, null, errors[0].message)))),
            props.children)));
}
//# sourceMappingURL=ErrorContext.js.map