"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.yamlViewValidationSchema = void 0;
const tslib_1 = require("tslib");
const Yup = tslib_1.__importStar(require("yup"));
const common_1 = require("../../../../../../common");
const commonValidationSchemas_1 = require("../../commonValidationSchemas");
const utils_1 = require("../../../../../../common/utils");
const networkYamlValidationSchema = (t) => Yup.string()
    .required('Required field')
    .test('file-size-limit', (0, utils_1.getMaxFileSizeMessage)(t), utils_1.validateFileSize)
    .test('file-type-yaml', (0, common_1.getIncorrectFileTypeMessage)(t), utils_1.validateFileType);
const getAllMacAddresses = (values) => {
    const allMacAddresses = [];
    for (const host of values.hosts) {
        if (!host.macInterfaceMap) {
            continue;
        }
        for (const mapItem of host.macInterfaceMap) {
            if (mapItem.macAddress) {
                allMacAddresses.push(mapItem.macAddress);
            }
        }
    }
    return allMacAddresses;
};
const getInterfaceNamesInCurrentHost = (values, context) => {
    const currentMacInterfaceObject = context.parent;
    const currentHost = values.hosts.find((currentHost) => currentHost.macInterfaceMap &&
        currentHost.macInterfaceMap.indexOf(currentMacInterfaceObject) > -1);
    if (!currentHost || !currentHost.macInterfaceMap) {
        return undefined;
    }
    return currentHost.macInterfaceMap.map((currentItem) => {
        return currentItem.logicalNicName ? currentItem.logicalNicName : '';
    });
};
const macInterfaceMapValidationSchema = (t) => Yup.array().of(Yup.object({
    macAddress: (0, common_1.macAddressValidationSchema)(t)
        .required('Required field')
        .concat((0, commonValidationSchemas_1.getUniqueValidationSchema)(getAllMacAddresses)),
    logicalNicName: Yup.string()
        .required('Required field')
        .concat((0, commonValidationSchemas_1.getUniqueValidationSchema)(getInterfaceNamesInCurrentHost))
        .max(15, 'Interface name must be 15 characters at most.')
        .matches(/^\S+$/, 'Interface name can not contain spaces.'),
}));
const yamlViewValidationSchema = (t) => Yup.object({
    hosts: Yup.array().of(Yup.object().shape({
        networkYaml: networkYamlValidationSchema(t),
        macInterfaceMap: macInterfaceMapValidationSchema(t),
    })),
});
exports.yamlViewValidationSchema = yamlViewValidationSchema;
//# sourceMappingURL=yamlViewValidationSchema.js.map