"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.ErrorPanel = void 0;
const React = __importStar(require("react"));
const ReactContext_1 = __importDefault(require("../../ReactContext"));
const components_1 = require("../components");
class ErrorPanel extends React.Component {
    constructor(props) {
        super(props);
        this.expand = () => {
            if (!this.state.expanded)
                this.setState({ expanded: true });
        };
        this.collapseToggle = () => this.setState({ expanded: !this.state.expanded });
        this.state = { expanded: true };
    }
    render() {
        const { errors, title, clickHandler, poppedToggle, showToggle, classNames, footer, formContext: { translations }, popped } = this.props;
        if (errors.length === 0)
            return null;
        const { Panel, ListGroup } = this.context.theme;
        return (React.createElement(Panel, { collapsible: "true", onToggle: this.collapseToggle, className: classNames },
            React.createElement(Panel.Heading, null,
                React.createElement("div", { className: "laji-form-clickable-panel-header", onClick: this.collapseToggle },
                    React.createElement("div", { className: "panel-title" },
                        title,
                        React.createElement("span", { className: "pull-right" },
                            React.createElement(components_1.GlyphButton, { glyph: this.state.expanded ? "chevron-up" : "chevron-down", variant: "link" }, translations[this.state.expanded ? "Close" : "Open"]),
                            showToggle && React.createElement(components_1.GlyphButton, { glyph: "new-window", variant: "link", onClick: poppedToggle }, translations[popped ? "Unpop" : "Pop"]))))),
            React.createElement(Panel.Collapse, { in: this.state.expanded },
                React.createElement(ListGroup, null, errors.map((props, i) => React.createElement(ErrorPanelError, Object.assign({ key: i, clickHandler: clickHandler }, props))))),
            footer
                ? (React.createElement(Panel.Footer, null, footer))
                : null));
    }
}
exports.ErrorPanel = ErrorPanel;
ErrorPanel.contextType = ReactContext_1.default;
function ErrorPanelError({ label, error, id, getId, extra = null, disabled, clickHandler }) {
    const message = error && error.message ? error.message : error;
    const _clickHandler = React.useCallback(() => {
        clickHandler(id || (getId ? getId() : undefined));
    }, [clickHandler, id, getId]);
    const { ListGroupItem } = React.useContext(ReactContext_1.default).theme;
    return (React.createElement(ListGroupItem, { onClick: _clickHandler, disabled: disabled },
        label ? React.createElement("b", null,
            label,
            ":") : null,
        " ",
        message,
        " ",
        extra));
}
