export function buildJoinSlug(serverName: string, serverId: string): string {
  const base =
    serverName
      .toLowerCase()
      .normalize('NFD')
      .replace(/[\u0300-\u036f]/g, '')
      .replace(/[^a-z0-9]+/g, '-')
      .replace(/^-+|-+$/g, '')
      .slice(0, 24) || 'server';

  const suffix = serverId.replace(/-/g, '').slice(0, 6);
  return `${base}-${suffix}`;
}

export function extractSlugFromHostname(hostname: string, playDomain: string): string | null {
  const normalizedHost = hostname.split('\0')[0]?.toLowerCase() ?? '';
  const normalizedDomain = playDomain.toLowerCase();

  if (normalizedHost === normalizedDomain) {
    return null;
  }

  const suffix = `.${normalizedDomain}`;
  if (!normalizedHost.endsWith(suffix)) {
    return null;
  }

  const slug = normalizedHost.slice(0, -suffix.length);
  if (!slug || slug.includes('.')) {
    return null;
  }

  return slug;
}
