const INVALID_SUBDOMAINS = new Set([ '0.0.0.0', 'www', 'api', 'admin', 'test', 'dev', 'development', 'staging', 'production', 'localhost', ]) const DEFAULT_TENANT = 'sistema' export function tenantFromHost(host: string | undefined | null): string { if (!host) return DEFAULT_TENANT const sub = host.split(':')[0]?.split('.')[0]?.toLowerCase() if (!sub || sub.length <= 1) return DEFAULT_TENANT if (INVALID_SUBDOMAINS.has(sub)) return DEFAULT_TENANT if (!/^[a-z0-9-]+$/.test(sub)) return DEFAULT_TENANT return sub } /** * Resolve o tenant atual — isomorfo (server e client). * No server, prefira passar o host via `tenantFromHost(useRequestHeaders(['host']).host)`. */ export function getClientTenant(): string { if (typeof window === 'undefined') return DEFAULT_TENANT return tenantFromHost(window.location.host) }