"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClusterProgressAlerts = void 0;
const tslib_1 = require("tslib");
const common_1 = require("../../../common");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const ProgressBarAlerts_1 = require("./ProgressBarAlerts");
const hostsStatus = (hosts, hostRole) => {
    const totalHosts = hosts.filter((host) => host.role && host.role === hostRole);
    const failedHosts = totalHosts.filter((host) => ['cancelled', 'error'].includes(host.status));
    const pendingUserActionHosts = totalHosts.filter((host) => ['installing-pending-user-action'].includes(host.status));
    return [totalHosts.length, failedHosts.length, pendingUserActionHosts.length];
};
const olmOperatorsStatus = (olmOperators) => {
    return olmOperators.filter((operator) => operator.status && operator.status === 'failed');
};
const getClusterProgressAlerts = (hosts, cluster, olmOperators) => {
    var _a;
    const [totalMasters, failedMasters, pendingUserActionMasters] = hostsStatus(hosts, 'master');
    const [totalWorkers, failedWorkers, pendingUserActionWorkers] = hostsStatus(hosts, 'worker');
    const failedOperators = olmOperatorsStatus(olmOperators);
    const finalizingStageTimeout = (_a = cluster === null || cluster === void 0 ? void 0 : cluster.progress) === null || _a === void 0 ? void 0 : _a.finalizingStageTimedOut;
    if (['error', 'cancelled'].includes(cluster.status)) {
        return (react_1.default.createElement(react_core_1.Stack, null,
            react_1.default.createElement(react_core_1.StackItem, null,
                react_1.default.createElement(ProgressBarAlerts_1.HostsInstallationFailed, { cluster: cluster, totalHosts: totalMasters, failedHosts: failedMasters, isCriticalNumberOfWorkersFailed: failedWorkers === 1 }))));
    }
    else if (finalizingStageTimeout) {
        return (react_1.default.createElement(react_core_1.Stack, null,
            react_1.default.createElement(react_core_1.StackItem, null,
                react_1.default.createElement(ProgressBarAlerts_1.ClusterInstallationTimeout, { cluster: cluster }),
                ' ')));
    }
    else {
        return (react_1.default.createElement(react_core_1.Stack, { hasGutter: true },
            react_1.default.createElement(common_1.RenderIf, { condition: cluster.status === 'installed' },
                react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(ProgressBarAlerts_1.HostsInstallationSuccess, null))),
            react_1.default.createElement(common_1.RenderIf, { condition: failedWorkers > 0 },
                react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(ProgressBarAlerts_1.HostInstallationWarning, { cluster: cluster, totalHosts: totalWorkers, failedHosts: failedWorkers, title: `Could not install ${failedWorkers} worker hosts`, hostsType: 'worker', message: 'failed to install.' }))),
            react_1.default.createElement(common_1.RenderIf, { condition: failedOperators.length > 0 },
                react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(ProgressBarAlerts_1.HostInstallationWarning, { cluster: cluster, totalHosts: totalMasters, failedHosts: failedMasters, title: 'Some operators failed to install', failedOperators: failedOperators, message: 'failed to install.' }))),
            react_1.default.createElement(common_1.RenderIf, { condition: pendingUserActionMasters > 0 },
                react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(ProgressBarAlerts_1.HostInstallationWarning, { cluster: cluster, totalHosts: totalMasters, failedHosts: failedMasters, title: 'Some hosts are pending user action', failedOperators: failedOperators, message: 'are pending user action.' }))),
            react_1.default.createElement(common_1.RenderIf, { condition: pendingUserActionWorkers > 0 },
                react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(ProgressBarAlerts_1.HostInstallationWarning, { cluster: cluster, totalHosts: totalWorkers, failedHosts: failedWorkers, title: 'Some hosts are pending user action', message: 'are pending user action.' })))));
    }
};
exports.getClusterProgressAlerts = getClusterProgressAlerts;
//# sourceMappingURL=getProgressBarAlerts.js.map