"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importStar(require("react"));
const formik_1 = require("formik");
const is_cidr_1 = tslib_1.__importDefault(require("is-cidr"));
const react_core_1 = require("@patternfly/react-core");
const networkingSteps_1 = require("../../../common/components/clusterWizard/networkingSteps");
const common_1 = require("../../../common");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const NetworkTypeDropdown_1 = require("./networking/NetworkTypeDropdown");
const NetworkConfiguration = ({ cluster, hostSubnets, isVipDhcpAllocationDisabled, defaultNetworkSettings, hideManagedNetworking, }) => {
    var _a, _b, _c;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const { setFieldValue, values, touched, validateField } = (0, formik_1.useFormikContext)();
    const isMultiNodeCluster = !(0, common_1.isSNO)(cluster);
    const isDualStackSelectable = react_1.default.useMemo(() => (0, common_1.canBeDualStack)(hostSubnets), [hostSubnets]);
    const isDualStack = values.stackType === common_1.DUAL_STACK;
    const isSDNSelectable = react_1.default.useMemo(() => (0, common_1.canSelectNetworkTypeSDN)(!isMultiNodeCluster, isDualStack), [isMultiNodeCluster, isDualStack]);
    const [isAdvanced, setAdvanced] = react_1.default.useState(isDualStack || (0, common_1.isAdvNetworkConf)(cluster, defaultNetworkSettings));
    const toggleAdvConfiguration = (0, react_1.useCallback)((checked) => {
        var _a, _b;
        setAdvanced(checked);
        if (!checked) {
            const primaryCidr = (_b = (_a = values.machineNetworks) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.cidr;
            const useIPv6Defaults = primaryCidr && is_cidr_1.default.v6(primaryCidr);
            setFieldValue('clusterNetworks', useIPv6Defaults
                ? defaultNetworkSettings.clusterNetworksIpv6
                : defaultNetworkSettings.clusterNetworksIpv4);
            setFieldValue('serviceNetworks', useIPv6Defaults
                ? defaultNetworkSettings.serviceNetworksIpv6
                : defaultNetworkSettings.serviceNetworksIpv4);
        }
    }, [setFieldValue, defaultNetworkSettings, values.machineNetworks]);
    // Auto-enable advanced networking when switching to dual-stack
    (0, react_1.useEffect)(() => {
        if (isDualStack) {
            toggleAdvConfiguration(true);
        }
    }, [isDualStack, toggleAdvConfiguration]);
    // Sync cluster/service networks to match primary machine network family when single-stack (fix mismatch)
    (0, react_1.useEffect)(() => {
        var _a, _b, _c, _d;
        if (isDualStack) {
            return;
        }
        const primaryCidr = (_b = (_a = values.machineNetworks) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.cidr;
        const firstClusterCidr = (_d = (_c = values.clusterNetworks) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.cidr;
        if (!primaryCidr || !firstClusterCidr) {
            return;
        }
        const primaryIsIPv6 = is_cidr_1.default.v6(primaryCidr);
        const primaryIsIPv4 = is_cidr_1.default.v4(primaryCidr);
        const clusterIsIPv6 = is_cidr_1.default.v6(firstClusterCidr);
        const clusterIsIPv4 = is_cidr_1.default.v4(firstClusterCidr);
        if (primaryIsIPv4 && clusterIsIPv6) {
            setFieldValue('clusterNetworks', defaultNetworkSettings.clusterNetworksIpv4);
            setFieldValue('serviceNetworks', defaultNetworkSettings.serviceNetworksIpv4);
        }
        else if (primaryIsIPv6 && clusterIsIPv4) {
            setFieldValue('clusterNetworks', defaultNetworkSettings.clusterNetworksIpv6);
            setFieldValue('serviceNetworks', defaultNetworkSettings.serviceNetworksIpv6);
        }
    }, [
        isDualStack,
        values.machineNetworks,
        values.clusterNetworks,
        values.serviceNetworks,
        setFieldValue,
        defaultNetworkSettings,
    ]);
    const isUserManagedNetworking = values.managedNetworkingType === 'userManaged';
    const primaryMachineNetwork = (_b = (_a = values.machineNetworks) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.cidr;
    // Clear machine networks when switching to user-managed networking for multi-node clusters
    (0, react_1.useEffect)(() => {
        var _a;
        if (isUserManagedNetworking && isMultiNodeCluster && ((_a = values.machineNetworks) === null || _a === void 0 ? void 0 : _a.length)) {
            setFieldValue('machineNetworks', [], false);
        }
    }, [isUserManagedNetworking, isMultiNodeCluster, (_c = values.machineNetworks) === null || _c === void 0 ? void 0 : _c.length, setFieldValue]);
    (0, react_1.useEffect)(() => {
        if (!cluster.networkType) {
            setFieldValue('networkType', common_1.NETWORK_TYPE_OVN);
        }
        // Skipping "cluster.networkType" as it's ultimately the value we are setting in the form
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [isDualStack, isMultiNodeCluster, setFieldValue]);
    (0, react_1.useEffect)(() => {
        if (isUserManagedNetworking) {
            const shouldValidate = !!touched.machineNetworks;
            // We need to reset these fields' values in order to align with the values the server sends
            setFieldValue('vipDhcpAllocation', false);
            setFieldValue('ingressVips', [], shouldValidate);
            setFieldValue('apiVips', [], shouldValidate);
        }
        else {
            if (!values.vipDhcpAllocation) {
                validateField('ingressVips');
                validateField('apiVips');
            }
        }
    }, [
        touched.machineNetworks,
        isMultiNodeCluster,
        isUserManagedNetworking,
        setFieldValue,
        values.vipDhcpAllocation,
        validateField,
        primaryMachineNetwork,
    ]);
    return (react_1.default.createElement(react_core_1.Grid, { hasGutter: true },
        !hideManagedNetworking && react_1.default.createElement(networkingSteps_1.ManagedNetworkingControlGroup, null),
        isUserManagedNetworking && (react_1.default.createElement(networkingSteps_1.UserManagedNetworkingTextContent, { shouldDisplayLoadBalancersBullet: isMultiNodeCluster, docVersion: cluster.openshiftVersion })),
        react_1.default.createElement(NetworkTypeDropdown_1.NetworkTypeDropDown, { isSDNSelectable: isSDNSelectable }),
        (!isMultiNodeCluster || !isUserManagedNetworking) && (react_1.default.createElement(networkingSteps_1.StackTypeControlGroup, { clusterId: cluster.id, isDualStackSelectable: isDualStackSelectable, hostSubnets: hostSubnets, defaultNetworkValues: defaultNetworkSettings, allowSingleStackIPv6: true })),
        !(isMultiNodeCluster && isUserManagedNetworking) && (react_1.default.createElement(networkingSteps_1.AvailableSubnetsControl, { clusterId: cluster.id, hostSubnets: hostSubnets, isRequired: !isUserManagedNetworking, isDisabled: hostSubnets.length === 0, openshiftVersion: cluster.openshiftVersion, hosts: cluster.hosts || [], isMultiNodeCluster: isMultiNodeCluster, allowSingleStackIPv6: true })),
        !isUserManagedNetworking && (react_1.default.createElement(networkingSteps_1.VirtualIPControlGroup, { cluster: cluster, hostSubnets: hostSubnets, isVipDhcpAllocationDisabled: isVipDhcpAllocationDisabled })),
        react_1.default.createElement(react_core_1.Checkbox, { id: "useAdvancedNetworking", label: t('ai:Use advanced networking'), description: t('ai:Configure advanced networking properties (e.g. CIDR ranges).'), isChecked: isAdvanced, onChange: (_event, checked) => toggleAdvConfiguration(checked), isDisabled: isDualStack, body: isAdvanced && react_1.default.createElement(networkingSteps_1.AdvancedNetworkFields, null) })));
};
exports.default = NetworkConfiguration;
//# sourceMappingURL=NetworkConfiguration.js.map