"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HostsValidations = exports.ClusterValidations = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const values_js_1 = tslib_1.__importDefault(require("lodash-es/values.js"));
const react_core_1 = require("@patternfly/react-core");
const check_circle_icon_1 = require("@patternfly/react-icons/dist/js/icons/check-circle-icon");
const exclamation_circle_icon_1 = require("@patternfly/react-icons/dist/js/icons/exclamation-circle-icon");
const exclamation_triangle_icon_1 = require("@patternfly/react-icons/dist/js/icons/exclamation-triangle-icon");
const config_1 = require("../../config");
const hosts_1 = require("../hosts");
const validationsInfoUtils_1 = require("./validationsInfoUtils");
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const react_i18next_1 = require("react-i18next");
const utils_1 = require("../../utils");
const AllValidationsPassed = () => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(react_core_1.Icon, { status: "success" },
            react_1.default.createElement(check_circle_icon_1.CheckCircleIcon, null)),
        ' ',
        t('ai:All checks passed')));
};
const PendingValidations = ({ id, count }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return react_1.default.createElement("div", { id: id }, t('ai:There is still {{count}} pending check', { count: count }));
};
const ValidationActionLink = ({ step, setCurrentStepId, wizardStepNames, }) => (react_1.default.createElement(react_core_1.Button, { variant: react_core_1.ButtonVariant.link, onClick: () => setCurrentStepId(step), isInline: true }, wizardStepNames[step]));
const FailingValidation = ({ validation, clusterGroup, hostGroup, severity = 'danger', setCurrentStepId, wizardStepNames, wizardStepsValidationsMap, host, }) => {
    var _a, _b;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const issue = t('ai:{{check_failed}} check failed', {
        check_failed: (0, config_1.hostValidationLabels)(t)[validation.id] ||
            (0, config_1.clusterValidationLabels)(t)[validation.id] ||
            validation.id,
    });
    let fix;
    const step = (0, validationsInfoUtils_1.findValidationFixStep)({ validationId: validation.id, clusterGroup, hostGroup }, wizardStepsValidationsMap);
    if (step === 'review') {
        // no sooner step, so the user can not do anything about it ...
        fix = t('ai:Please wait till all checks are finished.');
    }
    else if (step) {
        const pageURL = host
            ? (_b = (_a = wizardStepsValidationsMap[step]).getPageURL) === null || _b === void 0 ? void 0 : _b.call(_a, host, validation.id)
            : undefined;
        fix = pageURL ? (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(react_i18next_1.Trans, { t: t },
                "ai:It can be fixed in the ",
                react_1.default.createElement("a", { href: pageURL.url }, pageURL.name),
                " page."))) : (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(react_i18next_1.Trans, { t: t },
                "ai:It can be fixed in the",
                ' ',
                react_1.default.createElement(ValidationActionLink, { step: step, setCurrentStepId: setCurrentStepId, wizardStepNames: wizardStepNames }),
                ' ',
                "step.")));
    }
    else {
        if (validation.id === 'platform-requirements-satisfied' ||
            validation.id === 'openshift-ai-gpu-requirements-satisfied') {
            fix = validation.message;
        }
        // console.error(
        //   'Unknown validation ID detected in the ',
        //   clusterGroup || hostGroup,
        //   ' group:',
        //   JSON.stringify(validation),
        // );
    }
    let icon;
    if (severity === 'warning') {
        icon = (react_1.default.createElement(react_core_1.Icon, { status: "warning" },
            react_1.default.createElement(exclamation_triangle_icon_1.ExclamationTriangleIcon, null)));
    }
    else {
        icon = (react_1.default.createElement(react_core_1.Icon, { status: "danger" },
            react_1.default.createElement(exclamation_circle_icon_1.ExclamationCircleIcon, null)));
    }
    return (react_1.default.createElement("div", { id: `failing-validation-${validation.id}` },
        icon,
        " ",
        issue,
        fix ? '.' : '',
        " ",
        fix));
};
const ClusterValidations = ({ validationsInfo: validationsInfoString = '', setCurrentStepId, wizardStepNames, wizardStepsValidationsMap, }) => {
    const validationsInfo = (0, utils_1.stringToJSON)(validationsInfoString) || {};
    const failingValidations = [];
    let pendingCount = 0;
    (0, utils_1.getKeys)(validationsInfo).forEach((group) => {
        var _a;
        const addFailingValidation = (validation) => {
            if (validation.status === 'pending') {
                pendingCount++;
            }
            if (validation.status === 'failure') {
                failingValidations.push(react_1.default.createElement(FailingValidation, { key: validation.id, validation: validation, clusterGroup: group, setCurrentStepId: setCurrentStepId, wizardStepNames: wizardStepNames, wizardStepsValidationsMap: wizardStepsValidationsMap }));
            }
        };
        (_a = validationsInfo[group]) === null || _a === void 0 ? void 0 : _a.forEach(addFailingValidation);
    });
    if (pendingCount) {
        failingValidations.unshift(react_1.default.createElement(PendingValidations, { key: "pending-validations", id: "cluster-pending-validations", count: pendingCount }));
    }
    if (failingValidations.length === 0) {
        return react_1.default.createElement(AllValidationsPassed, null);
    }
    return react_1.default.createElement(react_1.default.Fragment, null, failingValidations);
};
exports.ClusterValidations = ClusterValidations;
const HostsValidations = ({ hosts = [], setCurrentStepId, wizardStepNames, allClusterWizardSoftValidationIds, wizardStepsValidationsMap, }) => {
    const failingValidations = {};
    (0, hosts_1.getEnabledHosts)(hosts).forEach((host) => {
        const validationsInfo = (0, utils_1.stringToJSON)(host.validationsInfo) || {};
        (0, utils_1.getKeys)(validationsInfo).forEach((group) => {
            var _a;
            const addFailingValidation = (validation) => {
                if (validation.status === 'failure') {
                    const validationId = validation.id;
                    const severity = allClusterWizardSoftValidationIds.includes(validationId)
                        ? 'warning'
                        : 'danger';
                    if (!failingValidations[validationId]) {
                        failingValidations[validationId] = (react_1.default.createElement(FailingValidation, { key: validationId, validation: validation, host: host, hostGroup: group, severity: severity, setCurrentStepId: setCurrentStepId, wizardStepNames: wizardStepNames, wizardStepsValidationsMap: wizardStepsValidationsMap }));
                    }
                }
            };
            (_a = validationsInfo[group]) === null || _a === void 0 ? void 0 : _a.forEach(addFailingValidation);
        });
    });
    const array = (0, values_js_1.default)(failingValidations);
    if (array.length === 0) {
        return react_1.default.createElement(AllValidationsPassed, null);
    }
    return react_1.default.createElement(react_1.default.Fragment, null, array);
};
exports.HostsValidations = HostsValidations;
//# sourceMappingURL=ReviewValidations.js.map