export interface TenantResolveQueryInput { host?: string; tenantCode?: string; } function normalizedHostname(value: string) { const raw = value.trim(); if (!raw) return ''; try { return new URL(`http://${raw}`).hostname.toLowerCase().replace(/^\[|\]$/g, '').replace(/\.$/, ''); } catch { return ''; } } export function isLocalRuntimeHost(value: string) { const hostname = normalizedHostname(value); return hostname === 'localhost' || hostname.endsWith('.localhost') || hostname === '::1' || hostname === '0.0.0.0' || /^127(?:\.\d{1,3}){3}$/.test(hostname); } export function tenantResolveQuery(input: TenantResolveQueryInput) { const host = input.host?.trim() || ''; const tenantCode = input.tenantCode?.trim() || ''; return { host: host || undefined, tenantCode: !host || isLocalRuntimeHost(host) ? (tenantCode || undefined) : undefined, }; }