"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 __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const React = __importStar(require("react"));
const components_1 = require("../components");
const utils_1 = require("../../utils");
const ReactContext_1 = __importDefault(require("../../ReactContext"));
class ErrorListTemplate extends React.Component {
    constructor(props) {
        super(props);
        this.expand = () => {
            if (!this.state.popped)
                this.setState({ popped: true });
            this.refs.errorPanel.expand();
            this.refs.warningPanel.expand();
        };
        this.poppedToggle = (e) => {
            e.stopPropagation();
            this.setState({ popped: !this.state.popped, poppedTouched: true });
        };
        this.revalidate = () => {
            this.props.formContext.services.rootInstance.validate();
            this.refs.errorPanel.expand();
            this.refs.warningPanel.expand();
        };
        this.submitWithWarnings = () => {
            this.props.formContext.services.rootInstance.submitWithWarnings();
        };
        this.state = { popped: false, poppedTouched: false };
        this.props.formContext.services.rootInstance.setErrorListInstance(this);
    }
    render() {
        const { errorSchema, schema, formContext, uiSchema } = this.props;
        const { "ui:disabled": disabled, "ui:readonly": readonly } = uiSchema;
        const { translations } = formContext;
        const clickHandler = formContext.services.focus.focus;
        function walkErrors(path, id, errorSchema, uiSchema, defaultTitle) {
            const { __errors } = errorSchema, properties = __rest(errorSchema, ["__errors"]);
            const _schema = (0, utils_1.parseJSONPointer)(schema, path);
            const title = _schema.title || defaultTitle;
            let { errors, warnings } = (__errors || []).reduce(({ errors, warnings }, _error) => {
                if (_error.includes("[warning]")) {
                    warnings.push({
                        label: title,
                        error: (0, utils_1.formatErrorMessage)(_error),
                        id: id
                    });
                }
                else {
                    errors.push({
                        label: title,
                        error: (0, utils_1.formatErrorMessage)(_error),
                        id: id
                    });
                }
                return { errors, warnings };
            }, { errors: [], warnings: [] });
            Object.keys(properties).forEach(prop => {
                let _path = path;
                if (prop.match(/^\d+$/))
                    _path = `${_path}/items`;
                else
                    _path = `${_path}/properties/${prop}`;
                const _defaultTitle = _schema.type === "array" || uiSchema["ui:multiLanguage"] ? `${title} (${prop})` : prop;
                const childErrors = walkErrors(_path, `${id}_${prop}`, errorSchema[prop], uiSchema[prop] || {}, _defaultTitle);
                errors = [...errors, ...childErrors.errors];
                warnings = [...warnings, ...childErrors.warnings];
            });
            return { errors, warnings };
        }
        const { Glyphicon } = this.context.theme;
        const { errors, warnings } = walkErrors("", "root", errorSchema, uiSchema, "");
        const footer = (errors.length > 0
            ? React.createElement(components_1.Button, { onClick: this.revalidate },
                React.createElement(Glyphicon, { glyph: "refresh" }),
                " ",
                translations.Revalidate)
            : React.createElement(components_1.Button, { onClick: this.submitWithWarnings, variant: "success", disabled: disabled || readonly }, translations.SubmitWithWarnings));
        return (React.createElement("div", { className: `laji-form-error-list${this.state.popped ? " laji-form-popped" : ""}${errors.length === 0 ? " laji-form-warning-list" : ""}`, style: this.state.popped ? { top: (this.props.formContext.topOffset || 0) + 5 } : null },
            React.createElement(components_1.ErrorPanel, { classNames: "error-panel", ref: "errorPanel", errors: errors, title: translations.Errors, clickHandler: clickHandler, showToggle: true, poppedToggle: this.poppedToggle, popped: this.state.popped, formContext: this.props.formContext, footer: footer }),
            React.createElement(components_1.ErrorPanel, { classNames: "warning-panel", ref: "warningPanel", errors: warnings, title: translations.Warnings, clickHandler: clickHandler, showToggle: errors.length === 0, poppedToggle: this.poppedToggle, popped: this.state.popped, formContext: this.props.formContext, footer: footer })));
    }
}
ErrorListTemplate.contextType = ReactContext_1.default;
exports.default = ErrorListTemplate;
