"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveDashboardUrls = exports.normalizeDashboardHost = exports.isWildcardHost = exports.isLoopbackHost = exports.DEFAULT_DASHBOARD_HOST = void 0;
const os = __importStar(require("os"));
exports.DEFAULT_DASHBOARD_HOST = 'localhost';
const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '::1']);
const WILDCARD_HOSTS = new Set(['0.0.0.0', '::']);
function isLoopbackHost(host) {
    return LOOPBACK_HOSTS.has(normalizeDashboardHost(host)?.toLowerCase() ?? '');
}
exports.isLoopbackHost = isLoopbackHost;
function isWildcardHost(host) {
    return WILDCARD_HOSTS.has(normalizeDashboardHost(host)?.toLowerCase() ?? '');
}
exports.isWildcardHost = isWildcardHost;
function normalizeDashboardHost(host) {
    if (!host) {
        return undefined;
    }
    const trimmedHost = host.trim();
    if (!trimmedHost) {
        return undefined;
    }
    if (trimmedHost.startsWith('[') && trimmedHost.endsWith(']') && trimmedHost.includes(':')) {
        return trimmedHost.slice(1, -1);
    }
    return trimmedHost;
}
exports.normalizeDashboardHost = normalizeDashboardHost;
function resolveDashboardUrls(host, port, networkInterfaces) {
    const bindHost = normalizeDashboardHost(host);
    if (!bindHost) {
        return {
            browserUrl: `http://localhost:${port}`,
        };
    }
    if (isWildcardHost(bindHost)) {
        return {
            bindHost,
            browserUrl: `http://localhost:${port}`,
            networkUrls: getExternalIpv4Urls(port, networkInterfaces ?? getNetworkInterfacesSafe()),
        };
    }
    return {
        bindHost,
        browserUrl: `http://${formatHostForUrl(bindHost)}:${port}`,
    };
}
exports.resolveDashboardUrls = resolveDashboardUrls;
function getExternalIpv4Urls(port, networkInterfaces) {
    if (!networkInterfaces) {
        return undefined;
    }
    const urls = [];
    const seen = new Set();
    for (const interfaceName of Object.keys(networkInterfaces)) {
        const candidates = networkInterfaces[interfaceName];
        if (!candidates) {
            continue;
        }
        for (const candidate of candidates) {
            const family = typeof candidate.family === 'string' ? candidate.family : String(candidate.family);
            if (family === 'IPv4' && !candidate.internal) {
                const url = `http://${candidate.address}:${port}`;
                if (!seen.has(url)) {
                    seen.add(url);
                    urls.push(url);
                }
            }
        }
    }
    return urls.length > 0 ? urls : undefined;
}
function getNetworkInterfacesSafe() {
    try {
        // Some constrained environments (for example proot) throw here; keep the local dashboard URL.
        return os.networkInterfaces();
    }
    catch {
        return undefined;
    }
}
function formatHostForUrl(host) {
    if (host.includes(':') && !host.startsWith('[') && !host.endsWith(']')) {
        return `[${host}]`;
    }
    return host;
}
//# sourceMappingURL=config-dashboard-host.js.map