"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OcmClusterDetailsFormFields = void 0;
const tslib_1 = require("tslib");
/* eslint-disable no-console */
const React = tslib_1.__importStar(require("react"));
const react_core_1 = require("@patternfly/react-core");
const formik_1 = require("formik");
const HostsNetworkConfigurationControlGroup_1 = require("./HostsNetworkConfigurationControlGroup");
const common_1 = require("../../../common");
const DiskEncryptionControlGroup_1 = tslib_1.__importDefault(require("../../../common/components/clusterConfiguration/DiskEncryptionFields/DiskEncryptionControlGroup"));
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const OcmFormFields_1 = require("../ui/OcmFormFields");
const OcmOpenShiftVersion_1 = tslib_1.__importDefault(require("./OcmOpenShiftVersion"));
const OcmOpenShiftVersionSelect_1 = tslib_1.__importDefault(require("./OcmOpenShiftVersionSelect"));
const CpuArchitectureDropdown_1 = tslib_1.__importDefault(require("./CpuArchitectureDropdown"));
const OcmBaseDomainField_1 = require("./OcmBaseDomainField");
const useSupportLevelsAPI_1 = tslib_1.__importDefault(require("../../hooks/useSupportLevelsAPI"));
const OpenShiftVersionsContext_1 = require("../clusterWizard/OpenShiftVersionsContext");
const ExternalPlatformDropdown_1 = require("./platformIntegration/ExternalPlatformDropdown");
const types_1 = require("../../services/types");
const newFeatureSupportLevels_1 = require("../../../common/components/newFeatureSupportLevels");
const constants_1 = require("./platformIntegration/constants");
const use_feature_1 = require("../../hooks/use-feature");
const ControlPlaneNodesDropdown_1 = tslib_1.__importStar(require("./ControlPlaneNodesDropdown"));
const OcmClusterDetailsFormFields = ({ managedDomains = [], versions, defaultPullSecret, isOcm, cluster, }) => {
    const clusterExists = !!cluster;
    const { values, setFieldValue } = (0, formik_1.useFormikContext)();
    const { useRedHatDnsService } = values;
    const nameInputRef = React.useRef();
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const isSingleClusterFeatureEnabled = (0, use_feature_1.useFeature)('ASSISTED_INSTALLER_SINGLE_CLUSTER_FEATURE');
    const { openshiftVersion, platform } = values;
    const { getCpuArchitectures } = (0, OpenShiftVersionsContext_1.useOpenShiftVersionsContext)();
    const cpuArchitecturesByVersionImage = getCpuArchitectures(openshiftVersion);
    const featureSupportLevelData = (0, useSupportLevelsAPI_1.default)('features', values.openshiftVersion, values.cpuArchitecture);
    const cpuArchitectures = React.useMemo(() => (0, common_1.getSupportedCpuArchitectures)(cpuArchitecturesByVersionImage), [cpuArchitecturesByVersionImage]);
    const cpuArchitecture = common_1.architectureData[values.cpuArchitecture].label;
    const featureSupportLevelContext = (0, newFeatureSupportLevels_1.useNewFeatureSupportLevel)();
    React.useEffect(() => {
        var _a;
        (_a = nameInputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
    }, []);
    const handleExternalPartnerIntegrationsChange = React.useCallback((selectedPlatform) => {
        const isOracleSelected = selectedPlatform === 'external';
        if (isOracleSelected) {
            setFieldValue('hostsNetworkConfigurationType', types_1.HostsNetworkConfigurationType.DHCP);
        }
    }, [setFieldValue]);
    React.useEffect(() => {
        setFieldValue('isCMNSupported', featureSupportLevelContext.isFeatureSupported('CLUSTER_MANAGED_NETWORKING', featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined), false);
        setFieldValue('isSNODevPreview', featureSupportLevelContext.getFeatureSupportLevel('SNO', featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined) === 'dev-preview');
    }, [setFieldValue, featureSupportLevelContext, featureSupportLevelData]);
    React.useEffect(() => {
        const currentCount = values.controlPlaneCount;
        const isNonStandardControlPlaneEnabled = featureSupportLevelContext.isFeatureSupported('NON_STANDARD_HA_CONTROL_PLANE', featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined);
        const isTnaEnabled = featureSupportLevelContext.isFeatureSupported('TNA', featureSupportLevelData !== null && featureSupportLevelData !== void 0 ? featureSupportLevelData : undefined);
        const isItemEnabled = (0, ControlPlaneNodesDropdown_1.isCPNDropdownItemEnabled)(currentCount, isNonStandardControlPlaneEnabled, isTnaEnabled);
        if (!isItemEnabled) {
            setFieldValue('controlPlaneCount', ControlPlaneNodesDropdown_1.DEFAULT_VALUE_CPN, false);
        }
    }, [
        values.openshiftVersion,
        featureSupportLevelData,
        featureSupportLevelContext,
        values.controlPlaneCount,
        setFieldValue,
    ]);
    return (React.createElement(react_core_1.Form, { id: "wizard-cluster-details__form" },
        React.createElement(OcmFormFields_1.OcmRichInputField, { ref: nameInputRef, label: "Cluster name", name: "name", placeholder: isOcm ? '' : 'Enter cluster name', isRequired: true, richValidationMessages: useRedHatDnsService
                ? (0, common_1.uniqueOcmClusterNameValidationMessages)(t)
                : (0, common_1.ocmClusterNameValidationMessages)(t), maxLength: common_1.CLUSTER_NAME_MAX_LENGTH }),
        React.createElement(OcmBaseDomainField_1.OcmBaseDomainField, { managedDomains: managedDomains, clusterExists: clusterExists }),
        clusterExists || isSingleClusterFeatureEnabled ? (React.createElement(OcmOpenShiftVersion_1.default, { versions: versions, openshiftVersion: values.openshiftVersion, clusterCpuArchitecture: values.cpuArchitecture, withPreviewText: true, withMultiText: true })) : (React.createElement(OcmOpenShiftVersionSelect_1.default, null)),
        clusterExists || isSingleClusterFeatureEnabled ? (React.createElement(common_1.StaticTextField, { name: "cpuArchitecture", label: "CPU architecture", isRequired: true }, cpuArchitecture)) : (React.createElement(CpuArchitectureDropdown_1.default, { openshiftVersion: openshiftVersion, cpuArchitectures: cpuArchitectures })),
        !(cluster === null || cluster === void 0 ? void 0 : cluster.pullSecretSet) && (React.createElement(common_1.PullSecret, { isOcm: isOcm, defaultPullSecret: defaultPullSecret, isSingleClusterFeatureEnabled: isSingleClusterFeatureEnabled })),
        !isSingleClusterFeatureEnabled &&
            (clusterExists ? (React.createElement(common_1.StaticTextField, { name: "platform", label: "Integrate with external partner platforms", isRequired: true }, constants_1.ExternalPlatformLabels[values.platform])) : (React.createElement(ExternalPlatformDropdown_1.ExternalPlatformDropdown, { onChange: handleExternalPartnerIntegrationsChange, cpuArchitecture: values.cpuArchitecture, featureSupportLevelData: featureSupportLevelData, isSNO: values.controlPlaneCount === 1 }))),
        clusterExists ? (React.createElement(common_1.StaticTextField, { name: "controlPlaneCount", label: React.createElement(ControlPlaneNodesDropdown_1.ControlPlaneNodesLabel, null) }, values.controlPlaneCount)) : (React.createElement(ControlPlaneNodesDropdown_1.default, { featureSupportLevelData: featureSupportLevelData })),
        !isSingleClusterFeatureEnabled && (React.createElement(HostsNetworkConfigurationControlGroup_1.HostsNetworkConfigurationControlGroup, { clusterExists: clusterExists, isDisabled: platform === 'external' })),
        React.createElement(DiskEncryptionControlGroup_1.default, { values: values, isDisabled: cluster === null || cluster === void 0 ? void 0 : cluster.pullSecretSet, isSNO: values.controlPlaneCount === 1, docVersion: openshiftVersion })));
};
exports.OcmClusterDetailsFormFields = OcmClusterDetailsFormFields;
//# sourceMappingURL=OcmClusterDetailsFormFields.js.map