import IPCIDR from "ip-cidr";
import net from "node:net";
function normalizeIp(ip) {
    let normalized = ip.split(",")[0]?.trim();
    if (!normalized) return null;
    if (normalized.startsWith("[") && normalized.includes("]")) {
        normalized = normalized.slice(1, normalized.indexOf("]"));
    }
    if (net.isIP(normalized)) return normalized;
    const ipv4WithPort = normalized.match(/^(\d{1,3}(?:\.\d{1,3}){3}):\d+$/);
    if (ipv4WithPort && net.isIPv4(ipv4WithPort[1])) return ipv4WithPort[1];
    return null;
}
export function getIpHash(ip) {
    const normalized = normalizeIp(ip);
    if (!normalized) return "ip-invalid";
    // because a single person may control many IPv6 addresses,
    // only a /64 subnet prefix of any IP will be taken into account.
    // (this means for IPv4 the entire address is used)
    const prefix = IPCIDR.createAddress(normalized).mask(64);
    return `ip-${BigInt(`0b${prefix}`).toString(36)}`;
}
