"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.richHostnameValidationSchema = 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 richHostnameValidationSchema = (t, usedNames, origName) => {
    const hostnameValidationMessagesList = (0, constants_1.hostnameValidationMessages)(t);
    return Yup.string()
        .min(1, hostnameValidationMessagesList.INVALID_LENGTH)
        .max(63, hostnameValidationMessagesList.INVALID_LENGTH)
        .test(hostnameValidationMessagesList.INVALID_START_END, hostnameValidationMessagesList.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.NAME_CHARS_REGEX, {
        message: hostnameValidationMessagesList.INVALID_VALUE,
        excludeEmptyString: true,
    })
        .test(hostnameValidationMessagesList.NOT_UNIQUE, hostnameValidationMessagesList.NOT_UNIQUE, (value) => {
        if (!value || value === origName) {
            return true;
        }
        return !usedNames.find((n) => n === value);
    })
        .notOneOf(constants_1.FORBIDDEN_HOSTNAMES, hostnameValidationMessagesList.LOCALHOST_ERR);
};
exports.richHostnameValidationSchema = richHostnameValidationSchema;
//# sourceMappingURL=hostnameValidation.js.map