"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveOpenAICompatProxyPreferredPort = exports.resolveOpenAICompatProxyPortPreference = exports.listOpenAICompatProxyCandidatePorts = exports.resolveOpenAICompatProxyAdaptivePort = void 0;
const proxy_daemon_paths_1 = require("./proxy-daemon-paths");
const config_loader_facade_1 = require("../config/config-loader-facade");
const ADAPTIVE_PORT_RANGE_SIZE = proxy_daemon_paths_1.OPENAI_COMPAT_PROXY_ADAPTIVE_PORT_END - proxy_daemon_paths_1.OPENAI_COMPAT_PROXY_ADAPTIVE_PORT_START + 1;
function hashProfileName(profileName) {
    let hash = 0;
    for (const char of profileName.trim()) {
        hash = (hash * 31 + char.charCodeAt(0)) >>> 0;
    }
    return hash;
}
function resolveOpenAICompatProxyAdaptivePort(profileName) {
    return (proxy_daemon_paths_1.OPENAI_COMPAT_PROXY_ADAPTIVE_PORT_START +
        (hashProfileName(profileName) % ADAPTIVE_PORT_RANGE_SIZE));
}
exports.resolveOpenAICompatProxyAdaptivePort = resolveOpenAICompatProxyAdaptivePort;
function listOpenAICompatProxyCandidatePorts(profileName, preferredPort, excludedPorts = new Set()) {
    const candidates = new Set();
    if (!excludedPorts.has(preferredPort)) {
        candidates.add(preferredPort);
    }
    const adaptiveStart = resolveOpenAICompatProxyAdaptivePort(profileName);
    for (let offset = 0; offset < ADAPTIVE_PORT_RANGE_SIZE; offset += 1) {
        const candidate = proxy_daemon_paths_1.OPENAI_COMPAT_PROXY_ADAPTIVE_PORT_START +
            ((adaptiveStart - proxy_daemon_paths_1.OPENAI_COMPAT_PROXY_ADAPTIVE_PORT_START + offset) %
                ADAPTIVE_PORT_RANGE_SIZE);
        if (!excludedPorts.has(candidate)) {
            candidates.add(candidate);
        }
    }
    return [...candidates];
}
exports.listOpenAICompatProxyCandidatePorts = listOpenAICompatProxyCandidatePorts;
function resolveOpenAICompatProxyPortPreference(profileName) {
    const config = (0, config_loader_facade_1.loadOrCreateUnifiedConfig)();
    const profilePort = config.proxy?.profile_ports?.[profileName];
    if (typeof profilePort === 'number') {
        return { port: profilePort, source: 'profile' };
    }
    const sharedPort = config.proxy?.port;
    if (typeof sharedPort === 'number') {
        if (sharedPort === proxy_daemon_paths_1.OPENAI_COMPAT_PROXY_LEGACY_DEFAULT_PORT) {
            return {
                port: resolveOpenAICompatProxyAdaptivePort(profileName),
                source: 'adaptive',
            };
        }
        return { port: sharedPort, source: 'shared' };
    }
    return {
        port: resolveOpenAICompatProxyAdaptivePort(profileName),
        source: 'adaptive',
    };
}
exports.resolveOpenAICompatProxyPortPreference = resolveOpenAICompatProxyPortPreference;
function resolveOpenAICompatProxyPreferredPort(profileName) {
    return resolveOpenAICompatProxyPortPreference(profileName).port;
}
exports.resolveOpenAICompatProxyPreferredPort = resolveOpenAICompatProxyPreferredPort;
//# sourceMappingURL=proxy-port-resolver.js.map