import { isIPv6, isPrivate, isReserved } from "is-in-subnet";
import { networkInterfaces } from "os";
//#region ../zero-cache/src/config/network.ts
var DEFAULT_PREFERRED_PREFIXES = ["eth", "en"];
function getHostIp(lc, preferredPrefixes = DEFAULT_PREFERRED_PREFIXES) {
	const interfaces = networkInterfaces();
	const preferred = getPreferredIp(interfaces, preferredPrefixes);
	lc?.info?.(`network interfaces`, {
		preferred,
		interfaces
	});
	return preferred;
}
function getPreferredIp(interfaces, preferredPrefixes) {
	const rank = ({ name }) => {
		for (let i = 0; i < preferredPrefixes.length; i++) if (name.startsWith(preferredPrefixes[i])) return i;
		return Number.MAX_SAFE_INTEGER;
	};
	const sorted = Object.entries(interfaces).flatMap(([name, infos]) => (infos ?? []).map((info) => ({
		...info,
		name
	}))).sort((a, b) => {
		const ap = isIPv6(a.address) && isPrivate(a.address) || isReserved(a.address);
		if (ap !== (isIPv6(b.address) && isPrivate(b.address) || isReserved(b.address))) return ap ? 1 : -1;
		if (a.internal !== b.internal) return a.internal ? 1 : -1;
		if (a.family !== b.family) return a.family === "IPv4" ? -1 : 1;
		const rankA = rank(a);
		const rankB = rank(b);
		if (rankA !== rankB) return rankA - rankB;
		return a.address.localeCompare(b.address);
	});
	return sorted[0].family === "IPv4" ? sorted[0].address : `[${sorted[0].address}]`;
}
//#endregion
export { DEFAULT_PREFERRED_PREFIXES, getHostIp };

//# sourceMappingURL=network.js.map