"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reorderNetworksByCurrentPrimary = exports.reorderNetworksForPrimary = void 0;
const ip_address_1 = require("ip-address");
const config_1 = require("../../../config");
const reorderByPrimaryCidr = (primaryCidr, values, setFieldValue) => {
    const isDualStack = values.stackType === config_1.DUAL_STACK;
    if (!isDualStack || !primaryCidr)
        return;
    const isPrimaryIPv6 = ip_address_1.Address6.isValid(primaryCidr);
    // Reorder Cluster Networks
    if (Array.isArray(values.clusterNetworks) && values.clusterNetworks.length >= 2) {
        const [first, second] = values.clusterNetworks;
        const firstClusterCidr = first === null || first === void 0 ? void 0 : first.cidr;
        const secondClusterCidr = second === null || second === void 0 ? void 0 : second.cidr;
        if (firstClusterCidr && secondClusterCidr) {
            const isFirstClusterIPv6 = ip_address_1.Address6.isValid(firstClusterCidr);
            const isSecondClusterIPv6 = ip_address_1.Address6.isValid(secondClusterCidr);
            if (isFirstClusterIPv6 !== isSecondClusterIPv6) {
                if ((isPrimaryIPv6 && !isFirstClusterIPv6 && isSecondClusterIPv6) ||
                    (!isPrimaryIPv6 && isFirstClusterIPv6 && !isSecondClusterIPv6)) {
                    const reordered = [values.clusterNetworks[1], values.clusterNetworks[0]];
                    setFieldValue('clusterNetworks', reordered, false);
                }
            }
        }
    }
    // Reorder Service Networks
    if (Array.isArray(values.serviceNetworks) && values.serviceNetworks.length >= 2) {
        const [first, second] = values.serviceNetworks;
        const firstServiceCidr = first === null || first === void 0 ? void 0 : first.cidr;
        const secondServiceCidr = second === null || second === void 0 ? void 0 : second.cidr;
        if (firstServiceCidr && secondServiceCidr) {
            const isFirstServiceIPv6 = ip_address_1.Address6.isValid(firstServiceCidr);
            const isSecondServiceIPv6 = ip_address_1.Address6.isValid(secondServiceCidr);
            if (isFirstServiceIPv6 !== isSecondServiceIPv6) {
                if ((isPrimaryIPv6 && !isFirstServiceIPv6 && isSecondServiceIPv6) ||
                    (!isPrimaryIPv6 && isFirstServiceIPv6 && !isSecondServiceIPv6)) {
                    const reordered = [values.serviceNetworks[1], values.serviceNetworks[0]];
                    setFieldValue('serviceNetworks', reordered, false);
                }
            }
        }
    }
};
const reorderNetworksForPrimary = (nextPrimaryCidr, values, setFieldValue) => {
    if (!nextPrimaryCidr)
        return;
    reorderByPrimaryCidr(nextPrimaryCidr, values, setFieldValue);
};
exports.reorderNetworksForPrimary = reorderNetworksForPrimary;
const reorderNetworksByCurrentPrimary = (values, setFieldValue) => {
    var _a, _b;
    const primaryCidr = (_b = (_a = values.machineNetworks) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.cidr;
    if (!primaryCidr)
        return;
    reorderByPrimaryCidr(primaryCidr, values, setFieldValue);
};
exports.reorderNetworksByCurrentPrimary = reorderNetworksByCurrentPrimary;
//# sourceMappingURL=reorderNetworks.js.map