"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateRandomAddressInCIDR = void 0;
function generateRandomAddressInCIDR(cidr) {
    const [ip, mask] = cidr.split('/');
    const sections = ip.split('.');
    /**
     * Since allowed `Mask` values are multiples of 8,
     * we can divide it by 8 to determine which sections
     * of the IP address can be changed according to the CIDR
     * specification.
     *
     * @example
     * Given the CIDR 10.8.0.0/8, the first usable IP is 10.8.0.1
     * and the last one is 10.255.255.254
     *
     * This means that we can start at index 1 (8/8)
     *
     * @example
     * Given the CIDR 10.8.0.0/16, the first usable IP is 10.8.0.1
     * and the last one is 10.8.255.254
     *
     * This means that we can start at index 2 (16/8)
     *
     * @example
     * Given the CIDR 10.8.0.0/24, the first usable IP is 10.8.0.1
     * and the last one is 10.8.0.254
     *
     * This means that we can start at index 3 (24/8)
     *
     * @example
     * Given the CIDR 10.8.0.0/32, the only usable IP is 10.8.0.0
     *
     * This means that we can start at index 4 (32/8) which turns this into
     * a no-op.
     */
    const firstSectionIndexToEdit = mask / 8;
    for (let i = firstSectionIndexToEdit; i < sections.length; i++) {
        const isLastSection = i === sections.length - 1;
        sections[i] = isLastSection
            ? `${generateRandomIntInRange(1, 254)}`
            : `${generateRandomIntInRange(0, 255)}`;
    }
    return sections.join('.');
}
exports.generateRandomAddressInCIDR = generateRandomAddressInCIDR;
/**
 * Generates a random integer within the provided range (inclusive).
 */
function generateRandomIntInRange(min, max) {
    const difference = max + 1 - min;
    return Math.floor(Math.random() * difference) + min;
}
