"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FinalizingProgress = exports.getFinalizingStatus = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const capitalize_js_1 = tslib_1.__importDefault(require("lodash-es/capitalize.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 in_progress_icon_1 = require("@patternfly/react-icons/dist/js/icons/in-progress-icon");
const pending_icon_1 = require("@patternfly/react-icons/dist/js/icons/pending-icon");
const ui_1 = require("../ui");
const ClusterProgressItem_1 = tslib_1.__importDefault(require("./ClusterProgressItem"));
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const areAllBuiltInOperatorsAvailable = (monitoredOperators = []) => {
    return monitoredOperators.length
        ? monitoredOperators
            .filter((op) => op.operatorType === 'builtin')
            .every((op) => op.status === 'available')
        : false;
};
const getFinalizingStatus = (cluster) => {
    let finalizingIcon, initializationStatus;
    if (areAllBuiltInOperatorsAvailable(cluster.monitoredOperators)) {
        finalizingIcon = (react_1.default.createElement(react_core_1.Icon, { status: "success" },
            react_1.default.createElement(check_circle_icon_1.CheckCircleIcon, null)));
        initializationStatus = 'completed';
    }
    else {
        switch (cluster.status) {
            case 'finalizing':
                finalizingIcon = react_1.default.createElement(in_progress_icon_1.InProgressIcon, null);
                initializationStatus = 'finalizing';
                break;
            case 'error':
            case 'cancelled':
                finalizingIcon = (react_1.default.createElement(react_core_1.Icon, { status: "danger" },
                    react_1.default.createElement(exclamation_circle_icon_1.ExclamationCircleIcon, null)));
                initializationStatus = 'failed';
                break;
            case 'installed':
            case 'adding-hosts':
                finalizingIcon = (react_1.default.createElement(react_core_1.Icon, { status: "success" },
                    react_1.default.createElement(check_circle_icon_1.CheckCircleIcon, null)));
                initializationStatus = 'completed';
                break;
            default:
                finalizingIcon = react_1.default.createElement(pending_icon_1.PendingIcon, null);
                initializationStatus = 'pending';
        }
    }
    return { finalizingIcon, initializationStatus };
};
exports.getFinalizingStatus = getFinalizingStatus;
const FinalizingProgress = ({ cluster, onFetchEvents, fallbackEventsURL, }) => {
    const [isModalOpen, setIsModalOpen] = react_1.default.useState(false);
    const { finalizingIcon, initializationStatus } = (0, exports.getFinalizingStatus)(cluster);
    const closeModal = () => setIsModalOpen(false);
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(ui_1.EventsModal, { title: t('ai:Cluster Events'), isOpen: isModalOpen, onClose: closeModal, hostId: undefined, cluster: cluster, entityKind: "cluster", onFetchEvents: onFetchEvents, fallbackEventsURL: fallbackEventsURL }),
        react_1.default.createElement(ClusterProgressItem_1.default, { icon: finalizingIcon },
            react_1.default.createElement(react_1.default.Fragment, null,
                ['finalizing', 'pending'].includes(initializationStatus) ? (react_1.default.createElement(react_core_1.Popover, { zIndex: 300, headerContent: react_1.default.createElement(react_1.default.Fragment, null, t('ai:Initialization')), bodyContent: react_1.default.createElement(react_core_1.Content, { component: "p" }, t('ai:This stage may take a while to finish. To view detailed information, click the events log link below.')), footerContent: react_1.default.createElement(react_core_1.Button, { variant: react_core_1.ButtonVariant.link, isInline: true, onClick: () => setIsModalOpen(true) }, t('ai:Open Events Log')) },
                    react_1.default.createElement(react_core_1.Button, { variant: react_core_1.ButtonVariant.link, isInline: true }, t('ai:Initialization')))) : (t('ai:Initialization')),
                react_1.default.createElement("small", null, (0, capitalize_js_1.default)(initializationStatus))))));
};
exports.FinalizingProgress = FinalizingProgress;
//# sourceMappingURL=FinalizingProgress.js.map