"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 common_1 = require("../../../../common");
const ClusterWizardContext_1 = require("../../clusterWizard/ClusterWizardContext");
const api_1 = require("../../../../common/api");
const slice_1 = require("../../../store/slices/current-cluster/slice");
const ClusterWizardFooter_1 = tslib_1.__importDefault(require("../../clusterWizard/ClusterWizardFooter"));
const ClusterWizardNavigation_1 = tslib_1.__importDefault(require("../../clusterWizard/ClusterWizardNavigation"));
const services_1 = require("../../../services");
const hooks_1 = require("../../../../common/hooks");
const selectors_1 = require("../../../store/slices/current-cluster/selectors");
const ReviewPreflightChecks_1 = tslib_1.__importDefault(require("./ReviewPreflightChecks"));
const ReviewSummary_1 = tslib_1.__importDefault(require("./ReviewSummary"));
require("./ReviewCluster.css");
const ReviewStep = ({ cluster }) => {
    const { addAlert, clearAlerts } = (0, common_1.useAlerts)();
    const clusterWizardContext = (0, ClusterWizardContext_1.useClusterWizardContext)();
    const { isViewerMode } = (0, react_redux_1.useSelector)(selectors_1.selectCurrentClusterPermissionsState);
    const [isStartingInstallation, setIsStartingInstallation] = (0, hooks_1.useStateSafely)(false);
    const dispatch = (0, react_redux_1.useDispatch)();
    const [disableInstallButton, setDisableInstallButton] = react_1.default.useState();
    react_1.default.useEffect(() => {
        var _a, _b, _c;
        const manifestFail = ((_a = cluster['last-installation-preparation']) === null || _a === void 0 ? void 0 : _a.status) === 'failed' &&
            ((_b = cluster['last-installation-preparation'].reason) === null || _b === void 0 ? void 0 : _b.includes('manifest'));
        const userDidntAddressManifestIssue = ((_c = clusterWizardContext.uiSettings) === null || _c === void 0 ? void 0 : _c.customManifestsUpdated) !== true;
        setDisableInstallButton((manifestFail && userDidntAddressManifestIssue) ||
            isViewerMode ||
            isStartingInstallation ||
            cluster.status !== 'ready');
    }, [cluster, isStartingInstallation, isViewerMode, clusterWizardContext]);
    const handleClusterInstall = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        var _a;
        clearAlerts();
        setIsStartingInstallation(true);
        try {
            const { data } = yield services_1.ClustersService.install(cluster.id, cluster.tags);
            dispatch((0, slice_1.updateCluster)(data));
            setIsStartingInstallation(false);
            if ((_a = clusterWizardContext.uiSettings) === null || _a === void 0 ? void 0 : _a.customManifestsUpdated) {
                void clusterWizardContext.updateUISettings({ customManifestsUpdated: undefined });
            }
            // If successful, backend changes cluster state which leads to unmounting the Wizard
            // If validation fails, the wizard stays on this step and shows alerts
        }
        catch (e) {
            (0, api_1.handleApiError)(e, () => addAlert({
                title: 'Failed to start cluster installation',
                message: (0, api_1.getApiErrorMessage)(e),
            }));
            setIsStartingInstallation(false);
        }
    });
    const footer = (react_1.default.createElement(ClusterWizardFooter_1.default, { cluster: cluster, onBack: () => clusterWizardContext.moveBack(), isSubmitting: isStartingInstallation, submittingText: "Starting installation...", additionalActions: react_1.default.createElement(react_core_1.ActionListItem, null,
            react_1.default.createElement(react_core_1.Button, { variant: react_core_1.ButtonVariant.primary, name: "install", "data-testid": "button-install-cluster", onClick: () => void handleClusterInstall(), isDisabled: disableInstallButton }, "Install cluster")) }));
    return (react_1.default.createElement(common_1.ClusterWizardStep, { navigation: react_1.default.createElement(ClusterWizardNavigation_1.default, { cluster: cluster }), footer: footer },
        react_1.default.createElement(react_core_1.Grid, { hasGutter: true },
            react_1.default.createElement(react_core_1.GridItem, null,
                react_1.default.createElement(common_1.ClusterWizardStepHeader, null, "Review and create")),
            react_1.default.createElement(ReviewPreflightChecks_1.default, { cluster: cluster }),
            react_1.default.createElement(ReviewSummary_1.default, { cluster: cluster }))));
};
exports.default = ReviewStep;
//# sourceMappingURL=ReviewStep.js.map