"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClusterPage = exports.SingleClusterPage = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_router_dom_v5_compat_1 = require("react-router-dom-v5-compat");
const react_redux_1 = require("react-redux");
const react_core_1 = require("@patternfly/react-core");
const common_1 = require("../../../common");
const ClusterDetail_1 = tslib_1.__importDefault(require("../clusterDetail/ClusterDetail"));
const CancelInstallationModal_1 = tslib_1.__importDefault(require("../clusterDetail/CancelInstallationModal"));
const ResetClusterModal_1 = tslib_1.__importDefault(require("../clusterDetail/ResetClusterModal"));
const AddHosts_1 = require("../AddHosts");
const ClusterDefaultConfigurationContext_1 = require("../clusterConfiguration/ClusterDefaultConfigurationContext");
const ClusterBreadcrumbs_1 = tslib_1.__importDefault(require("./ClusterBreadcrumbs"));
const ClusterWizard_1 = tslib_1.__importDefault(require("../clusterWizard/ClusterWizard"));
const ModalDialogsContext_1 = require("../hosts/ModalDialogsContext");
const clusterPolling_1 = require("./clusterPolling");
const DiscoveryImageModal_1 = require("../clusterConfiguration/DiscoveryImageModal");
const config_1 = require("../../config");
const ClusterWizardContextProvider_1 = tslib_1.__importDefault(require("../clusterWizard/ClusterWizardContextProvider"));
const useInfraEnv_1 = tslib_1.__importDefault(require("../../hooks/useInfraEnv"));
const SentryErrorMonitorContextProvider_1 = require("../SentryErrorMonitorContextProvider");
const slice_1 = require("../../store/slices/current-cluster/slice");
const ClusterPageErrors_1 = require("./ClusterPageErrors");
const ClusterLoading_1 = tslib_1.__importDefault(require("./ClusterLoading"));
const ClusterPollingErrorModal_1 = tslib_1.__importDefault(require("../clusterDetail/ClusterPollingErrorModal"));
const ClusterUpdateErrorModal_1 = tslib_1.__importDefault(require("../clusterDetail/ClusterUpdateErrorModal"));
const BackButton_1 = require("../ui/Buttons/BackButton");
const featureSupportLevels_1 = require("../featureSupportLevels");
const hooks_1 = require("../../hooks");
const AssistedInstallerHeader_1 = require("./AssistedInstallerHeader");
const OpenShiftVersionsContext_1 = require("../clusterWizard/OpenShiftVersionsContext");
const ClusterPageGeneric = ({ clusterId, showBreadcrumbs = false, resetModal, }) => {
    var _a;
    const fetchCluster = (0, clusterPolling_1.useFetchCluster)(clusterId);
    const dispatch = (0, react_redux_1.useDispatch)();
    const { cluster, uiState, errorDetail } = (0, clusterPolling_1.useClusterPolling)(clusterId);
    const pullSecret = (0, hooks_1.usePullSecret)();
    const { infraEnv, isLoading: infraEnvLoading, error: infraEnvError, updateInfraEnv, } = (0, useInfraEnv_1.default)(clusterId, (cluster === null || cluster === void 0 ? void 0 : cluster.cpuArchitecture)
        ? cluster.cpuArchitecture
        : common_1.CpuArchitecture.USE_DAY1_ARCHITECTURE, cluster === null || cluster === void 0 ? void 0 : cluster.name, pullSecret, cluster === null || cluster === void 0 ? void 0 : cluster.openshiftVersion);
    const getContent = (cluster, infraEnv) => {
        if (cluster.status === 'adding-hosts') {
            const onReset = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
                dispatch((0, slice_1.forceReload)());
                return Promise.resolve();
            });
            return (react_1.default.createElement(common_1.AddHostsContextProvider, { cluster: cluster, resetCluster: onReset },
                react_1.default.createElement(OpenShiftVersionsContext_1.OpenShiftVersionsContextProvider, null,
                    react_1.default.createElement(AddHosts_1.AddHosts, null))));
        }
        else if ([
            'preparing-for-installation',
            'installing',
            'installing-pending-user-action',
            'finalizing',
            'installed',
            'error',
            'cancelled',
        ].includes(cluster.status)) {
            return (react_1.default.createElement(react_1.default.Fragment, null,
                showBreadcrumbs && react_1.default.createElement(ClusterBreadcrumbs_1.default, { clusterName: cluster.name }),
                react_1.default.createElement(react_core_1.PageSection, { hasBodyWrapper: false },
                    react_1.default.createElement(react_core_1.Content, { component: "h1" }, cluster.name)),
                react_1.default.createElement(react_core_1.PageSection, { hasBodyWrapper: false, isFilled: true },
                    react_1.default.createElement(ClusterDetail_1.default, { cluster: cluster }))));
        }
        else {
            return (react_1.default.createElement(react_1.default.Fragment, null,
                showBreadcrumbs && react_1.default.createElement(ClusterBreadcrumbs_1.default, { clusterName: cluster.name }),
                showBreadcrumbs && (react_1.default.createElement(react_core_1.PageSection, { hasBodyWrapper: false },
                    react_1.default.createElement(AssistedInstallerHeader_1.AssistedInstallerHeader, { clusterName: cluster.name }))),
                react_1.default.createElement(react_core_1.PageSection, { hasBodyWrapper: false },
                    react_1.default.createElement(ClusterWizardContextProvider_1.default, { cluster: cluster, infraEnv: infraEnv },
                        react_1.default.createElement(ClusterWizard_1.default, { cluster: cluster, infraEnv: infraEnv, updateInfraEnv: updateInfraEnv })))));
        }
    };
    if (uiState === common_1.ResourceUIState.LOADING || infraEnvLoading) {
        return react_1.default.createElement(ClusterLoading_1.default, null);
    }
    if (uiState === common_1.ResourceUIState.POLLING_ERROR && !cluster) {
        if (Number(errorDetail === null || errorDetail === void 0 ? void 0 : errorDetail.code) === 404) {
            return react_1.default.createElement(react_router_dom_v5_compat_1.Navigate, { to: `${config_1.routeBasePath}/clusters` });
        }
        return (react_1.default.createElement(react_core_1.PageSection, { hasBodyWrapper: false, isFilled: true },
            react_1.default.createElement(common_1.ErrorState, { title: "Failed to fetch the cluster", fetchData: Number(errorDetail === null || errorDetail === void 0 ? void 0 : errorDetail.code) === 401 ? () => window.location.reload() : fetchCluster, actions: [react_1.default.createElement(BackButton_1.BackButton, { key: 'cancel', to: '..' })] })));
    }
    if (infraEnvError) {
        return (react_1.default.createElement(react_core_1.PageSection, { hasBodyWrapper: false, isFilled: true },
            react_1.default.createElement(common_1.ErrorState, { title: "Cluster details not found", actions: [react_1.default.createElement(BackButton_1.BackButton, { key: 'cancel', to: `..` })], content: 'Check to make sure the cluster-ID is valid. Otherwise, the cluster details may have been deleted.' })));
    }
    if (cluster && infraEnv) {
        return (react_1.default.createElement(ClusterWizardContextProvider_1.default, { cluster: cluster, infraEnv: infraEnv },
            react_1.default.createElement(common_1.AlertsContextProvider, null,
                react_1.default.createElement(SentryErrorMonitorContextProvider_1.SentryErrorMonitorContextProvider, null,
                    react_1.default.createElement(ModalDialogsContext_1.ModalDialogsContextProvider, null,
                        react_1.default.createElement(ClusterDefaultConfigurationContext_1.ClusterDefaultConfigurationProvider, { loadingUI: react_1.default.createElement(ClusterLoading_1.default, null), errorUI: react_1.default.createElement(ClusterPageErrors_1.ClusterUiError, null) },
                            react_1.default.createElement(OpenShiftVersionsContext_1.OpenShiftVersionsContextProvider, null,
                                react_1.default.createElement(featureSupportLevels_1.NewFeatureSupportLevelProvider, { loadingUi: react_1.default.createElement(ClusterLoading_1.default, null), cluster: cluster, cpuArchitecture: infraEnv.cpuArchitecture, openshiftVersion: cluster.openshiftVersion, platformType: (_a = cluster.platform) === null || _a === void 0 ? void 0 : _a.type },
                                    getContent(cluster, infraEnv),
                                    uiState === common_1.ResourceUIState.POLLING_ERROR && react_1.default.createElement(ClusterPollingErrorModal_1.default, null),
                                    uiState === common_1.ResourceUIState.UPDATE_ERROR && react_1.default.createElement(ClusterUpdateErrorModal_1.default, null),
                                    react_1.default.createElement(CancelInstallationModal_1.default, null),
                                    react_1.default.createElement(ResetClusterModal_1.default, null),
                                    resetModal,
                                    react_1.default.createElement(DiscoveryImageModal_1.DiscoveryImageModal, null)))))))));
    }
    return react_1.default.createElement(react_router_dom_v5_compat_1.Navigate, { to: "/clusters" });
};
const SingleClusterPage = ({ clusterId, resetModal, }) => (react_1.default.createElement(common_1.AlertsContextProvider, null,
    react_1.default.createElement(react_core_1.PageSection, { hasBodyWrapper: false, isFilled: true },
        react_1.default.createElement(AssistedInstallerHeader_1.AssistedInstallerHeader, null)),
    react_1.default.createElement(ClusterPageGeneric, { clusterId: clusterId, resetModal: resetModal })));
exports.SingleClusterPage = SingleClusterPage;
const ClusterPage = () => {
    const { clusterId } = (0, react_router_dom_v5_compat_1.useParams)();
    return (react_1.default.createElement(common_1.AlertsContextProvider, null,
        react_1.default.createElement(ClusterPageGeneric, { clusterId: clusterId || '', showBreadcrumbs: true })));
};
exports.ClusterPage = ClusterPage;
//# sourceMappingURL=ClusterPage.js.map