"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.FailedBackgroundJobsPanel = void 0;
const React = __importStar(require("react"));
const ReactContext_1 = __importDefault(require("../../ReactContext"));
const components_1 = require("../components");
const utils_1 = require("../../utils");
class FailedBackgroundJobsPanel extends React.Component {
    constructor(props) {
        super(props);
        this.dismissFailedJob = ({ hook, running }) => (e) => {
            e.stopPropagation();
            if (running)
                return;
            this.props.formContext.services.submitHooks.remove(undefined, hook);
        };
        this.retryFailedJob = ({ hook, running }) => (e) => {
            e.stopPropagation();
            if (running)
                return;
            hook();
        };
        this.poppedToggle = (e) => {
            e.stopPropagation();
            this.setState({ popped: !this.state.popped, poppedTouched: true });
        };
        this.state = { popped: true };
    }
    render() {
        const { jobs = [], schema, uiSchema = {}, formContext: { translations } } = this.props;
        if (!jobs.length)
            return null;
        const { Glyphicon } = this.context.theme;
        const errors = jobs.reduce((_errors, error) => {
            const { lajiFormId, relativePointer, e, running } = error;
            if (!e) {
                return _errors;
            }
            const getJsonPointer = () => this.props.formContext.services.ids.getJSONPointerFromLajiFormIdAndRelativePointer(lajiFormId, relativePointer);
            const jsonPointer = getJsonPointer();
            const label = (0, utils_1.parseJSONPointer)(uiSchema, `${(0, utils_1.uiSchemaJSONPointer)(uiSchema, jsonPointer)}/ui:title`, true)
                || (0, utils_1.parseJSONPointer)(schema, `${(0, utils_1.schemaJSONPointer)(schema, jsonPointer)}/title`, true);
            const retryButton = React.createElement(components_1.Button, { key: "retry", className: "pull-right", variant: "link", small: true, disabled: running, onClick: this.retryFailedJob(error) },
                React.createElement(Glyphicon, { className: running ? "rotating" : "", glyph: "refresh" }),
                " ",
                translations.Retry);
            const dismissButton = React.createElement(components_1.Button, { key: "dismiss", className: "pull-right", variant: "link", small: true, onClick: this.dismissFailedJob(error) },
                React.createElement(Glyphicon, { glyph: "ok" }),
                " ",
                translations.Dismiss);
            const getId = () => {
                const jsonPointer = getJsonPointer();
                return `root_${(0, utils_1.JSONPointerToId)(jsonPointer)}`;
            };
            const _error = {
                getId,
                error: e,
                extra: [dismissButton, retryButton],
                disabled: running,
                label
            };
            return [..._errors, _error];
        }, []);
        if (!errors.length)
            return null;
        const footer = (React.createElement(components_1.Button, { onClick: this.props.formContext.services.submitHooks.removeAll },
            React.createElement(Glyphicon, { glyph: "ok" }),
            " ",
            `${translations.Dismiss} ${translations.all}`));
        return (React.createElement("div", { className: `laji-form-error-list laji-form-failed-jobs-list${this.state.popped ? " laji-form-popped" : ""}`, style: this.state.popped ? { top: (this.props.formContext.topOffset || 0) + 5 } : undefined },
            React.createElement(components_1.ErrorPanel, { title: translations.FailedBackgroundJobs, errors: errors, showToggle: true, poppedToggle: this.poppedToggle, popped: this.state.popped, formContext: this.props.formContext, clickHandler: this.props.errorClickHandler, classNames: "error-panel", footer: footer })));
    }
}
exports.FailedBackgroundJobsPanel = FailedBackgroundJobsPanel;
FailedBackgroundJobsPanel.contextType = ReactContext_1.default;
