"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getArrayIndexFromPath = exports.alwaysRequired = exports.isIPv6Address = exports.isIPv4Address = exports.isIPorDN = void 0;
const tslib_1 = require("tslib");
const Yup = tslib_1.__importStar(require("yup"));
const ip_address_1 = require("ip-address");
const regexes_1 = require("./regexes");
const isIPorDN = (value, dnsRegex = regexes_1.DNS_NAME_REGEX) => {
    if (value.match(dnsRegex)) {
        return true;
    }
    else if (value) {
        return ip_address_1.Address4.isValid(value) || ip_address_1.Address6.isValid(value);
    }
    else {
        return false;
    }
};
exports.isIPorDN = isIPorDN;
// Helpers to classify literal IPs more robustly
const isIPv4Address = (ip) => {
    if (!ip)
        return false;
    return ip.includes('.') && ip_address_1.Address4.isValid(ip);
};
exports.isIPv4Address = isIPv4Address;
const isIPv6Address = (ip) => {
    if (!ip)
        return false;
    return ip.includes(':') && ip_address_1.Address6.isValid(ip);
};
exports.isIPv6Address = isIPv6Address;
const alwaysRequired = (message) => Yup.string().test('always-required', message || 'The value is required.', (value) => !!value);
exports.alwaysRequired = alwaysRequired;
const getArrayIndexFromPath = (path) => {
    const match = path.match(/\[(\d+)\][^\[]*$/); // Prefer the last [...] occurrence for nested array paths like "foo[0].bar[1].ip"
    return match ? parseInt(match[1], 10) : NaN;
};
exports.getArrayIndexFromPath = getArrayIndexFromPath;
//# sourceMappingURL=utils.js.map