"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const values_js_1 = tslib_1.__importDefault(require("lodash-es/values.js"));
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const validationsInfoUtils_1 = require("./validationsInfoUtils");
const validationsInfoUtils_2 = require("./validationsInfoUtils");
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const utils_1 = require("../../utils");
const ClusterWizardStepValidationsAlert = ({ currentStepId, clusterStatus, validationsInfo, hosts, wizardStepsValidationsMap, children, lastInstallationPreparation, uiSettings, }) => {
    const [showInstallAlert, setShowInstallAlert] = react_1.default.useState(lastInstallationPreparation && lastInstallationPreparation.status === 'failed');
    const { failedClusterValidations, failedHostnameValidations } = react_1.default.useMemo(() => {
        const reducedValidationsInfo = (0, validationsInfoUtils_2.getWizardStepClusterValidationsInfo)(validationsInfo || {}, currentStepId, wizardStepsValidationsMap);
        const flattenedValues = (0, values_js_1.default)(reducedValidationsInfo).flat();
        const hostnameValidations = hosts.some((host) => {
            const validations = typeof host.validationsInfo === 'string'
                ? (0, utils_1.stringToJSON)(host.validationsInfo)
                : host.validationsInfo;
            return !(0, validationsInfoUtils_1.checkHostValidations)(validations || {}, ['hostname-valid', 'hostname-unique']);
        });
        return {
            failedClusterValidations: flattenedValues.filter((validation) => (validation === null || validation === void 0 ? void 0 : validation.status) === 'failure'),
            failedHostnameValidations: hostnameValidations,
        };
    }, [validationsInfo, currentStepId, wizardStepsValidationsMap, hosts]);
    react_1.default.useEffect(() => {
        var _a;
        if (((_a = lastInstallationPreparation === null || lastInstallationPreparation === void 0 ? void 0 : lastInstallationPreparation.reason) === null || _a === void 0 ? void 0 : _a.includes('manifest')) &&
            (uiSettings === null || uiSettings === void 0 ? void 0 : uiSettings.customManifestsUpdated)) {
            setShowInstallAlert(false);
        }
    }, [lastInstallationPreparation, uiSettings]);
    const isClusterReady = (0, validationsInfoUtils_2.getWizardStepClusterStatus)(currentStepId, wizardStepsValidationsMap, { status: clusterStatus, validationsInfo: validationsInfo || {} }, hosts) === 'ready';
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return (react_1.default.createElement(react_1.default.Fragment, null,
        !isClusterReady && (react_1.default.createElement(react_core_1.AlertGroup, null,
            children,
            react_1.default.createElement(react_core_1.Alert, { variant: react_core_1.AlertVariant.warning, title: "Cluster is not ready yet.", isInline: true },
                react_1.default.createElement(react_core_1.Flex, { spaceItems: { default: 'spaceItemsSm' }, direction: { default: 'column' } },
                    !!failedClusterValidations.length && (react_1.default.createElement(react_1.default.Fragment, null,
                        react_1.default.createElement(react_core_1.FlexItem, null, t('ai:The following requirement must be met:', {
                            count: failedClusterValidations.length,
                        })),
                        react_1.default.createElement(react_core_1.FlexItem, null, failedClusterValidations.length === 1 ? (failedClusterValidations[0].message) : (react_1.default.createElement(react_core_1.List, null, failedClusterValidations.map((validation) => (react_1.default.createElement(react_core_1.ListItem, { key: validation.id }, validation.message)))))))),
                    failedHostnameValidations && (react_1.default.createElement(react_core_1.FlexItem, null, t('ai:The cluster is not ready yet. Some hosts have an ineligible name. To change the hostname, click on it.'))))))),
        showInstallAlert && (react_1.default.createElement(react_core_1.AlertGroup, null,
            react_1.default.createElement(react_core_1.Alert, { variant: react_core_1.AlertVariant.danger, title: "Error in preparing installation", isInline: true },
                react_1.default.createElement(react_core_1.Flex, { spaceItems: { default: 'spaceItemsSm' }, direction: { default: 'column' } },
                    react_1.default.createElement(react_core_1.FlexItem, null, lastInstallationPreparation === null || lastInstallationPreparation === void 0 ? void 0 : lastInstallationPreparation.reason)))))));
};
exports.default = ClusterWizardStepValidationsAlert;
//# sourceMappingURL=ClusterWizardStepValidationsAlert.js.map