"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClusterDeploymentDetailsStep = exports.useDetailsFormik = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const formik_1 = require("formik");
const react_core_1 = require("@patternfly/react-core");
const common_1 = require("../../../../common");
const helpers_1 = require("../../helpers");
const ClusterDeploymentDetailsForm_1 = require("./ClusterDeploymentDetailsForm");
const helpers_2 = require("../helpers");
const use_translation_wrapper_1 = require("../../../../common/hooks/use-translation-wrapper");
const useDetailsFormik = ({ clusterDeployment, agentClusterInstall, agents, usedClusterNames, infraEnv, ocpVersions, }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const isEdit = !!clusterDeployment || !!agentClusterInstall;
    const cluster = react_1.default.useMemo(() => clusterDeployment && agentClusterInstall
        ? (0, helpers_1.getAICluster)({
            clusterDeployment,
            agentClusterInstall,
            agents,
            infraEnv,
        })
        : undefined, [agentClusterInstall, clusterDeployment, agents, infraEnv]);
    const initialValues = react_1.default.useMemo(() => {
        const initValues = (0, common_1.getClusterDetailsInitialValues)({
            managedDomains: [], // not supported
            cluster,
            ocpVersions,
        });
        const ocpVersion = ocpVersions.find((ocpVersion) => ocpVersion.value === initValues.openshiftVersion);
        return Object.assign(Object.assign({}, initValues), { networkType: (0, helpers_1.getNetworkType)(ocpVersion) });
    }, []);
    const validationSchema = react_1.default.useMemo(() => (0, common_1.getClusterDetailsValidationSchema)({
        usedClusterNames,
        pullSecretSet: isEdit,
        validateUniqueName: true,
        t,
    }), [usedClusterNames, isEdit, t]);
    return [initialValues, validationSchema];
};
exports.useDetailsFormik = useDetailsFormik;
const ClusterDeploymentDetailsStep = ({ clusterImages, clusterDeployment, agentClusterInstall, agents, usedClusterNames, onSaveDetails, isPreviewOpen, infraEnv, isNutanix, }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const { addAlert } = (0, common_1.useAlerts)();
    const { goToNextStep } = (0, react_core_1.useWizardContext)();
    const ocpVersions = (0, helpers_1.getOCPVersions)(clusterImages, isNutanix);
    const gridSpans = (0, helpers_2.getGridSpans)(isPreviewOpen);
    const [initialValues, validationSchema] = (0, exports.useDetailsFormik)({
        clusterDeployment,
        ocpVersions,
        agentClusterInstall,
        agents,
        usedClusterNames,
        infraEnv,
    });
    const handleSubmit = (values) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        try {
            yield onSaveDetails(values);
            yield goToNextStep();
        }
        catch (error) {
            addAlert({
                title: t('ai:Failed to save ClusterDeployment'),
                message: error,
            });
        }
    });
    return (react_1.default.createElement(formik_1.Formik, { initialValues: initialValues, validate: (0, common_1.getRichTextValidation)(validationSchema), onSubmit: handleSubmit },
        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, "Cluster Details")),
            react_1.default.createElement(react_core_1.GridItem, Object.assign({}, gridSpans),
                react_1.default.createElement(ClusterDeploymentDetailsForm_1.ClusterDeploymentDetailsFormWrapper, null,
                    react_1.default.createElement(ClusterDeploymentDetailsForm_1.ClusterDeploymentDetailsForm, { agentClusterInstall: agentClusterInstall, clusterDeployment: clusterDeployment, clusterImages: clusterImages, isNutanix: isNutanix }))))));
};
exports.ClusterDeploymentDetailsStep = ClusterDeploymentDetailsStep;
//# sourceMappingURL=ClusterDeploymentDetailsStep.js.map