import { headers } from 'next/headers'

const RESERVED_SUBDOMAINS = ['www', 'app', 'admin', 'localhost', 'superadmin', 'api']

export async function getTenantSlug() {
    const host = (await headers()).get('host') || ''
    const domainParts = host.split(':')[0].split('.')

    if (
        domainParts.length > 2 ||
        (domainParts.length === 2 && domainParts[1] === 'localhost')
    ) {
        const slug = domainParts[0]
        if (!RESERVED_SUBDOMAINS.includes(slug)) {
            return slug
        }
    }

    return null
}
