"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
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 info_circle_icon_1 = require("@patternfly/react-icons/dist/js/icons/info-circle-icon");
const common_1 = require("../../../../common");
const ClusterWizardContext_1 = require("../../clusterWizard/ClusterWizardContext");
const OpenShiftVersionsContext_1 = require("../../clusterWizard/OpenShiftVersionsContext");
const constants_1 = require("../../clusterWizard/constants");
const wizardTransition_1 = require("../../clusterWizard/wizardTransition");
const featureSupportLevels_1 = require("../../featureSupportLevels");
const ReviewClusterFeatureSupportLevels_1 = require("../../featureSupportLevels/ReviewClusterFeatureSupportLevels");
const use_translation_wrapper_1 = require("../../../../common/hooks/use-translation-wrapper");
const newFeatureSupportLevels_1 = require("../../../../common/components/newFeatureSupportLevels");
const utils_1 = require("../../../../common/utils");
const PreflightChecksDetailExpanded = ({ cluster }) => {
    const clusterWizardContext = (0, ClusterWizardContext_1.useClusterWizardContext)();
    return (react_1.default.createElement(common_1.DetailList, null,
        react_1.default.createElement(common_1.DetailItem, { title: "Cluster preflight checks", value: react_1.default.createElement(common_1.ClusterValidations, { validationsInfo: cluster.validationsInfo, setCurrentStepId: (stepId) => clusterWizardContext.setCurrentStepId(stepId), wizardStepNames: constants_1.wizardStepNames, wizardStepsValidationsMap: wizardTransition_1.wizardStepsValidationsMap }), classNameValue: 'pf-v6-u-mb-md', testId: "cluster-preflight-checks" }),
        react_1.default.createElement(common_1.DetailItem, { title: "Host preflight checks", value: react_1.default.createElement(common_1.HostsValidations, { hosts: cluster.hosts, setCurrentStepId: (stepId) => clusterWizardContext.setCurrentStepId(stepId), wizardStepNames: constants_1.wizardStepNames, allClusterWizardSoftValidationIds: wizardTransition_1.allClusterWizardSoftValidationIds, wizardStepsValidationsMap: wizardTransition_1.wizardStepsValidationsMap }), classNameValue: 'pf-v6-u-mb-md', testId: "host-preflight-checks" }),
        react_1.default.createElement(featureSupportLevels_1.ClusterFeatureSupportLevelsDetailItem, { cluster: cluster })));
};
const PreflightCheckInfo = ({ title, icon, span = 3, offset, className, }) => {
    return (react_1.default.createElement(react_core_1.GridItem, { span: span, offset: offset, className: className },
        react_1.default.createElement(react_core_1.Content, { className: "pf-v6-u-text-wrap pf-v6-u-font-weight-bold" },
            icon,
            " ",
            title)));
};
const getCheckIcon = (validationStatuses) => {
    if (validationStatuses.includes('failure') || validationStatuses.includes('error')) {
        return (react_1.default.createElement(react_core_1.Icon, { size: "sm", status: "danger" },
            react_1.default.createElement(exclamation_circle_icon_1.ExclamationCircleIcon, null)));
    }
    else if (validationStatuses.includes('warning')) {
        return (react_1.default.createElement(react_core_1.Icon, { size: "sm", status: "warning" },
            react_1.default.createElement(exclamation_triangle_icon_1.ExclamationTriangleIcon, null)));
    }
    else if (validationStatuses.includes('pending')) {
        return (react_1.default.createElement(react_core_1.Icon, { size: "sm", status: "info" },
            react_1.default.createElement(info_circle_icon_1.InfoCircleIcon, null)));
    }
    return (react_1.default.createElement(react_core_1.Icon, { status: "success", size: "sm" },
        react_1.default.createElement(check_circle_icon_1.CheckCircleIcon, null)));
};
const PreflightChecksDetailCollapsed = ({ cluster }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const featureSupportLevelData = (0, newFeatureSupportLevels_1.useNewFeatureSupportLevel)();
    const { isSupportedOpenShiftVersion } = (0, OpenShiftVersionsContext_1.useOpenShiftVersionsContext)();
    const { isFullySupported } = react_1.default.useMemo(() => (0, ReviewClusterFeatureSupportLevels_1.getSupportLevelInfo)(cluster, featureSupportLevelData, isSupportedOpenShiftVersion, t), [cluster, featureSupportLevelData, t, isSupportedOpenShiftVersion]);
    const supportLevelIcon = isFullySupported ? (react_1.default.createElement(react_core_1.Icon, { status: "success", size: "sm" },
        react_1.default.createElement(check_circle_icon_1.CheckCircleIcon, null))) : (react_1.default.createElement(react_core_1.Icon, { status: "info", size: "sm" },
        react_1.default.createElement(info_circle_icon_1.InfoCircleIcon, null)));
    const clusterValidations = react_1.default.useMemo(() => Object.values((0, utils_1.stringToJSON)(cluster.validationsInfo) || {})
        .flat()
        .map((val) => (val === null || val === void 0 ? void 0 : val.status) || '')
        .filter(Boolean), [cluster.validationsInfo]);
    const hostValidations = react_1.default.useMemo(() => {
        var _a;
        const hostValidations = (_a = cluster.hosts) === null || _a === void 0 ? void 0 : _a.map((host) => Object.values((0, utils_1.stringToJSON)(host.validationsInfo) || {})).flat(2);
        return ((hostValidations === null || hostValidations === void 0 ? void 0 : hostValidations.map((val) => 
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        wizardTransition_1.allClusterWizardSoftValidationIds.includes((val === null || val === void 0 ? void 0 : val.id) || '') && (val === null || val === void 0 ? void 0 : val.status) !== 'success'
            ? 'warning'
            : (val === null || val === void 0 ? void 0 : val.status) || '')) || []);
    }, [cluster.hosts]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(PreflightCheckInfo, { title: "Cluster preflight checks", icon: getCheckIcon(clusterValidations), span: 3 }),
        react_1.default.createElement(PreflightCheckInfo, { title: "Host preflight checks", icon: getCheckIcon(hostValidations), span: 3 }),
        react_1.default.createElement(PreflightCheckInfo, { title: `Cluster support level: ${isFullySupported ? 'Full' : 'Limited'}`, icon: supportLevelIcon, span: 3 })));
};
const ReviewPreflightChecks = ({ cluster }) => {
    const [isChecksExpanded, setChecksExpanded] = react_1.default.useState(false);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(react_core_1.Divider, null),
        react_1.default.createElement(react_core_1.ExpandableSection, { toggleContent: react_1.default.createElement(react_core_1.Grid, null,
                react_1.default.createElement(react_core_1.GridItem, { span: 2, className: "pf-v6-u-text-wrap pf-v6-u-text-align-start" }, "Preflight checks"),
                !isChecksExpanded && react_1.default.createElement(PreflightChecksDetailCollapsed, { cluster: cluster })), isIndented: true, isExpanded: isChecksExpanded, onToggle: () => setChecksExpanded(!isChecksExpanded), className: 'review-expandable', "data-testid": "review-preflight-checks-main" },
            react_1.default.createElement(PreflightChecksDetailExpanded, { cluster: cluster })),
        react_1.default.createElement(react_core_1.Divider, null)));
};
exports.default = ReviewPreflightChecks;
//# sourceMappingURL=ReviewPreflightChecks.js.map