"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFormViewHostsValidationSchema = void 0;
const tslib_1 = require("tslib");
const Yup = tslib_1.__importStar(require("yup"));
const dataTypes_1 = require("../../data/dataTypes");
const common_1 = require("../../../../../../common");
const protocolVersion_1 = require("../../data/protocolVersion");
const formViewNetworkWideValidationSchema_1 = require("../FormViewNetworkWide/formViewNetworkWideValidationSchema");
const commonValidationSchemas_1 = require("../../commonValidationSchemas");
const machineNetwork_1 = require("../../data/machineNetwork");
const requiredMsg = 'A value is required';
const getAllIpv4Addresses = (values) => values.hosts.map((host) => host.ips.ipv4);
const getAllIpv6Addresses = (values) => values.hosts.map((host) => host.ips.ipv6);
const getAllMacAddresses = (values) => {
    return values.hosts.map((host) => host.macAddress);
};
const getAllBondInterfaces = (values) => {
    return values.hosts.flatMap((host) => [
        host.bondPrimaryInterface.toLowerCase(),
        host.bondSecondaryInterface.toLowerCase(),
    ]);
};
const getHostValidationSchema = (networkWideValues, t) => Yup.object({
    macAddress: Yup.mixed().when('useBond', {
        is: false,
        then: () => (0, common_1.macAddressValidationSchema)(t)
            .required(requiredMsg)
            .concat((0, commonValidationSchemas_1.getUniqueValidationSchema)(getAllMacAddresses)),
        otherwise: () => Yup.mixed().notRequired(),
    }),
    ips: Yup.object({
        ipv4: (0, protocolVersion_1.showIpv4)(networkWideValues.protocolType)
            ? (0, formViewNetworkWideValidationSchema_1.getInMachineNetworkValidationSchema)(dataTypes_1.ProtocolVersion.ipv4, networkWideValues.ipConfigs['ipv4'].machineNetwork)
                .required(requiredMsg)
                .concat((0, commonValidationSchemas_1.getUniqueValidationSchema)(getAllIpv4Addresses))
                .concat((0, commonValidationSchemas_1.getIpIsNotNetworkOrBroadcastAddressSchema)(dataTypes_1.ProtocolVersion.ipv4, (0, machineNetwork_1.getMachineNetworkCidr)(networkWideValues.ipConfigs['ipv4'].machineNetwork)))
            : Yup.string(),
        ipv6: (0, protocolVersion_1.showIpv6)(networkWideValues.protocolType)
            ? (0, formViewNetworkWideValidationSchema_1.getInMachineNetworkValidationSchema)(dataTypes_1.ProtocolVersion.ipv6, networkWideValues.ipConfigs['ipv6'].machineNetwork)
                .required(requiredMsg)
                .concat((0, commonValidationSchemas_1.getUniqueValidationSchema)(getAllIpv6Addresses))
                .concat((0, commonValidationSchemas_1.getIpIsNotNetworkOrBroadcastAddressSchema)(dataTypes_1.ProtocolVersion.ipv6, (0, machineNetwork_1.getMachineNetworkCidr)(networkWideValues.ipConfigs['ipv6'].machineNetwork)))
            : Yup.string(),
    }),
    bondPrimaryInterface: Yup.mixed().when('useBond', {
        is: true,
        then: () => (0, common_1.macAddressValidationSchema)(t)
            .required(requiredMsg)
            .concat((0, commonValidationSchemas_1.getUniqueValidationSchema)(getAllBondInterfaces)),
        otherwise: () => Yup.mixed().notRequired(),
    }),
    bondSecondaryInterface: Yup.mixed().when('useBond', {
        is: true,
        then: () => (0, common_1.macAddressValidationSchema)(t)
            .required(requiredMsg)
            .concat((0, commonValidationSchemas_1.getUniqueValidationSchema)(getAllBondInterfaces)),
        otherwise: () => Yup.mixed().notRequired(),
    }),
});
const getFormViewHostsValidationSchema = (networkWideValues, t) => {
    return Yup.object().shape({
        hosts: Yup.array(getHostValidationSchema(networkWideValues, t)),
    });
};
exports.getFormViewHostsValidationSchema = getFormViewHostsValidationSchema;
//# sourceMappingURL=formViewHostsValidationSchema.js.map