"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_redux_1 = require("react-redux");
const react_core_1 = require("@patternfly/react-core");
const store_1 = require("../../store");
const common_1 = require("../../../common");
const clusterPolling_1 = require("../clusters/clusterPolling");
const ClusterWizard_1 = tslib_1.__importDefault(require("../clusterWizard/ClusterWizard"));
const ClusterDefaultConfigurationContext_1 = require("../clusterConfiguration/ClusterDefaultConfigurationContext");
const ModalDialogsContext_1 = require("../hosts/ModalDialogsContext");
const ClusterInstallationProgressCard_1 = tslib_1.__importDefault(require("./ClusterInstallationProgressCard"));
const DiscoveryImageModal_1 = require("../clusterConfiguration/DiscoveryImageModal");
const CancelInstallationModal_1 = tslib_1.__importDefault(require("./CancelInstallationModal"));
const ResetClusterModal_1 = tslib_1.__importDefault(require("./ResetClusterModal"));
const ClusterPollingErrorModal_1 = tslib_1.__importDefault(require("./ClusterPollingErrorModal"));
const useInfraEnv_1 = tslib_1.__importDefault(require("../../hooks/useInfraEnv"));
const SentryErrorMonitorContextProvider_1 = require("../SentryErrorMonitorContextProvider");
const ClusterWizardContextProvider_1 = tslib_1.__importDefault(require("../clusterWizard/ClusterWizardContextProvider"));
const BackButton_1 = require("../ui/Buttons/BackButton");
const featureSupportLevels_1 = require("../featureSupportLevels");
const hooks_1 = require("../../hooks");
const use_feature_detection_1 = require("../../hooks/use-feature-detection");
const OpenShiftVersionsContext_1 = require("../clusterWizard/OpenShiftVersionsContext");
const react_router_dom_v5_compat_1 = require("react-router-dom-v5-compat");
const LoadingCard = () => (react_1.default.createElement(react_core_1.Card, { "data-testid": "ai-cluster-details-card" },
    react_1.default.createElement(react_core_1.CardHeader, null,
        react_1.default.createElement(react_core_1.Title, { headingLevel: "h1", size: "lg", className: "card-title" }, "Loading additional details")),
    react_1.default.createElement(react_core_1.CardBody, null,
        react_1.default.createElement(common_1.LoadingState, null))));
const ClusterLoadFailed = ({ clusterId, error }) => {
    const fetchCluster = (0, clusterPolling_1.useFetchCluster)(clusterId);
    return (react_1.default.createElement(react_core_1.Card, { "data-testid": "ai-cluster-details-card" },
        react_1.default.createElement(react_core_1.CardHeader, null,
            react_1.default.createElement(react_core_1.Title, { headingLevel: "h1", size: "lg", className: "card-title" }, "Loading additional details")),
        react_1.default.createElement(react_core_1.CardBody, null,
            react_1.default.createElement(react_router_dom_v5_compat_1.BrowserRouter, null,
                react_1.default.createElement(common_1.ErrorState, { title: "Failed to fetch the cluster", fetchData: fetchCluster, actions: [react_1.default.createElement(BackButton_1.BackButton, { key: 'cancel', to: '/cluster-list' })], content: error })))));
};
const LoadingDefaultConfigFailedCard = () => (react_1.default.createElement(react_core_1.Card, { "data-testid": "ai-cluster-details-card" },
    react_1.default.createElement(react_core_1.CardHeader, null,
        react_1.default.createElement(react_core_1.Title, { headingLevel: "h1", size: "lg", className: "card-title" }, "Loading additional details")),
    react_1.default.createElement(react_core_1.CardBody, null,
        react_1.default.createElement(react_router_dom_v5_compat_1.BrowserRouter, null,
            react_1.default.createElement(common_1.ErrorState, { title: "Failed to retrieve the default configuration", actions: [react_1.default.createElement(BackButton_1.BackButton, { key: 'cancel', to: '/cluster-list' })] })))));
const AssistedInstallerDetailCard = ({ aiClusterId, allEnabledFeatures, history, basename, permissions, }) => {
    var _a;
    (0, use_feature_detection_1.useFeatureDetection)(allEnabledFeatures);
    const { cluster, uiState } = (0, clusterPolling_1.useClusterPolling)(aiClusterId);
    const pullSecret = (0, hooks_1.usePullSecret)();
    const { infraEnv, isLoading: infraEnvLoading, error: infraEnvError, updateInfraEnv, } = (0, useInfraEnv_1.default)(aiClusterId, (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);
    if (uiState === common_1.ResourceUIState.LOADING || infraEnvLoading) {
        return react_1.default.createElement(LoadingCard, null);
    }
    else if ((uiState === common_1.ResourceUIState.POLLING_ERROR && !cluster) || infraEnvError) {
        return react_1.default.createElement(ClusterLoadFailed, { clusterId: aiClusterId, error: infraEnvError });
    }
    if (!cluster || !infraEnv || cluster.status === 'adding-hosts') {
        // In OCM the Day 2 flow is rendered in a separate tab.
        return null;
    }
    const showWizard = ['insufficient', 'ready', 'pending-for-input'].includes(cluster.status);
    const content = showWizard ? (react_1.default.createElement(ClusterWizardContextProvider_1.default, { cluster: cluster, infraEnv: infraEnv, permissions: permissions },
        react_1.default.createElement(ClusterWizard_1.default, { cluster: cluster, infraEnv: infraEnv, updateInfraEnv: updateInfraEnv }))) : (react_1.default.createElement(ClusterInstallationProgressCard_1.default, { cluster: cluster }));
    const isOutdatedClusterData = uiState === common_1.ResourceUIState.POLLING_ERROR;
    const clusterDetailCard = (react_1.default.createElement(common_1.AlertsContextProvider, null,
        react_1.default.createElement(OpenShiftVersionsContext_1.OpenShiftVersionsContextProvider, 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(LoadingCard, null), errorUI: react_1.default.createElement(LoadingDefaultConfigFailedCard, null) },
                        react_1.default.createElement(featureSupportLevels_1.NewFeatureSupportLevelProvider, { loadingUi: react_1.default.createElement(LoadingCard, null), cluster: cluster, cpuArchitecture: infraEnv.cpuArchitecture, openshiftVersion: cluster.openshiftVersion, platformType: (_a = cluster.platform) === null || _a === void 0 ? void 0 : _a.type }, content),
                        isOutdatedClusterData && react_1.default.createElement(ClusterPollingErrorModal_1.default, null),
                        react_1.default.createElement(CancelInstallationModal_1.default, null),
                        react_1.default.createElement(ResetClusterModal_1.default, null),
                        react_1.default.createElement(DiscoveryImageModal_1.DiscoveryImageModal, null)))))));
    return (react_1.default.createElement(react_1.default.Fragment, null, history ? (react_1.default.createElement(react_router_dom_v5_compat_1.unstable_HistoryRouter, { history: history, basename: basename || '/' }, clusterDetailCard)) : (react_1.default.createElement(react_router_dom_v5_compat_1.BrowserRouter, null, clusterDetailCard))));
};
const Wrapper = (props) => (react_1.default.createElement(react_redux_1.Provider, { store: store_1.storeDay1 },
    react_1.default.createElement(common_1.AlertsContextProvider, null,
        react_1.default.createElement(AssistedInstallerDetailCard, Object.assign({}, props)))));
exports.default = Wrapper;
//# sourceMappingURL=AssistedInstallerDetailCard.js.map