"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.noProxyValidationSchema = exports.httpProxyValidationSchema = void 0;
const tslib_1 = require("tslib");
const Yup = tslib_1.__importStar(require("yup"));
const utils_1 = require("../components/ui/formik/utils");
const regexes_1 = require("./regexes");
const utils_2 = require("./utils");
const httpProxyValidationSchema = ({ values, pairValueName, allowEmpty, t, }) => {
    const validation = Yup.string().test('http-proxy-validation', t('ai:Provide a valid HTTP URL.'), (value) => {
        if (!value) {
            return true;
        }
        if (!value.startsWith('http://')) {
            return false;
        }
        try {
            new URL(value);
        }
        catch (_a) {
            return false;
        }
        return true;
    });
    if (allowEmpty) {
        return validation;
    }
    return validation.test('http-proxy-no-empty-validation', t('ai:At least one of the HTTP or HTTPS proxy URLs is required.'), (value) => !values.enableProxy || !!value || !!values[pairValueName]);
};
exports.httpProxyValidationSchema = httpProxyValidationSchema;
const noProxyValidationSchema = (t) => Yup.string().test('no-proxy-validation', t('ai:Provide a comma separated list of valid DNS names or IP addresses.'), (value) => {
    if (!value || value === '*') {
        return true;
    }
    // https://docs.openshift.com/container-platform/4.5/installing/installing_bare_metal/installing-restricted-networks-bare-metal.html#installation-configure-proxy_installing-restricted-networks-bare-metal
    // A comma-separated list of destination domain names, domains, IP addresses, or other network CIDRs to exclude proxying.
    // Preface a domain with . to match subdomains only. For example, .y.com matches x.y.com, but not y.com.
    // Use * to bypass proxy for all destinations.
    const noProxyList = (0, utils_1.trimCommaSeparatedList)(value).split(',');
    return noProxyList.every((p) => (0, utils_2.isIPorDN)(p, regexes_1.PROXY_DNS_REGEX));
});
exports.noProxyValidationSchema = noProxyValidationSchema;
//# sourceMappingURL=proxyValidation.js.map