"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serviceNetworksSchema = exports.clusterNetworksSchema = exports.IPv6ValidationSchema = exports.IPv4ValidationSchema = exports.dualStackValidationSchema = exports.serviceNetworkValidationSchema = exports.clusterNetworksValidationSchema = exports.machineNetworksValidationSchema = void 0;
const tslib_1 = require("tslib");
const Yup = tslib_1.__importStar(require("yup"));
const is_cidr_1 = tslib_1.__importDefault(require("is-cidr"));
const addressValidation_1 = require("./addressValidation");
const utils_1 = require("../utils");
const utils_2 = require("../components/ui/formik/utils");
const config_1 = require("../config");
exports.machineNetworksValidationSchema = Yup.array().of(Yup.object({ cidr: addressValidation_1.hostSubnetValidationSchema, clusterId: Yup.string() }));
const clusterNetworksValidationSchema = (t) => Yup.array().of(Yup.lazy((values) => Yup.object({
    cidr: (0, addressValidation_1.ipBlockValidationSchema)(undefined /* So far used in OCM only and so validated by backend */, t),
    hostPrefix: (0, addressValidation_1.hostPrefixValidationSchema)(values.cidr, t),
    clusterId: Yup.string(),
})));
exports.clusterNetworksValidationSchema = clusterNetworksValidationSchema;
const serviceNetworkValidationSchema = (t) => Yup.array().of(Yup.object({
    cidr: (0, addressValidation_1.ipBlockValidationSchema)(undefined /* So far used in OCM only and so validated by backend */, t),
    clusterId: Yup.string(),
}));
exports.serviceNetworkValidationSchema = serviceNetworkValidationSchema;
const dualStackValidationSchema = (field, t, openshiftVersion) => Yup.array()
    .max(2, t('ai:Maximum number of {{field}} subnets in dual stack is 2.', { field }))
    .test('dual-stack-ipv4', openshiftVersion && (0, utils_1.isMajorMinorVersionEqualOrGreater)(openshiftVersion, '4.12')
    ? t('ai:First network has to be a valid IPv4 or IPv6 subnet.')
    : t('ai:First network has to be IPv4 subnet.'), (values) => {
    // For OCP versions > 4.11, allow IPv6 as primary network
    if (openshiftVersion && (0, utils_1.isMajorMinorVersionEqualOrGreater)(openshiftVersion, '4.12')) {
        return !!(values === null || values === void 0 ? void 0 : values[0].cidr) && (is_cidr_1.default.v4(values[0].cidr) || is_cidr_1.default.v6(values[0].cidr));
    }
    // For older versions, require IPv4 as primary network
    return !!(values === null || values === void 0 ? void 0 : values[0].cidr) && is_cidr_1.default.v4(values[0].cidr);
})
    .test('dual-stack-unique-cidrs', t('ai:Provided {{field}} subnets must be unique.', { field }), (values) => {
    var _a, _b;
    if (!values || values.length < 2) {
        return true;
    }
    const first = ((_a = values[0]) === null || _a === void 0 ? void 0 : _a.cidr) || '';
    const second = ((_b = values[1]) === null || _b === void 0 ? void 0 : _b.cidr) || '';
    if (!first || !second) {
        return true;
    }
    const firstIsCidr = is_cidr_1.default.v4(first) || is_cidr_1.default.v6(first);
    const secondIsCidr = is_cidr_1.default.v4(second) || is_cidr_1.default.v6(second);
    if (!firstIsCidr || !secondIsCidr) {
        return true;
    }
    return first !== second;
})
    .test('dual-stack-opposite-families', t('ai:When two {{field}} values are provided, one must be IPv4 and the other IPv6.', {
    field,
}), (values) => {
    var _a, _b;
    if (!values || values.length < 2) {
        return true;
    }
    const a = ((_a = values[0]) === null || _a === void 0 ? void 0 : _a.cidr) || '';
    const b = ((_b = values[1]) === null || _b === void 0 ? void 0 : _b.cidr) || '';
    if (!a || !b) {
        return true;
    }
    const a4 = is_cidr_1.default.v4(a);
    const a6 = is_cidr_1.default.v6(a);
    const b4 = is_cidr_1.default.v4(b);
    const b6 = is_cidr_1.default.v6(b);
    if (!((a4 || a6) && (b4 || b6))) {
        return true;
    }
    return (a4 && b6) || (a6 && b4);
});
exports.dualStackValidationSchema = dualStackValidationSchema;
exports.IPv4ValidationSchema = Yup.array().test('single-stack', `All network subnets must be IPv4.`, (values) => (0, utils_2.allSubnetsIPv4)(values));
exports.IPv6ValidationSchema = Yup.array().test('single-stack-ipv6', 'All network subnets must be IPv6.', (values) => (0, utils_2.allSubnetsIPv6)(values));
const clusterNetworksSchema = (t, stackType, primaryCidr, openshiftVersion) => {
    const singleStackFamilySchema = primaryCidr && is_cidr_1.default.v6(primaryCidr) ? exports.IPv6ValidationSchema : exports.IPv4ValidationSchema;
    const stackFamilySchema = stackType === config_1.SINGLE_STACK
        ? singleStackFamilySchema
        : (0, exports.dualStackValidationSchema)(t('ai:cluster network'), t, openshiftVersion);
    return (0, exports.clusterNetworksValidationSchema)(t).concat(stackFamilySchema);
};
exports.clusterNetworksSchema = clusterNetworksSchema;
const serviceNetworksSchema = (t, stackType, primaryCidr, openshiftVersion) => {
    const singleStackFamilySchema = primaryCidr && is_cidr_1.default.v6(primaryCidr) ? exports.IPv6ValidationSchema : exports.IPv4ValidationSchema;
    const stackFamilySchema = stackType === config_1.SINGLE_STACK
        ? singleStackFamilySchema
        : (0, exports.dualStackValidationSchema)(t('ai:service network'), t, openshiftVersion);
    return (0, exports.serviceNetworkValidationSchema)(t).concat(stackFamilySchema);
};
exports.serviceNetworksSchema = serviceNetworksSchema;
//# sourceMappingURL=networkingValidation.js.map