"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const formik_1 = require("formik");
const config_1 = require("../../../config");
const ui_1 = require("../../ui");
const formik_2 = require("../../ui/formik");
const use_translation_wrapper_1 = require("../../../hooks/use-translation-wrapper");
const AdvancedNetworkFields = ({ isDisabled = false }) => {
    const { values, errors } = (0, formik_1.useFormikContext)();
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const isDualStack = values.stackType === config_1.DUAL_STACK;
    const clusterNetworkCidrPrefix = (index) => parseInt(((values.clusterNetworks && values.clusterNetworks[index].cidr) || '').split('/')[1]) || 1;
    const isSubnetIPv6 = (index) => (isDualStack ? !!index : false);
    const IPv4PrefixPopoverText = t('ai:For example, if Cluster Network Host Prefix is set to 23, then each node is assigned a /23 subnet out of the given cidr (clusterNetworkCIDR), which allows for 510 (2^(32 - 23) - 2) pod IPs addresses.');
    const IPv6PrefixPopoverText = t('ai:For example, if Cluster Network Host Prefix is set to 116, then each node is assigned a /116 subnet out of the given cidr (clusterNetworkCIDR), which allows for 4,094 (2^(128 - 116) - 2) pod IPs addresses.');
    const clusterPrefixHelperText = t('ai:Defines how big the subnets for each individual node are out of the given CIDR. Must enter a whole number.');
    const clusterCidrHelperText = t('ai:The block must not overlap with existing physical networks. To access the Pods from an external network, configure load balancers and routers to manage the traffic.');
    const serviceCidrHelperText = t('ai:Enter only 1 IP address pool. If you need to access the services from an external network, configure load balancers and routers to manage the traffic.');
    const clusterNetworkHostPrefixPopoverText = (index) => (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("p", null,
            t('ai:The subnet prefix length to assign to each individual node.'),
            ' ',
            isSubnetIPv6(index) ? IPv6PrefixPopoverText : IPv4PrefixPopoverText),
        react_1.default.createElement("p", null, t('ai:If you are required to provide access to nodes from an external network, configure load balancers and routers to manage the traffic.'))));
    return (react_1.default.createElement(react_core_1.Grid, { className: "pf-v6-u-ml-lg" },
        react_1.default.createElement(formik_1.FieldArray, { name: "clusterNetworks" }, () => {
            var _a;
            return (react_1.default.createElement(react_core_1.FormGroup, { fieldId: "clusterNetworks", labelInfo: isDualStack && t('ai:Primary') }, (_a = values.clusterNetworks) === null || _a === void 0 ? void 0 : _a.map((_, index) => {
                return (react_1.default.createElement(react_core_1.Grid, { key: index, className: "pf-v6-u-pb-md" },
                    react_1.default.createElement(react_core_1.GridItem, { className: 'network-field-group' },
                        react_1.default.createElement(formik_2.InputField, { name: `clusterNetworks.${index}.cidr`, label: react_1.default.createElement(react_1.default.Fragment, null,
                                react_1.default.createElement("span", null,
                                    t('ai:Cluster network CIDR'),
                                    " "),
                                react_1.default.createElement(ui_1.PopoverIcon, { bodyContent: t('ai:IP address blocks from which Pod IPs are allocated.') })), helperText: clusterCidrHelperText, isRequired: true, isDisabled: isDisabled, labelInfo: isDualStack ? (index === 0 ? t('ai:Primary') : t('ai:Secondary')) : '' })),
                    react_1.default.createElement(react_core_1.GridItem, { className: 'network-field-group' },
                        react_1.default.createElement(formik_2.InputField, { name: `clusterNetworks.${index}.hostPrefix`, label: react_1.default.createElement(react_1.default.Fragment, null,
                                react_1.default.createElement("span", null,
                                    t('ai:Cluster network host prefix'),
                                    " "),
                                react_1.default.createElement(ui_1.PopoverIcon, { bodyContent: clusterNetworkHostPrefixPopoverText(index), minWidth: "30rem" })), type: react_core_1.TextInputTypes.number, min: clusterNetworkCidrPrefix(index), max: isSubnetIPv6(index)
                                ? config_1.PREFIX_MAX_RESTRICTION.IPv6
                                : config_1.PREFIX_MAX_RESTRICTION.IPv4, helperText: clusterPrefixHelperText, isRequired: true, isDisabled: isDisabled }))));
            })));
        }),
        typeof errors.clusterNetworks === 'string' && (react_1.default.createElement(react_core_1.Alert, { variant: react_core_1.AlertVariant.warning, title: errors.clusterNetworks, isInline: true })),
        react_1.default.createElement(formik_1.FieldArray, { name: "serviceNetworks" }, () => {
            var _a;
            return (react_1.default.createElement(react_core_1.FormGroup, { fieldId: "serviceNetworks", labelInfo: isDualStack && t('ai:Primary') }, (_a = values.serviceNetworks) === null || _a === void 0 ? void 0 : _a.map((_, index) => (react_1.default.createElement(react_core_1.GridItem, { key: index, className: 'network-field-group' },
                react_1.default.createElement(formik_2.InputField, { name: `serviceNetworks.${index}.cidr`, label: react_1.default.createElement(react_1.default.Fragment, null,
                        react_1.default.createElement("span", null,
                            t('ai:Service network CIDR'),
                            " "),
                        react_1.default.createElement(ui_1.PopoverIcon, { bodyContent: t('ai:The IP address pool used for service IP addresses.') })), helperText: serviceCidrHelperText, isRequired: true, isDisabled: isDisabled, labelInfo: isDualStack ? (index === 0 ? t('ai:Primary') : t('ai:Secondary')) : '' }))))));
        }),
        typeof errors.serviceNetworks === 'string' && (react_1.default.createElement(react_core_1.Alert, { variant: react_core_1.AlertVariant.warning, title: errors.serviceNetworks, isInline: true }))));
};
exports.default = AdvancedNetworkFields;
//# sourceMappingURL=AdvancedNetworkFields.js.map