"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateNumber = exports.networkWideValidationSchema = exports.getIsNotNetworkOrBroadcastAddressSchema = exports.getInMachineNetworkValidationSchema = exports.MAX_VLAN_ID = exports.MIN_VLAN_ID = exports.MAX_PREFIX_LENGTH = exports.MIN_PREFIX_LENGTH = void 0;
const tslib_1 = require("tslib");
const Yup = tslib_1.__importStar(require("yup"));
const dataTypes_1 = require("../../data/dataTypes");
const protocolVersion_1 = require("../../data/protocolVersion");
const machineNetwork_1 = require("../../data/machineNetwork");
const commonValidationSchemas_1 = require("../../commonValidationSchemas");
const REQUIRED_MESSAGE = 'A value is required';
const MUST_BE_A_NUMBER = 'Must be a number';
const ONLY_DIGITS_REGEX = /^\d+$/;
exports.MIN_PREFIX_LENGTH = 1;
exports.MAX_PREFIX_LENGTH = {
    ipv4: 32,
    ipv6: 128,
};
exports.MIN_VLAN_ID = 1;
exports.MAX_VLAN_ID = 4094;
const transformNumber = (originalValue) => {
    return isNaN(originalValue) ? null : originalValue;
};
const getInMachineNetworkValidationSchema = (protocolVersion, machineNetwork) => {
    return (0, commonValidationSchemas_1.getIpAddressInSubnetValidationSchema)(protocolVersion, (0, machineNetwork_1.getMachineNetworkCidr)(machineNetwork));
};
exports.getInMachineNetworkValidationSchema = getInMachineNetworkValidationSchema;
const getIsNotNetworkOrBroadcastAddressSchema = (protocolVersion, machineNetwork) => {
    return (0, commonValidationSchemas_1.getIpIsNotNetworkOrBroadcastAddressSchema)(protocolVersion, (0, machineNetwork_1.getMachineNetworkCidr)(machineNetwork));
};
exports.getIsNotNetworkOrBroadcastAddressSchema = getIsNotNetworkOrBroadcastAddressSchema;
const getMachineNetworkValidationSchema = (protocolVersion) => Yup.object().shape({
    ip: getIPValidationSchema(protocolVersion),
    prefixLength: Yup.number()
        .required('Prefix length is required')
        .min(1, `Prefix length must be more than or equal to 1`)
        .max(exports.MAX_PREFIX_LENGTH[protocolVersion], `Prefix length must be less than or equal to ${exports.MAX_PREFIX_LENGTH[protocolVersion]}`)
        .transform(transformNumber), //add casting to not get typescript error caused by nullable
});
const getIPValidationSchema = (protocolVersion) => {
    return (0, commonValidationSchemas_1.getIpAddressValidationSchema)(protocolVersion)
        .required(REQUIRED_MESSAGE)
        .concat((0, commonValidationSchemas_1.isNotReservedHostIPAddress)(protocolVersion));
};
const getDNSValidationSchema = (protocolType) => {
    if (protocolType === 'dualStack') {
        return (0, commonValidationSchemas_1.getMultipleIpAddressValidationSchema)()
            .required(REQUIRED_MESSAGE)
            .concat((0, commonValidationSchemas_1.isNotReservedHostDNSAddress)());
    }
    return (0, commonValidationSchemas_1.getMultipleIpAddressValidationSchema)(dataTypes_1.ProtocolVersion.ipv4)
        .required(REQUIRED_MESSAGE)
        .concat((0, commonValidationSchemas_1.isNotReservedHostDNSAddress)(dataTypes_1.ProtocolVersion.ipv4));
};
const getAddressDataValidationSchema = (protocolVersion, ipConfig) => {
    return Yup.object({
        machineNetwork: getMachineNetworkValidationSchema(protocolVersion),
        gateway: getIPValidationSchema(protocolVersion)
            .concat((0, exports.getInMachineNetworkValidationSchema)(protocolVersion, ipConfig.machineNetwork))
            .concat((0, exports.getIsNotNetworkOrBroadcastAddressSchema)(protocolVersion, ipConfig.machineNetwork)),
    });
};
exports.networkWideValidationSchema = Yup.lazy((values) => {
    const ipConfigsValidationSchemas = Yup.object({
        ipv4: getAddressDataValidationSchema(dataTypes_1.ProtocolVersion.ipv4, values.ipConfigs.ipv4),
        ipv6: (0, protocolVersion_1.showProtocolVersion)(values.protocolType, dataTypes_1.ProtocolVersion.ipv6)
            ? getAddressDataValidationSchema(dataTypes_1.ProtocolVersion.ipv6, values.ipConfigs.ipv6)
            : Yup.object(),
    });
    return Yup.object({
        useVlan: Yup.boolean(),
        vlanId: Yup.mixed().when('useVlan', {
            is: (useVlan) => useVlan,
            then: () => Yup.number()
                .required(MUST_BE_A_NUMBER)
                .min(1, `Must be more than or equal to 1`)
                .max(exports.MAX_VLAN_ID, `Must be less than or equal to ${exports.MAX_VLAN_ID}`)
                .test('not-number', MUST_BE_A_NUMBER, () => (0, exports.validateNumber)(values.vlanId))
                .nullable()
                .transform(transformNumber),
        }),
        protocolType: Yup.string(),
        dns: getDNSValidationSchema(values.protocolType),
        ipConfigs: ipConfigsValidationSchemas,
    });
});
const validateNumber = (vlanId) => {
    //We need to validate that value is a number(without letters) and is not an exponential number (ex: 1e2)
    return new RegExp(ONLY_DIGITS_REGEX).test((vlanId || '').toString());
};
exports.validateNumber = validateNumber;
//# sourceMappingURL=formViewNetworkWideValidationSchema.js.map