"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __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.AttributesErrorsPanel = void 0;
var react_1 = __importStar(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var Warning_1 = __importDefault(require("@mui/icons-material/Warning"));
var Error_1 = __importDefault(require("@mui/icons-material/Error"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var useCachedErrors_1 = require("./useCachedErrors");
var ErrorsPanel_1 = require("./components/ErrorsPanel");
var Error_2 = require("./components/Error");
var styles_1 = require("./styles");
var AttributesErrorsPanel = function (_a) {
    var _b = _a.attributesWithErrors, attributesWithErrors = _b === void 0 ? [] : _b, _c = _a.alwaysShowLink, alwaysShowLink = _c === void 0 ? false : _c;
    var styles = (0, styles_1.useStyles)();
    var cachedErrors = (0, useCachedErrors_1.useCachedErrors)(attributesWithErrors);
    var _d = (0, react_1.useMemo)(function () { return (0, mdm_sdk_1.splitErrorsBySeverity)(cachedErrors); }, [cachedErrors]), errors = _d[0], warnings = _d[1];
    var _e = (0, react_1.useState)(null), expandedPanelId = _e[0], setExpandedPanelId = _e[1];
    var panels = (0, react_1.useMemo)(function () {
        return [
            {
                id: 'errors',
                title: ui_i18n_1.default.text('${number} ${errors}', {
                    number: errors.length,
                    errors: errors.length === 1 ? ui_i18n_1.default.text('error') : ui_i18n_1.default.text('errors')
                }),
                icon: react_1.default.createElement(Warning_1.default, { className: (0, classnames_1.default)(styles.icon, styles.errorIcon) }),
                items: errors,
                accordionClassName: styles.errorsAccordion,
                accordionDetailsClassName: styles.errorsAccordionDetails
            },
            {
                id: 'warnings',
                title: ui_i18n_1.default.text('${number} ${warnings}', {
                    number: warnings.length,
                    warnings: warnings.length === 1 ? ui_i18n_1.default.text('warning') : ui_i18n_1.default.text('warnings')
                }),
                icon: react_1.default.createElement(Error_1.default, { className: (0, classnames_1.default)(styles.icon, styles.warningIcon) }),
                items: warnings,
                accordionClassName: styles.warningsAccordion,
                accordionDetailsClassName: styles.warningsAccordionDetails
            }
        ].filter(function (_a) {
            var items = _a.items;
            return items.length > 0;
        });
    }, [errors, warnings, styles]);
    (0, react_1.useEffect)(function () {
        setExpandedPanelId('errors');
    }, [errors]);
    var handleChange = (0, react_1.useCallback)(function (panelId) { return function (event, isExpanded) {
        setExpandedPanelId(isExpanded ? panelId : null);
    }; }, []);
    return panels.length > 0 ? (react_1.default.createElement(ErrorsPanel_1.ErrorsPanel, { panels: panels, expandedPanelId: expandedPanelId, onToggle: handleChange, ErrorComponent: Error_2.AttributeErrorItem, alwaysShowLink: alwaysShowLink })) : null;
};
exports.AttributesErrorsPanel = AttributesErrorsPanel;
