const SERVER_URL_PROTOCOL_PATTERN = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//;
export const SERVER_URL_COMPARABLE_KEY_ERROR_CODE = 'invalid_server_url';
export class ServerUrlComparableKeyError extends Error {
    code = SERVER_URL_COMPARABLE_KEY_ERROR_CODE;
    constructor(message) {
        super(message);
        this.name = 'ServerUrlComparableKeyError';
    }
}
function normalizeLoopbackHost(rawHost) {
    const host = String(rawHost ?? '').trim().toLowerCase().replace(/\.$/, '');
    if (host === 'localhost'
        || host === '127.0.0.1'
        || host === '::1'
        || host === '[::1]'
        || host.endsWith('.localhost')) {
        return 'localhost';
    }
    return host;
}
function resolveComparablePort(protocol, explicitPort) {
    if (!explicitPort) {
        return '';
    }
    if (protocol === 'https:' && explicitPort === '443') {
        return '';
    }
    if (protocol === 'http:' && explicitPort === '80') {
        return '';
    }
    return `:${explicitPort}`;
}
function parseServerUrlForIdentity(rawUrl) {
    const trimmed = String(rawUrl ?? '').trim();
    if (!trimmed) {
        throw new ServerUrlComparableKeyError('Invalid server URL: empty input');
    }
    const candidate = SERVER_URL_PROTOCOL_PATTERN.test(trimmed) ? trimmed : `https://${trimmed}`;
    let parsed;
    try {
        parsed = new URL(candidate);
    }
    catch {
        throw new ServerUrlComparableKeyError(`Invalid server URL: ${trimmed}`);
    }
    if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
        throw new ServerUrlComparableKeyError(`Invalid server URL protocol: ${parsed.protocol}`);
    }
    return parsed;
}
export function canonicalizeServerUrlForIdentity(url) {
    const parsed = parseServerUrlForIdentity(url);
    const protocol = parsed.protocol.toLowerCase();
    const host = normalizeLoopbackHost(parsed.hostname);
    const port = resolveComparablePort(protocol, parsed.port);
    return `${protocol}//${host}${port}`;
}
export function createServerUrlComparableKey(url) {
    return canonicalizeServerUrlForIdentity(url);
}
//# sourceMappingURL=serverUrlComparableKey.js.map