"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.logStartupDetails = logStartupDetails;
exports.logServerListening = logServerListening;
exports.logDevConsoleAvailable = logDevConsoleAvailable;
exports.formatServerAddress = formatServerAddress;
function logStartupDetails(logger, packageName, routes) {
    logger.info(`Starting ${packageName}`, { packageName });
    logger.info('HTTP routes registered', { routeCount: routes.length });
    for (const group of groupRoutesByController(routes)) {
        logger.info('HTTP controller registered', {
            controller: group.controllerClass.name || '(anonymous)',
            routeCount: group.routes.length,
            routes: group.routes.map(route => ({ method: route.method, path: route.path }))
        });
    }
}
function logServerListening(logger, server, fallbackPort, host) {
    logger.info('HTTP listening', { url: formatServerAddress(server, fallbackPort, host) });
}
function logDevConsoleAvailable(logger, server, fallbackPort) {
    const url = `http://localhost:${getServerPort(server, fallbackPort)}/_devconsole`;
    logger.info(`DevConsole available at ${url}`);
}
function formatServerAddress(server, fallbackPort, host) {
    const address = server.address();
    if (typeof address === 'string')
        return address;
    if (address)
        return `http://${formatListenHost(address, host)}:${address.port}`;
    return `http://${host ?? 'localhost'}:${fallbackPort}`;
}
function getServerPort(server, fallbackPort) {
    const address = server.address();
    return address && typeof address === 'object' ? address.port : fallbackPort;
}
function formatListenHost(address, host) {
    const hostname = host ?? address.address;
    if (hostname.includes(':') && !hostname.startsWith('['))
        return `[${hostname}]`;
    return hostname;
}
function groupRoutesByController(routes) {
    const groups = [];
    for (const route of routes) {
        let group = groups.find(item => item.controllerClass === route.controllerClass);
        if (!group) {
            group = { controllerClass: route.controllerClass, routes: [] };
            groups.push(group);
        }
        group.routes.push(route);
    }
    return groups;
}
//# sourceMappingURL=startup-logging.js.map