"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNetworkConfigurationValidationSchema = exports.getNetworkInitialValues = void 0;
const tslib_1 = require("tslib");
const Yup = tslib_1.__importStar(require("yup"));
const common_1 = require("../../../../common");
const getNetworkInitialValues = (cluster, defaultNetworkValues, isClusterManagedNetworkingUnsupported) => {
    var _a, _b, _c;
    const isSNOCluster = (0, common_1.isSNO)(cluster);
    const isDualStackType = (0, common_1.isDualStack)(cluster);
    return {
        apiVips: cluster.apiVips,
        ingressVips: cluster.ingressVips,
        sshPublicKey: cluster.sshPublicKey || '',
        vipDhcpAllocation: cluster.vipDhcpAllocation,
        managedNetworkingType: ((_a = cluster.platform) === null || _a === void 0 ? void 0 : _a.type) === 'none' ||
            (cluster.apiVips && cluster.apiVips.length === 0 && isClusterManagedNetworkingUnsupported) ||
            isSNOCluster
            ? 'userManaged'
            : 'clusterManaged',
        networkType: cluster.networkType || common_1.NETWORK_TYPE_OVN,
        machineNetworks: cluster.machineNetworks || [],
        stackType: isDualStackType ? common_1.DUAL_STACK : common_1.SINGLE_STACK,
        clusterNetworks: cluster.clusterNetworks ||
            (isDualStackType
                ? defaultNetworkValues.clusterNetworksDualstack
                : (_b = defaultNetworkValues.clusterNetworksIpv4) === null || _b === void 0 ? void 0 : _b.map((network) => (Object.assign(Object.assign({}, network), { clusterId: cluster === null || cluster === void 0 ? void 0 : cluster.id })))),
        serviceNetworks: cluster.serviceNetworks ||
            (isDualStackType
                ? defaultNetworkValues.serviceNetworksDualstack
                : (_c = defaultNetworkValues.serviceNetworksIpv4) === null || _c === void 0 ? void 0 : _c.map((network) => (Object.assign(Object.assign({}, network), { clusterId: cluster === null || cluster === void 0 ? void 0 : cluster.id })))),
    };
};
exports.getNetworkInitialValues = getNetworkInitialValues;
const getNetworkConfigurationValidationSchema = (initialValues, hostSubnets, t, openshiftVersion) => Yup.lazy((values) => Yup.object().shape({
    apiVips: (0, common_1.vipArrayValidationSchema)(hostSubnets, values, t),
    ingressVips: (0, common_1.vipArrayValidationSchema)(hostSubnets, values, t),
    sshPublicKey: (0, common_1.sshPublicKeyValidationSchema)(t),
    machineNetworks: values.managedNetworkingType === 'userManaged'
        ? Yup.array()
        : common_1.machineNetworksValidationSchema.when('stackType', {
            is: (stackType) => stackType === common_1.SINGLE_STACK,
            then: () => common_1.machineNetworksValidationSchema.concat(common_1.IPv4ValidationSchema),
            otherwise: () => {
                var _a;
                return values.machineNetworks && ((_a = values.machineNetworks) === null || _a === void 0 ? void 0 : _a.length) >= 2
                    ? common_1.machineNetworksValidationSchema.concat((0, common_1.dualStackValidationSchema)('machine networks', t, openshiftVersion))
                    : Yup.array();
            },
        }),
    clusterNetworks: (0, common_1.clusterNetworksValidationSchema)(t).when('stackType', {
        is: (stackType) => stackType === common_1.SINGLE_STACK,
        then: (schema) => schema.concat(common_1.IPv4ValidationSchema),
        otherwise: (schema) => schema.concat((0, common_1.dualStackValidationSchema)('cluster network', t, openshiftVersion)),
    }),
    serviceNetworks: (0, common_1.serviceNetworkValidationSchema)(t).when('stackType', {
        is: (stackType) => stackType === common_1.SINGLE_STACK,
        then: (schema) => schema.concat(common_1.IPv4ValidationSchema),
        otherwise: (schema) => schema.concat((0, common_1.dualStackValidationSchema)('service network', t, openshiftVersion)),
    }),
}));
exports.getNetworkConfigurationValidationSchema = getNetworkConfigurationValidationSchema;
//# sourceMappingURL=networkConfigurationValidation.js.map