"use strict";
/**
 * Port validation and management utilities
 * Handles port number validation and default port resolution
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveLifecyclePort = exports.normalizeProtocol = exports.getRemoteDefaultPort = exports.validateRemotePort = exports.validatePort = exports.CLIPROXY_DEFAULT_PORT = void 0;
const unified_config_loader_1 = require("../../config/unified-config-loader");
/** Default CLIProxy port */
exports.CLIPROXY_DEFAULT_PORT = 8317;
/**
 * Validate port is a valid positive integer (1-65535).
 * Returns default port if invalid.
 *
 * @param port - Port number to validate
 * @returns Valid port or CLIPROXY_DEFAULT_PORT (8317) if invalid
 */
function validatePort(port) {
    if (port === undefined || port === null) {
        return exports.CLIPROXY_DEFAULT_PORT;
    }
    if (!Number.isFinite(port) || port < 1 || port > 65535 || !Number.isInteger(port)) {
        return exports.CLIPROXY_DEFAULT_PORT;
    }
    return port;
}
exports.validatePort = validatePort;
/**
 * Validate and sanitize port number for remote connections.
 * Returns undefined for invalid ports (letting caller use default).
 * Valid range: 1-65535, must be integer.
 */
function validateRemotePort(port) {
    if (port === undefined || port === null)
        return undefined;
    if (typeof port !== 'number' || !Number.isInteger(port))
        return undefined;
    if (port <= 0 || port > 65535)
        return undefined;
    return port;
}
exports.validateRemotePort = validateRemotePort;
/**
 * Get default port for remote CLIProxyAPI based on protocol.
 * - HTTP: 8317 (CLIProxyAPI default)
 * - HTTPS: 443 (standard SSL port)
 */
function getRemoteDefaultPort(protocol) {
    return protocol === 'https' ? 443 : exports.CLIPROXY_DEFAULT_PORT;
}
exports.getRemoteDefaultPort = getRemoteDefaultPort;
/**
 * Normalize protocol to lowercase and validate.
 * Handles runtime case sensitivity (e.g., 'HTTPS' → 'https').
 * Defaults to 'http' for invalid values.
 */
function normalizeProtocol(protocol) {
    if (!protocol)
        return 'http';
    const normalized = protocol.toLowerCase();
    if (normalized === 'https')
        return 'https';
    if (normalized === 'http')
        return 'http';
    // Invalid protocol (e.g., 'ftp') - default to http
    return 'http';
}
exports.normalizeProtocol = normalizeProtocol;
/**
 * Resolve the local CLIProxy lifecycle port from unified config.
 * Falls back to default port when unset, invalid, or unreadable.
 */
function resolveLifecyclePort(config, loadConfig = unified_config_loader_1.loadOrCreateUnifiedConfig) {
    if (config) {
        return validatePort(config.cliproxy_server?.local?.port ?? exports.CLIPROXY_DEFAULT_PORT);
    }
    try {
        const loadedConfig = loadConfig();
        return validatePort(loadedConfig.cliproxy_server?.local?.port ?? exports.CLIPROXY_DEFAULT_PORT);
    }
    catch {
        return exports.CLIPROXY_DEFAULT_PORT;
    }
}
exports.resolveLifecyclePort = resolveLifecyclePort;
//# sourceMappingURL=port-manager.js.map