"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClusterDeploymentReviewStep = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_core_1 = require("@patternfly/react-core");
const wizardTransition_1 = require("../wizardTransition");
const common_1 = require("../../../../common");
const helpers_1 = require("../../helpers");
const helpers_2 = require("../helpers");
const constants_1 = require("../constants");
const wizardTransition_2 = require("../wizardTransition");
const use_translation_wrapper_1 = require("../../../../common/hooks/use-translation-wrapper");
const ClusterDeploymentWizardContext_1 = require("../ClusterDeploymentWizardContext");
const ReviewConfigMapsTable_1 = require("./ReviewConfigMapsTable");
const ValidationSection_1 = require("../components/ValidationSection");
const ClusterDeploymentReviewStep = ({ agentClusterInstall, agents, onFinish, clusterDeployment, clusterImages, infraEnv, }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
    const { addAlert, clearAlerts } = (0, common_1.useAlerts)();
    const { activeStep, goToPrevStep, goToStepByName, close } = (0, react_core_1.useWizardContext)();
    const { syncError } = React.useContext(ClusterDeploymentWizardContext_1.ClusterDeploymentWizardContext);
    const [isSubmitting, setSubmitting] = React.useState(false);
    const cdName = (_a = clusterDeployment.metadata) === null || _a === void 0 ? void 0 : _a.name;
    const cdNamespace = (_b = clusterDeployment.metadata) === null || _b === void 0 ? void 0 : _b.namespace;
    const cpuArchitecture = (0, helpers_1.getClusterDeploymentCpuArchitecture)(clusterDeployment, infraEnv);
    const clusterAgents = React.useMemo(() => agents.filter((a) => (0, helpers_2.isAgentOfCluster)(a, cdName, cdNamespace)), [agents, cdName, cdNamespace]);
    const canContinue = (0, wizardTransition_1.canNextFromReviewStep)(agentClusterInstall, clusterAgents);
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const onNext = React.useCallback(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        clearAlerts();
        setSubmitting(true);
        try {
            yield onFinish();
        }
        catch (err) {
            const error = err;
            addAlert({ title: error.message || t('ai:An error occurred while starting installation.') });
        }
        finally {
            setSubmitting(false);
        }
    }), [addAlert, clearAlerts, onFinish, t]);
    const footer = React.useMemo(() => (React.createElement(react_core_1.WizardFooter, { activeStep: activeStep, onNext: onNext, isNextDisabled: isSubmitting || !canContinue || !!syncError, nextButtonText: t('ai:Install cluster'), nextButtonProps: { isLoading: isSubmitting }, onBack: goToPrevStep, onClose: close })), [activeStep, canContinue, close, goToPrevStep, isSubmitting, onNext, syncError, t]);
    (0, react_core_1.useWizardFooter)(footer);
    const openShiftVersion = (0, helpers_1.getSelectedVersion)(clusterImages, agentClusterInstall);
    const platform = (0, common_1.getPlatforms)(t)[(((_d = (_c = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _c === void 0 ? void 0 : _c.platformType) === null || _d === void 0 ? void 0 : _d.toLowerCase()) || 'baremetal')];
    const cluster = React.useMemo(() => (0, helpers_1.getAICluster)({ clusterDeployment, agentClusterInstall, agents: clusterAgents }), [clusterDeployment, agentClusterInstall, clusterAgents]);
    return (React.createElement(react_core_1.Grid, { hasGutter: true },
        React.createElement(react_core_1.GridItem, null,
            React.createElement(common_1.ClusterWizardStepHeader, null, "Review and create")),
        React.createElement(react_core_1.GridItem, null,
            React.createElement(common_1.DetailList, null,
                React.createElement(common_1.DetailItem, { title: t('ai:Cluster address'), value: `${((_e = clusterDeployment.metadata) === null || _e === void 0 ? void 0 : _e.name) || ''}.${((_f = clusterDeployment.spec) === null || _f === void 0 ? void 0 : _f.baseDomain) || ''}`, testId: "cluster-address" }),
                React.createElement(common_1.DetailItem, { title: t('ai:OpenShift version'), value: openShiftVersion, testId: "openshift-version" }),
                React.createElement(common_1.DetailItem, { title: t('ai:CPU architecture'), value: cpuArchitecture, testId: "cpu-architecture" }),
                React.createElement(common_1.DetailItem, { title: t('ai:External partner platform'), value: platform, testId: "platform-type" }),
                React.createElement(common_1.DetailItem, { title: t('ai:API IP (Primary)'), value: (_h = (_g = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.status) === null || _g === void 0 ? void 0 : _g.apiVIPs) === null || _h === void 0 ? void 0 : _h[0], testId: "api-vip-primary" }),
                ((_l = (_k = (_j = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.status) === null || _j === void 0 ? void 0 : _j.apiVIPs) === null || _k === void 0 ? void 0 : _k.length) !== null && _l !== void 0 ? _l : 0) > 1 ? (React.createElement(common_1.DetailItem, { title: t('ai:API IP (Secondary)'), value: (_o = (_m = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.status) === null || _m === void 0 ? void 0 : _m.apiVIPs) === null || _o === void 0 ? void 0 : _o[1], testId: "api-vip-secondary" })) : undefined,
                React.createElement(common_1.DetailItem, { title: t('ai:Ingress IP (Primary)'), value: (_q = (_p = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.status) === null || _p === void 0 ? void 0 : _p.ingressVIPs) === null || _q === void 0 ? void 0 : _q[0], testId: "ingress-vip-primary" }),
                ((_t = (_s = (_r = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.status) === null || _r === void 0 ? void 0 : _r.ingressVIPs) === null || _s === void 0 ? void 0 : _s.length) !== null && _t !== void 0 ? _t : 0) > 1 ? (React.createElement(common_1.DetailItem, { title: t('ai:Ingress IP (Secondary)'), value: (_v = (_u = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.status) === null || _u === void 0 ? void 0 : _u.ingressVIPs) === null || _v === void 0 ? void 0 : _v[1], testId: "ingress-vip-secondary" })) : undefined,
                React.createElement(common_1.DetailItem, { title: t('ai:Cluster summary'), testId: "cluster-summary", value: React.createElement(common_1.ReviewHostsInventory, { hosts: cluster.hosts }) }),
                !!((_x = (_w = agentClusterInstall.spec) === null || _w === void 0 ? void 0 : _w.manifestsConfigMapRefs) === null || _x === void 0 ? void 0 : _x.length) ? (React.createElement(common_1.DetailItem, { title: t('ai:Custom manifests'), value: React.createElement(ReviewConfigMapsTable_1.ReviewConfigMapsTable, { agentClusterInstall: agentClusterInstall }) })) : undefined,
                React.createElement(common_1.DetailItem, { title: t('ai:Cluster validations'), value: React.createElement(common_1.ClusterValidations, { validationsInfo: cluster.validationsInfo, setCurrentStepId: goToStepByName, wizardStepNames: (0, constants_1.wizardStepNames)(t), wizardStepsValidationsMap: wizardTransition_2.wizardStepsValidationsMap }), testId: "cluster-validations" }),
                React.createElement(common_1.DetailItem, { title: t('ai:Host validations'), value: React.createElement(common_1.HostsValidations, { hosts: cluster.hosts, setCurrentStepId: goToStepByName, wizardStepNames: (0, constants_1.wizardStepNames)(t), allClusterWizardSoftValidationIds: wizardTransition_2.allClusterWizardSoftValidationIds, wizardStepsValidationsMap: wizardTransition_2.wizardStepsValidationsMap }), testId: "host-validations" }))),
        syncError && (React.createElement(react_core_1.GridItem, null,
            React.createElement(ValidationSection_1.ValidationSection, { currentStepId: 'review', hosts: [] },
                React.createElement(react_core_1.Alert, { variant: react_core_1.AlertVariant.danger, title: t('ai:An error occurred'), isInline: true }, syncError))))));
};
exports.ClusterDeploymentReviewStep = ClusterDeploymentReviewStep;
//# sourceMappingURL=ClusterDeploymentReviewStep.js.map