"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.locationValidationSchema = exports.baseDomainValidationSchema = exports.dnsNameValidationSchema = exports.richNameValidationSchema = exports.nameValidationSchema = void 0;
const tslib_1 = require("tslib");
const Yup = tslib_1.__importStar(require("yup"));
const constants_1 = require("./constants");
const regexes_1 = require("./regexes");
const nameValidationSchema = (t, usedClusterNames, baseDnsDomain = '', validateUniqueName, isOcm = false) => {
    const clusterNameValidationMessagesList = (0, constants_1.clusterNameValidationMessages)(t);
    return Yup.string()
        .required(t('ai:Required field'))
        .matches(regexes_1.CLUSTER_NAME_VALID_CHARS_REGEX, {
        message: clusterNameValidationMessagesList.INVALID_VALUE,
        excludeEmptyString: true,
    })
        .matches(regexes_1.CLUSTER_NAME_START_END_REGEX, {
        message: clusterNameValidationMessagesList.INVALID_START_END,
        excludeEmptyString: true,
    })
        .min(isOcm ? 1 : 2, isOcm
        ? clusterNameValidationMessagesList.INVALID_LENGTH_OCM
        : clusterNameValidationMessagesList.INVALID_LENGTH_ACM)
        .max(constants_1.CLUSTER_NAME_MAX_LENGTH, isOcm
        ? clusterNameValidationMessagesList.INVALID_LENGTH_OCM
        : clusterNameValidationMessagesList.INVALID_LENGTH_ACM)
        .when('useRedHatDnsService', {
        is: (useRedHatDnsService) => useRedHatDnsService === true,
        then: (schema) => schema.test('is-name-unique', clusterNameValidationMessagesList.NOT_UNIQUE, (value) => {
            const clusterFullName = `${value || ''}.${baseDnsDomain}`;
            return !value || !usedClusterNames.includes(clusterFullName);
        }),
        otherwise: (schema) => schema.test('is-name-unique', clusterNameValidationMessagesList.NOT_UNIQUE, (value) => {
            // in CIM cluster name is ClusterDeployment CR name which must be unique
            return validateUniqueName ? !value || !usedClusterNames.includes(value) : true;
        }),
    });
};
exports.nameValidationSchema = nameValidationSchema;
const richNameValidationSchema = (t, usedNames, origName) => {
    const nameValidationMessagesList = (0, constants_1.nameValidationMessages)(t);
    return Yup.string()
        .min(1, nameValidationMessagesList.INVALID_LENGTH)
        .max(253, nameValidationMessagesList.INVALID_LENGTH)
        .test(nameValidationMessagesList.INVALID_START_END, nameValidationMessagesList.INVALID_START_END, (value) => {
        const trimmed = value === null || value === void 0 ? void 0 : value.trim();
        if (!trimmed) {
            return true;
        }
        return (!!trimmed[0].match(regexes_1.NAME_START_END_REGEX) &&
            (trimmed[trimmed.length - 1]
                ? !!trimmed[trimmed.length - 1].match(regexes_1.NAME_START_END_REGEX)
                : true));
    })
        .matches(regexes_1.HOST_NAME_REGEX, nameValidationMessagesList.INVALID_FORMAT)
        .matches(regexes_1.NAME_CHARS_REGEX, {
        message: nameValidationMessagesList.INVALID_VALUE,
        excludeEmptyString: true,
    })
        .test(nameValidationMessagesList.NOT_UNIQUE, nameValidationMessagesList.NOT_UNIQUE, (value) => {
        if (!value || value === origName) {
            return true;
        }
        return !usedNames.find((n) => n === value);
    })
        .notOneOf(constants_1.FORBIDDEN_HOSTNAMES, (0, constants_1.hostnameValidationMessages)(t).LOCALHOST_ERR);
};
exports.richNameValidationSchema = richNameValidationSchema;
const dnsNameValidationSchema = (t) => Yup.string()
    .test('dns-name-label-length', t('ai:Single label of the DNS name can not be longer than 63 characters.'), (value) => (value || '').split('.').every((label) => label.length <= 63))
    .matches(regexes_1.DNS_NAME_REGEX, {
    message: (value) => t('ai:Value "{{value}}" is not valid DNS name. Example: basedomain.example.com', { value }),
    excludeEmptyString: true,
});
exports.dnsNameValidationSchema = dnsNameValidationSchema;
const baseDomainValidationSchema = (t) => Yup.string().test('dns-name-label-length', t('ai:Every single host component in the base domain name cannot contain more than 63 characters and must not contain spaces.'), (value) => {
    // Check if the value contains any spaces
    if (/\s/.test(value)) {
        return false; // Value contains spaces, validation fails
    }
    // Check the label lengths
    const labels = (value || '').split('.');
    return labels.every((label) => label.length <= 63);
});
exports.baseDomainValidationSchema = baseDomainValidationSchema;
const locationValidationSchema = (t) => Yup.string()
    .min(1, (0, constants_1.locationValidationMessages)(t).INVALID_LENGTH)
    .max(63, (0, constants_1.locationValidationMessages)(t).INVALID_LENGTH)
    .test((0, constants_1.locationValidationMessages)(t).INVALID_START_END, (0, constants_1.locationValidationMessages)(t).INVALID_START_END, (value) => {
    const trimmed = value === null || value === void 0 ? void 0 : value.trim();
    if (!trimmed) {
        return true;
    }
    return (!!trimmed[0].match(regexes_1.ALPHANUMERIC_REGEX) &&
        (trimmed[trimmed.length - 1]
            ? !!trimmed[trimmed.length - 1].match(regexes_1.ALPHANUMERIC_REGEX)
            : true));
})
    .matches(regexes_1.LOCATION_CHARS_REGEX, {
    message: (0, constants_1.locationValidationMessages)(t).INVALID_VALUE,
    excludeEmptyString: true,
})
    .required(t('ai:Location is a required field.'));
exports.locationValidationSchema = locationValidationSchema;
//# sourceMappingURL=clusterDetailsValidation.js.map