"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VirtualIPControlGroup = 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 types_1 = require("../../../types");
const config_1 = require("../../../config");
const selectors_1 = require("../../../selectors");
const clusterConfiguration_1 = require("../../clusterConfiguration");
const ui_1 = require("../../ui");
const formik_2 = require("../../ui/formik");
const use_translation_wrapper_1 = require("../../../hooks/use-translation-wrapper");
const NewFeatureSupportLevelBadge_1 = tslib_1.__importDefault(require("../../newFeatureSupportLevels/NewFeatureSupportLevelBadge"));
const VipStaticValue = ({ vipValue, cluster, validationErrorMessage, id = 'vip-static', }) => {
    const { vipDhcpAllocation } = cluster;
    const machineNetworkCidr = (0, selectors_1.selectMachineNetworkCIDR)(cluster);
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    if (vipDhcpAllocation && !!vipValue) {
        return react_1.default.createElement("span", { id: `${id}-allocated` }, vipValue);
    }
    if (vipDhcpAllocation && validationErrorMessage) {
        return (react_1.default.createElement(react_core_1.Alert, { id: `${id}-alert-allocation-failed`, variant: react_core_1.AlertVariant.danger, title: t('ai:The DHCP server failed to allocate the IP'), isInline: true }, validationErrorMessage));
    }
    if (vipDhcpAllocation && machineNetworkCidr) {
        return (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(react_core_1.Spinner, { size: "md", id: `${id}-allocating`, "data-testid": `vip-static-${id}-value-spinner` }),
            react_1.default.createElement("i", null, t('ai:This IP is being allocated by the DHCP server'))));
    }
    return react_1.default.createElement("i", null, t('ai:This IP will be allocated by the DHCP server'));
};
const VirtualIPControlGroup = ({ cluster, hostSubnets, isVipDhcpAllocationDisabled, supportLevel, isViewerMode = false, }) => {
    var _a, _b;
    const { values, setFieldValue } = (0, formik_1.useFormikContext)();
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const apiVipPopoverContent = t('ai:Provides an endpoint for users, both human and machine, to interact with and configure the platform. If needed, contact your IT manager for more information.');
    const ingressVipPopoverContent = t('ai:Provides an endpoint for application traffic flowing in from outside the cluster. If needed, contact your IT manager for more information.');
    const { 'api-vips-defined': apiVipFailedValidationMessage, 'ingress-vips-defined': ingressVipFailedValidationMessage, } = react_1.default.useMemo(() => (0, clusterConfiguration_1.getVipValidationsById)(t, cluster.validationsInfo), [t, cluster.validationsInfo]);
    const enableAllocation = values.networkType === types_1.NETWORK_TYPE_SDN;
    const isDualStack = values.stackType === config_1.DUAL_STACK;
    const isVipInputDisabled = !hostSubnets.length || !((_b = (_a = values.machineNetworks) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.cidr);
    react_1.default.useEffect(() => {
        if (!isViewerMode && !enableAllocation) {
            setFieldValue('vipDhcpAllocation', false);
        }
    }, [enableAllocation, isViewerMode, setFieldValue]);
    // Ensure VIP arrays exist when (re-)entering cluster-managed networking.
    react_1.default.useEffect(() => {
        if (isViewerMode ||
            values.managedNetworkingType !== 'clusterManaged' ||
            values.vipDhcpAllocation) {
            return;
        }
        const vipCount = values.stackType === config_1.DUAL_STACK ? 2 : 1;
        const blankVipEntriesArray = Array.from({ length: vipCount }, () => ({
            ip: '',
            clusterId: cluster.id,
        }));
        const apiVipsEmpty = !values.apiVips || values.apiVips.length === 0;
        const ingressVipsEmpty = !values.ingressVips || values.ingressVips.length === 0;
        if (apiVipsEmpty) {
            setFieldValue('apiVips', blankVipEntriesArray, false);
        }
        if (ingressVipsEmpty) {
            setFieldValue('ingressVips', blankVipEntriesArray, false);
        }
    }, [
        cluster.id,
        isViewerMode,
        setFieldValue,
        values.apiVips,
        values.ingressVips,
        values.managedNetworkingType,
        values.stackType,
        values.vipDhcpAllocation,
    ]);
    const onChangeDhcp = react_1.default.useCallback((hasDhcp) => {
        // We need to sync the values back to the form
        setFieldValue('apiVips', hasDhcp ? [] : cluster.apiVips, true);
        setFieldValue('ingressVips', hasDhcp ? [] : cluster.ingressVips, true);
    }, [cluster.apiVips, cluster.ingressVips, setFieldValue]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        !isVipDhcpAllocationDisabled && (react_1.default.createElement(formik_2.CheckboxField, { onChange: onChangeDhcp, label: react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(react_core_1.Tooltip, { hidden: enableAllocation, content: t("ai:A cluster with OVN networking type cannot use 'allocate IPs via DHCP server'") },
                    react_1.default.createElement("span", null, t('ai:Allocate IPs via DHCP server'))),
                react_1.default.createElement(NewFeatureSupportLevelBadge_1.default, { featureId: "VIP_AUTO_ALLOC", supportLevel: supportLevel })), name: "vipDhcpAllocation", isDisabled: !enableAllocation || isViewerMode })),
        values.vipDhcpAllocation ? (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(ui_1.FormikStaticField, { label: react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement("span", null, t('ai:API IP')),
                    " ",
                    react_1.default.createElement(ui_1.PopoverIcon, { bodyContent: apiVipPopoverContent })), name: "apiVips.0.ip", value: (0, selectors_1.selectApiVip)(values), isValid: !apiVipFailedValidationMessage, isRequired: true },
                react_1.default.createElement(VipStaticValue, { id: "vip-api", vipValue: (0, selectors_1.selectApiVip)(cluster), cluster: cluster, validationErrorMessage: apiVipFailedValidationMessage })),
            react_1.default.createElement(ui_1.FormikStaticField, { label: react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement("span", null, t('ai:Ingress IP')),
                    ' ',
                    react_1.default.createElement(ui_1.PopoverIcon, { bodyContent: ingressVipPopoverContent })), name: "ingressVips.0.ip", value: (0, selectors_1.selectIngressVip)(values), isValid: !ingressVipFailedValidationMessage, isRequired: true },
                react_1.default.createElement(VipStaticValue, { id: "vip-ingress", vipValue: (0, selectors_1.selectIngressVip)(cluster), cluster: cluster, validationErrorMessage: ingressVipFailedValidationMessage })))) : (react_1.default.createElement(react_core_1.FormGroup, null,
            react_1.default.createElement(formik_1.FieldArray, { name: "vips" }, () => (react_1.default.createElement(react_core_1.Stack, null,
                react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(formik_2.InputField, { label: react_1.default.createElement(react_1.default.Fragment, null,
                            react_1.default.createElement("span", null, t('ai:API IP')),
                            ' ',
                            react_1.default.createElement(ui_1.PopoverIcon, { bodyContent: apiVipPopoverContent })), name: "apiVips.0.ip", isRequired: true, maxLength: 45, isDisabled: isVipInputDisabled || isViewerMode, labelInfo: isDualStack ? t('ai:Primary') : undefined })),
                isDualStack && (react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(formik_2.InputField, { label: react_1.default.createElement(react_1.default.Fragment, null,
                            react_1.default.createElement("span", null, t('ai:API IP')),
                            ' ',
                            react_1.default.createElement(ui_1.PopoverIcon, { bodyContent: apiVipPopoverContent })), name: "apiVips.1.ip", maxLength: 45, isDisabled: isVipInputDisabled || isViewerMode, labelInfo: t('ai:Secondary') }))),
                react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(formik_2.InputField, { name: "ingressVips.0.ip", label: react_1.default.createElement(react_1.default.Fragment, null,
                            react_1.default.createElement("span", null, t('ai:Ingress IP')),
                            ' ',
                            react_1.default.createElement(ui_1.PopoverIcon, { bodyContent: ingressVipPopoverContent })), isRequired: true, maxLength: 45, isDisabled: isVipInputDisabled || isViewerMode, labelInfo: isDualStack ? t('ai:Primary') : undefined })),
                isDualStack && (react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(formik_2.InputField, { name: "ingressVips.1.ip", label: react_1.default.createElement(react_1.default.Fragment, null,
                            react_1.default.createElement("span", null, t('ai:Ingress IP')),
                            ' ',
                            react_1.default.createElement(ui_1.PopoverIcon, { bodyContent: ingressVipPopoverContent })), maxLength: 45, isDisabled: isVipInputDisabled || isViewerMode, labelInfo: t('ai:Secondary') }))))))))));
};
exports.VirtualIPControlGroup = VirtualIPControlGroup;
//# sourceMappingURL=VirtualIPControlGroup.js.map